1

I have a client program in java that sends a message "Hello" to python server.

Java code

    import java.io.*;  
    import java.net.*; 
    public class MyClient {
    public static void main(String[] args) {  

    try{      
    Socket soc=new Socket("localhost",2004);  
    DataOutputStream dout=new DataOutputStream(soc.getOutputStream());  
    dout.writeUTF("Hello");
    dout.flush();
    dout.close();  
    soc.close();
    }catch(Exception e){
        e.printStackTrace();}  
    }  

Python server code

    import socket               # Import socket module

    soc = socket.socket()         # Create a socket object
    host = "localhost" # Get local machine name
    port = 2004                # Reserve a port for your service.
    soc.bind((host, port))       # Bind to the port
    soc.listen(5)                 # Now wait for client connection.
    while True:
    conn, addr = soc.accept()     # Establish connection with client.
    print ("Got connection from",addr)
    msg = conn.recv(1024)
    print (msg)
    if ( msg == "Hello Server" ):
    print("Hii everyone")
    else:
    print("Go away")

the problem is that java client is able to send a Hello message to the python server but in python side else statement always executes with output " Go away".python version 2.7

Output:

('Got connection from', ('127.0.0.1', 25214))
Hello
Go away
1
  • Python does not understand Java's writeUTF() format. Only Java's writeUTF() understands it. Use writeBytes(). Commented Mar 2, 2019 at 8:38

6 Answers 6

4

you are getting 'Hello' from client.

if ( msg == "Hello" ):
    print("Hii everyone")
else:
    print("Go away")
Sign up to request clarification or add additional context in comments.

Comments

1

because the string you get on the server side has 2 hidden characters at the beginning, so you must remove these 2 characters before comparing it.

if ( msg[2:] == "Hello" ):

2 Comments

it worked for the code if ( msg[2:] == b"Hello" ) but what are the hidden characters?
I don't know :D
0

In the Java code you have written

dout.writeUTF("Hello");

But the Python server expects "Hello Server" to print "Hii Everyone".

Change Java Client code to

dout.writeUTF("Hello Server");

Comments

0

When you're transmitting data by using Java on the socket, it is transmitted on a Data Stream, when it is received it remains in the data stream or byte data:

_test/Pyserver.py
Got connection from ('127.0.0.1', 53518)
b'\x00\x0cHello Server'
Go away

The \x00\x0c are special characters included by UTF8 format by Java. As @user10854863 mentioned, if you use dout.writeBytes instead of dout.writeUTF, the output will change to:

Got connection from ('127.0.0.1', 53519)
b'Hello Server'
Go away

In order to get the passing value, you need to instruct python to decode the UTF formatting from input msg, to see its toString Value:

if (  msg.decode('utf-8') == "Hello Server" ):
        print("Hii everyone")

This in turn throws the output as:

_test/Pyserver.py
Got connection from ('127.0.0.1', 53622)
b'Hello Server'
Hii everyone

(Assuming you already knew strings "Hello" and "Hello Server" were a mismatch).

Comments

-1

Just Use proper syntax. Since you are sending "Hello", "Go away" will be your output.

if ( msg == "Hello Server" ):
    print("Hii everyone")
else:
    print("Go away")

1 Comment

This is not a syntax issue,
-1

The problem is that you have to specify the decryption UTF-8 in the Python server and then you should use dout.writeBytes("Hello Server") in the Java client.

1 Comment

UTF-8 is not an encryption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.