Note that there are some explanatory texts on larger screens.

plurals
  1. POPython server only receives one character from Java client?
    primarykey
    data
    text
    <p>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.</p> <p>Client code:</p> <pre><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(); } } </code></pre> <p>Server code:</p> <pre class="lang-py prettyprint-override"><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() </code></pre> <p>Strangely, the first time I instantiate the server and send a request from the Java client, everything behaves as expected:</p> <p>Java console:</p> <pre><code>test FROM SERVER: TEST </code></pre> <p>Command line:</p> <pre><code>127.0.0.1 wrote: test </code></pre> <p>But subsequent requests result in:</p> <p>Java console:</p> <pre><code>test FROM SERVER: T </code></pre> <p>Command line:</p> <pre><code>127.0.0.1 wrote: t </code></pre> <p>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.</p> <p>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.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload