Friday, 30 August 2013

What is wrong with this TCP interaction?

What is wrong with this TCP interaction?

I have TCPServer.java:
import java.util.*;
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket tcp = new ServerSocket(1387);
while (true) {
Socket request = tcp.accept();
Scanner sc = new Scanner(request.getInputStream());
DataOutputStream out = new
DataOutputStream(request.getOutputStream());
System.out.println("Request count: " + sc.nextLine());
out.writeBytes("send another request please");
}
}
}
and pythonClient.py:
#!/usr/bin/python
import socket, time, sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 1387))
count = 0
while True:
count += 1
sock.sendall(str(count) + "\n")
time.sleep(1)
data = sock.recv(1024)
print "received:\n", str(data)
I am running both on the same host and was expecting to receive "Request
count: x" every second with an incremented x value in the console of the
TCPServer and
received:
send another request please
every second in the terminal that is running pythonClient.py. However, all
I get is "Request count: 1" in java console and nothing else. In the
terminal, I get:
received:
s
received:
end another request please
Why is this not working as expected? Why is it all locked up after one
iteration and why did the first response from the server get accepted as
two different messages?

No comments:

Post a Comment