简体   繁体   English

Python服务器仅从Java客户端接收一个字符?

[英]Python server only receives one character from Java client?

I'm trying to implement a simple Python server to communicate with some simple Java client code, but I'm getting some strange behavior. 我正在尝试实现一个简单的Python服务器以与一些简单的Java客户端代码进行通信,但是却出现了一些奇怪的行为。 I'm running Mac OSX 10.7.2. 我正在运行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客户端发送请求时,一切都按预期进行:

Java console: Java控制台:

test

FROM SERVER: TEST

Command line: 命令行:

127.0.0.1 wrote:
test

But subsequent requests result in: 但是随后的请求导致:

Java console: Java控制台:

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. 为了进一步使事情复杂化,我创建了一个Java服务器和Python客户端,并且Java客户端/服务器组合和Python客户端/服务器组合没有这个问题,但是Python客户端/ Java服务器却没有。

Any ideas? 有任何想法吗?

Disable the Nagle algorithm on the client before passing the socket to DataOutputStream : 在将套接字传递给DataOutputStream之前,禁用客户端上的Nagle算法

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 . 这次,您的服务器代码中还有另一个潜在的问题,相当于这个问题的重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM