3

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();
        }

    }

}
Dreo
  • 229
  • 5
  • 12

1 Answers1

4

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.

Alden
  • 837
  • 1
  • 6
  • 15
  • 1
    Awesome. Thanks! After posting the question i put the out.print in an infinite loop and it started sending something after a while so there was the problem. – Dreo Aug 13 '15 at 09:00
  • Yes, outputting a large amount of data is another (less reliable) way to force it through the socket, because the buffer can only contain so much data before it runs out of space and must send some of what it has. – Alden Aug 13 '15 at 09:07
  • Same problem, worked for me as well, thanks – Ariel Gluzman Aug 29 '21 at 20:44