Regarding Python's socket module, how do you have client always receive data -
i server , client socket connection in python,
client:
import socket import sys sys import stdin serv = 'ip goes here' port = 8888 sock = socket.socket(socket.af_inet, socket.sock_stream) sock.connect((serv, port)) def prompt(): sys.stdout.write('<you> ') sys.stdout.flush() def send(x): sock.sendall(x) rec = 0 ex = len(x) while rec < ex: data = sock.recv(16) rec += len(data) while 1: if sock: data = sock.recv(16) if len(data) == 0: pass print '<server> %s' % data prompt() msg = stdin.readline() sock.sendall(msg)
the way have now, if on 1 client, send "alphabet" both clients other clients not receive until send new message waiting stdin.readline() occur. wondering how make clients receive data server, , see without having have stdin.readline() happen first.
thanks
achieved wanted using threads suggested crazycasta
def receiving(): while 1: if sock: data = sock.recv(16) if len(data) == 0: pass print '<server> %s' % data def sending(): while 1: msg = stdin.readline() sock.sendall(msg) t1 = thread(target=receiving) t2 = thread(target=sending) t1.start() t2.start()
Comments
Post a Comment