Home > Enterprise >  write with Python client to Java server
write with Python client to Java server

Time:09-17

I am trying to combine Python and Java using a socket connection. I hava a Java server and a Python client. They are able to connect to each other, and the server is able to write to the client, but when I try to send a message from the client to the server, it throws an EOFException. What should I do to get this to work?

Server code:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    
    public static void main(String[] args) {
        try {
            ServerSocket serversocket = new ServerSocket(6000);
            Socket client = serversocket.accept();

            final DataInputStream input = new DataInputStream(client.getInputStream());
            final DataOutputStream output = new DataOutputStream(client.getOutputStream());

            output.writeUTF("Hello Client!");

            String message = (String)input.readUTF();
            System.out.println(message);

            serversocket.close();

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

Client code:

import socket

socket = socket.socket()
host = "localhost"
port = 6000
socket.connect((host, port))

message = socket.recv(1024)
print(message.decode())

socket.sendall("Hello Server".encode())

socket.close()

The exception:

java.io.EOFException
        at java.base/java.io.DataInputStream.readFully(DataInputStream.java:203)
        at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:615)
        at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:570)
        at Server.main(Server.java:19)

CodePudding user response:

Option #1:

Replace input.readUTF() in server with this:

   while(true) {
         int ch = input.read();
         if (ch == -1) break;
         System.out.print((char)ch);
   }

Option #2:

If want to read UTF-encoded strings (vs plain ASCII) on server then recommend using BufferedReader with utf-8 charset and readLine().

ServerSocket serversocket = new ServerSocket(6000);
System.out.println("Waiting for connections");
Socket client = serversocket.accept();

final BufferedReader input = new BufferedReader(
    new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); // changed
final OutputStream output = client.getOutputStream();

//output.writeUTF("Hello Client!"); // see note below
output.write("Hello Client!".getBytes(StandardCharsets.UTF_8)) // changed

String message = input.readLine(); // changed
System.out.println(message);

client.close();
serversocket.close();

Client output:

Hello Client!

Server output:

Hello Server

Note JavaDoc of DataOutputStream#writeUTF(...) says:

First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow.

Using output.write(s.getBytes(StandardCharsets.UTF_8)) is more compatible with non-Java clients. Python utf-8 decoding doesn't support the 2-byte length prefix added by writeUTF().

Finally, if want the server to handle more than one client connection, then add a loop that encloses the code after ServerSocket is created and only close the client socket inside the loop.

  • Related