0

I'm sending a message from a python program to a Java program using sockets. When I initially run the program, the message is received by the server but when I re-run the client (without re-running the server) there is no message sent and the server receives a null. Please help?

python client:

import socket

HOST = "localhost"
PORT = 8080

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.connect((HOST, PORT))

#sock.sendall(b'\x00\x0dHello, world!\n')
sock.send(bytes("welcome hi\n","utf8"))

data = sock.recv(1024)
print(data.decode("utf-8"))


if data == (bytes("olleH\n","utf-8")):
    sock.send(bytes("Bye\n","utf8"))
    data = sock.recv(1024)
    print(data.decode("utf-8"))
   }
  }
}

Java server


import java.net.*;
import java.util.ArrayList;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Javaserver {
    private static ArrayList<ClientHandler> clients = new ArrayList<>();
    private static ExecutorService pool = Executors.newFixedThreadPool(2);
    private static final int PORT = 8080;
    private ServerSocket server= null;





    public static void main(String args[]) throws Exception {

        System.out.println("wait for connection on port 8080");
        ServerSocket server = new ServerSocket(PORT);
        Socket client = server.accept();

        while (true) {




            System.out.println("got connection on port 8080");
            ClientHandler clientThread = new ClientHandler(client);
            clients.add(clientThread);
            pool.execute(clientThread);

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.Socket;

public class ClientHandler implements Runnable {
    private Socket client;
    private BufferedReader in;
    private PrintWriter out;
    private String fromClient;
    private String toClient;
    private int id;






    public ClientHandler(Socket client) throws IOException {
        this.client = client;

        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(), true);


    }

    @Override
    public void run() {
        boolean run = true;
        try {
            while (run) {




                fromClient = in.readLine();
                System.out.println("received: " + fromClient );


                if (fromClient.equals("welcome hi")) {
                    toClient = "olleH";
                    System.out.println("send olleH");
                    out.println(toClient);
                    fromClient = in.readLine();
                    System.out.println("received: " + fromClient);

                }
                if(fromClient.equals("ho")){
                    toClient = "olleH";
                    System.out.println("send olleH");
                    out.println(toClient);
                    fromClient = in.readLine();
                    System.out.println("received: " + fromClient);


                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Rizana Fazily
  • 51
  • 2
  • 11

1 Answers1

0

To listen to new socket connections you must use:

ServerSocket server = new ServerSocket(PORT);
Socket client = server.accept();

You are using this on your main method outside any kind of loop, so if the client closes connection the server won't listen for new connections unless you run again that part of code (so the server should be rerun).

If you don't want to rerun the server you could create a method that listens for a new socket and then call it if the client closes the current socket connection.

public Socket waitForClient(ServerSocket server) {
    return server.accept();
}

To check if the socket has been closed you could use read() == -1 or readLine == null if the client has closed the socket using close() method. If not, you could also determine if its closed by catching an IOException. Refer to this post for more info.

PauMAVA
  • 1,163
  • 14
  • 23