套接字Java客户端-Python服务器 [英] Socket Java client - Python Server

查看:163
本文介绍了套接字Java客户端-Python服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Java-python客户端/服务器套接字。客户端使用Java,服务器使用python

I'm tring to implement a java - python client/server socket. The client is in java and the server is write in python

Java客户端

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

public class client {

public static void main(String[] args) {  



    try{      
        Socket socket=new Socket("localhost",2004);  

        DataOutputStream dout=new DataOutputStream(socket.getOutputStream());  
        DataInputStream din=new DataInputStream(socket.getInputStream());


        dout.writeUTF("Hello");
        dout.flush();

        System.out.println("send first mess");
        String str = din.readUTF();//in.readLine();

        System.out.println("Message"+str);


        dout.close();  
        din.close();
        socket.close();
        }

    catch(Exception e){
        e.printStackTrace();}   


}  

}

Python服务器

import socket               

soc = socket.socket()         
host = "localhost" 
port = 2004                
soc.bind((host, port))      
soc.listen(5)                 

while True:
    conn, addr = soc.accept()     
    print ("Got connection from",addr)
    msg = conn.recv(1024)
    print (msg)
    print(len(msg))

    if "Hello"in msg:
         conn.send("bye".encode('UTF-8'))
    else:
         print("no message")

从客户端到服务器的第一条消息正确传递,而从服务器到客户端的第二条消息正确传递。我使用telnet检查服务器是否发送了邮件,但客户端陷入僵局,没有收到邮件。
我不明白为什么。

The first message from client to server was delivery correctly but the second from server to client no. Using telnet I check that sever send the message but the client goes in deadlock and don't receive message. I don't understand why.

谢谢

推荐答案

似乎您的缩进功能已在Python服务器中关闭,因为无法将
的消息发送回客户端的代码。

It seems that your indentation is off in the Python server, as the code to send the message back to the client is unreachable.

即使修复后缩进表示,您的服务器实现不正确,因为 msg 不是 String 。您需要对 msg 进行解码,如下所示。另外,由于在您的邮件中使用 DataInputStream#readUTF ,因此您需要以 short 的形式发送消息的长度。客户端:

Even after fixing the indentation, your server implementation is not correct, as msg is not a String. You need to decode msg as seen below. Also, you need to send the length of the message as a short since you're using DataInputStream#readUTF in your client:

import socket

soc = socket.socket()
host = "localhost"
port = 2004
soc.bind((host, port))
soc.listen(5)

while True:
    conn, addr = soc.accept()
    print("Got connection from",addr)
    length_of_message = int.from_bytes(conn.recv(2), byteorder='big')
    msg = conn.recv(length_of_message).decode("UTF-8")
    print(msg)
    print(length_of_message)

    # Note the corrected indentation below
    if "Hello"in msg:
        message_to_send = "bye".encode("UTF-8")
        conn.send(len(message_to_send).to_bytes(2, byteorder='big'))
        conn.send(message_to_send)
    else:
        print("no message")

这篇关于套接字Java客户端-Python服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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