I'm tring to implement a java - python client/server socket. The client is in java and the server is write in python
Java Client
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 server
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")
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.
Thanks
Python Socket Client This program is similar to the server program, except binding. The main difference between server and client program is, in server program, it needs to bind host address and port address together. See the below python socket client example code, the comment will help you to understand the code.
Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.
Create a Socket server To create the server socket, the endPoint object can listen for incoming connections on any IP address but the port number must be specified. Once the socket is created, the server can accept incoming connections and communicate with clients.
It seems that your indentation is off in the Python server, as the code to send the message back to the client is unreachable.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With