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();
 }
 }
}
asked Aug 13, 2015 at 8:19

1 Answer 1

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.

answered Aug 13, 2015 at 8:56
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.