Last active
November 22, 2016 12:51
-
-
Save efaisal/7081fcb26df68d63006f3e7844c9d92e to your computer and use it in GitHub Desktop.
Simple example of socket programming using Python and threading module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
int main(int argc, char *argv[]) { | |
struct sockaddr_in server; | |
int sock; | |
char msg[] = "12"; | |
/* Server info */ | |
server.sin_family = AF_INET; | |
server.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
server.sin_port = htons(8000); | |
/* Socket */ | |
sock = socket(AF_INET, SOCK_STREAM, 0); | |
if (sock == -1) { | |
printf("Failed to create socket\n"); | |
return 1; | |
} | |
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { | |
perror("Connect failed"); | |
return 1; | |
} | |
send(sock, msg, strlen(msg), 0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import socket | |
if __name__ == '__main__': | |
host = "127.0.0.1" | |
port = 8000 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((host, port)) | |
s.send(b"10") | |
s.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import socket | |
import traceback | |
import threading | |
def ledBlink(): | |
print ("Python Client Connected") | |
def ledKelip(): | |
print("Java Client Connected") | |
def ledKelap(): | |
print("C Client Connected") | |
def handle_conn(sock, addr): | |
print("Got Connection from {} and handled by {}".format(addr, threading.currentThread().getName())) | |
msg = '' # message buffer | |
while True: | |
data = sock.recv(1024) | |
if not len(data): | |
break | |
msg += str(data, 'utf-8') | |
if msg == '10': | |
ledBlink() | |
elif msg == '11': | |
ledKelib() | |
elif msg == '12': | |
ledKelap() | |
else: | |
print('Unknown command from {}'.format(addr)) | |
sock.close() | |
if __name__ == '__main__': | |
host = "127.0.0.1" | |
port = 8000 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind((host, port)) | |
s.listen(5) | |
print("Server Running..... Listening to Clients Now") | |
print("Press Ctrl-C to quit") | |
while True: | |
try: | |
clientsock, clientaddr = s.accept() | |
except KeyboardInterrupt: | |
break | |
except: | |
print('are we having error') | |
traceback.print_exc() | |
continue | |
t = threading.Thread(target=handle_conn, args=[clientsock, clientaddr]) | |
t.start() | |
s.close() | |
print('Server terminating') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment