Created
September 20, 2017 18:02
-
-
Save clucle/691eb0b446b600ff4c770d8914d49efd to your computer and use it in GitHub Desktop.
get input assignment
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 <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <string.h> | |
int main(int argc, char **argv) | |
{ | |
int sockfd, n; | |
char sendline[1000]; | |
char recvline[1000]; | |
struct sockaddr_in servaddr; | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
bzero(&servaddr, sizeof servaddr); | |
servaddr.sin_family = AF_INET; | |
servaddr.sin_port = htons(50000); | |
// your address ex)ec2-(??).ap-northeast-2.compute.amazonaws.com | |
inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr)); | |
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); | |
while(1) | |
{ | |
bzero(sendline, 100); | |
bzero(recvline, 100); | |
fgets(sendline, 100, stdin); | |
write(sockfd, sendline, strlen(sendline) + 1); | |
if(!strcmp(sendline, "0\n")) break; | |
//printf("%d", sendline); | |
//printf("%s", recvline); | |
} | |
} |
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
import socket | |
import threading | |
import sys | |
class Server(): | |
def __init__(self, host="localhost", port=50000): | |
self.clientes = [] | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.sock.bind((str(host), int(port))) | |
self.sock.listen(10) | |
self.sock.setblocking(False) | |
# Client Info | |
self.num_client = -1 | |
self.ID_client = [] | |
#thread - connect client, thread - receive Msg | |
thread_Accept = threading.Thread(target=self.Msg_Accept) | |
thread_Receive = threading.Thread(target=self.Msg_Receive) | |
thread_Accept.start() | |
thread_Receive.start() | |
def Msg_Accept(self): | |
print("waiting client connect") | |
while True: | |
try: | |
conn, addr = self.sock.accept() | |
conn.setblocking(False) | |
self.clientes.append(conn) | |
except: | |
pass | |
def Msg_Receive(self): | |
print("waiting client data") | |
while True: | |
if len(self.clientes) > 0: | |
c_count = 0 | |
for c in self.clientes: | |
c_count += 1 | |
try: | |
data = c.recv(1024) | |
if data: | |
print (c_count, " : ", data) | |
except: | |
pass | |
s = Server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment