Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket Java client - Python Server

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

like image 733
epascolo Avatar asked Jan 15 '18 15:01

epascolo


People also ask

What is the difference between a client socket and a server socket in python?

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.

What is socket in server and client?

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.

Can server open socket client?

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.


1 Answers

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")
like image 130
Jacob G. Avatar answered Oct 04 '22 14:10

Jacob G.