连接python套接字和java套接字 [英] connecting python socket and java socket

查看:137
本文介绍了连接python套接字和java套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Java客户端套接字和Python服务器套接字之间发送一个简单的字符串。这是服务器套接字的代码:

I have been trying to send a simple string between a Java client socket and a Python server socket. This is the code for the server socket:

HOST=''
PORT=12000
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADRR,1)
s.bind((HOST,PORT))
s.listen(5)
device=variador()
while True:

  conn,addr=s.accept()
  if data=="turn_on":
     respuesta=device.send_order(variador.start_order)
     conn.send(respuesta+'\n')
     conn.close()

,客户代码是:

   try {

            Socket socket = new Socket("192.168.10.171", 12000);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                                       socket.getInputStream()));    

            BufferedReader stdIn = new BufferedReader(
                                          new InputStreamReader(System.in));

            out.print(command);

            out.close();
            in.close();
            socket.close();

        } catch (UnknownHostException e) {
            System.err.println("Unknown Host.");
           // System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection.");
          //  System.exit(1);
        }

一切正常,直到我尝试读取服务器的响应,使用:

Everything works fine until I try to read the server's response, using this:

    String userInput;
  while ((userInput = stdIn.readLine()) != null) {
  out.println(userInput);
  System.out.println("echo: " + in.readLine());
  }

然后代码挂起,Python服务器没有收到任何信息,我使用print进行测试。

then the code hangs and the Python server does not receive any information, which I tested using print.

尝试先发送然后等待Java客户端服务器的响应是否有问题?

Is there a problem trying to send first and then wait for a response from the server in the Java client?

我们非常感谢任何帮助。

Any help will be much appreciated.

推荐答案

好吧,我发现java客户端因为消息而挂起由python服务器发送的没有用\\\显式完成,所以python代码应该是这样的:

Well, I discovered that the java client hangs because the messages sent by the python server were not explicitly finished with \r\n, so the python code should have been something like this:

     HOST=''
     PORT=12000
     s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADRR,1)
     s.bind((HOST,PORT))
     s.listen(5)
     device=variador()
     while True:

       conn,addr=s.accept()
       if data=="turn_on\r\n":
        respuesta=device.send_order(variador.start_order)
        conn.send(respuesta+'\r\n')
       conn.close()

我知道从java,readline()和println中的方法名称来看应该是非常明显的,两者都表明java以序列\\\\ n来结束字符串

I know it should have been quite obvious from the name of the methods in java, readline() and println, both suggesting that java ends strings with the sequence \r\n

这篇关于连接python套接字和java套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆