Dreo
Dreo

Reputation: 229

Server(Python) - Client(Java) communication using sockets

I try to send a message from server to a client, after client receives the message, it sends back a message to the server and so on. The problem is with receiving the message in python. The loop it's stuck there.

import socket

HOST = "localhost"
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')

try:
    s.bind((HOST, PORT))
except socket.error as err:
    print('Bind failed. Error Code : ' .format(err))
s.listen(10)
print("Socket Listening")
conn, addr = s.accept()
while(True):
    conn.send(bytes("Message"+"\r\n",'UTF-8'))
    print("Message sent")
    data = conn.recv(1024)
    print(data.decode(encoding='UTF-8'))

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class Main {
    static Thread sent;
    static Thread receive;
    static Socket socket;

    public static void main(String args[]){
            try {
                socket = new Socket("localhost",9999);
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            sent = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        BufferedReader stdIn =new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                        while(true){
                            System.out.println("Trying to read...");
                                String in = stdIn.readLine();
                                System.out.println(in);
                                out.print("Try"+"\r\n");
                                System.out.println("Message sent");
                            }

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                }
            });
        sent.start();
        try {
            sent.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Upvotes: 3

Views: 9407

Answers (1)

Alden
Alden

Reputation: 837

The Python code is fine. The problem is that calling out.print in the Java code does not necessarily cause your message to be sent through the socket immediately. Add

out.flush();

immediately after

out.print("Try"+"\r\n");

to force the message to be sent through the socket. (flush "flushes" through the stream any data that has not yet been sent.) The Python should then be able to receive it correctly.

Upvotes: 4

Related Questions