4

I'm trying to implement a simple Python server to communicate with some simple Java client code, but I'm getting some strange behavior. I'm running Mac OSX 10.7.2.

Client code:

import java.io.*;
import java.net.*;

public class JavaClient {
    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost", 9999);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = inFromUser.readLine() + '\n';
        outToServer.writeBytes(sentence);
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
        clientSocket.close();
    }
}

Server code:

import SocketServer

class PythonServer(SocketServer.BaseRequestHandler):

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

Strangely, the first time I instantiate the server and send a request from the Java client, everything behaves as expected:

Java console:

test

FROM SERVER: TEST

Command line:

127.0.0.1 wrote:
test

But subsequent requests result in:

Java console:

test

FROM SERVER: T

Command line:

127.0.0.1 wrote:
t

So it looks like my server is only receiving the first character of my message, despite the fact that my client believes that it's sending the whole message.

To further complicate things, I created a Java server and Python client and the Java client / server combo and Python client / server combo don't have this problem, but the Python client / Java server does.

Any ideas?

Karen
  • 885
  • 2
  • 7
  • 11
  • Try to `flush` your `outToServer` after writing the data that needs to go out to the server – GETah Jun 18 '12 at 17:03
  • @GETah I added a line to flush the buffer after writing to it, but this didn't change the behavior. Any other ideas? – Karen Jun 18 '12 at 17:14

1 Answers1

2

Disable the Nagle algorithm on the client before passing the socket to DataOutputStream:

clientSocket.noTcpDelay(true);

This is somewhat wasteful regarding network bandwidth but we can address that as well if that would be a problem for you.

There is also another potential problem, this time in your server code, which amounts to a duplicate of this question.

Community
  • 1
  • 1
Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • 1
    Enabling / disabling the Nagle algorithm doesn't seem to effect my output, but your second link fixed the problem - thank you! – Karen Jun 18 '12 at 18:30