Tuesday, 10 September 2013

Data miss in socket transfer

Data miss in socket transfer

I want to build a server socket and send a line to an client and the
client will print the line
but when I print the data received from server socket , only the first
line of the file be print , others are miss
this is the data I want to send to client
1000 2000 3000 4000 5000
1000 3000 5000 7000 9000
1111 2222 3333 4444 5555
server
import socket
Input = open("./Data","r")
data = Input.read()
Input.close()
# Construct the server_socket
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.bind(('localhost',15000))
server_sock.listen(1)
(client_socket,address) = server_sock.accept()
# send data
for line in data:
client_socket.send(line)
client_socket.send("EOF")
client_socket.close()
server_sock.close()
client
import socket
client_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_sock.connect(('localhost',15000))
data = ""
while True:
part = client_sock.recv(100)
data = data + part
# retrieve a line
if "\n" in data or "EOF" in data:
list = data[:data.index("\n")].split(" ")
print(list)
# I notice sometimes the data will still have data after the
"\n" so I add the following two lines of code , but the
program will crash
remain = data[data.index("\n")+1:]
data = remain
if "EOF" in data:
break
client_sock.close()
result.close()

No comments:

Post a Comment