Java client python server socket programming

前端 未结 5 1438
花落未央
花落未央 2021-02-06 19:38

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

Java code

    import java.io.*;  
    import java.net.*; 
    public cla         


        
相关标签:
5条回答
  • 2021-02-06 20:14

    you are getting 'Hello' from client.

    if ( msg == "Hello" ):
        print("Hii everyone")
    else:
        print("Go away")
    
    0 讨论(0)
  • 2021-02-06 20:14

    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.

    0 讨论(0)
  • 2021-02-06 20:25

    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")
    
    0 讨论(0)
  • 2021-02-06 20:26

    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" ):
    
    0 讨论(0)
  • 2021-02-06 20:36

    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");
    
    0 讨论(0)
提交回复
热议问题