I'm trying to transfer simple message between PHP socket and JAVA socket. The php socket successfully sends the data and is waiting for Java servers response. But on the other hand Java server's socket is still waiting for the message from PHP.
Here is the Java Code:
ServerSocket s = new ServerSocket(4280);
Socket sock = s.accept();
System.out.println("Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
System.out.println("Reading");
String str = br.readLine();
System.out.println("Writing");
bw.write(str);
Output:
Connected
Reading
Here's the PHP code:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "localhost", 4280);
socket_write($socket, "Hello");
echo socket_read($socket, 10);
socket_write($socket, "Lelo");
echo socket_read($socket, 10);
Output:
Browser: waiting for localhost
Two things that can usually cause a problem:
Java is utilizing the readLine() method but your not sending a linefeed and return in your PHP code.
Try also flushing on the PHP side.
Code:
Adding linefeed:
socket_write($socket, "Hello\r\n");
String str = br.readLine(); expect a \n which is not sent by the PHP program.
Add this :
socket_write($socket, "Hello\n" ); // <<<=== '\n' added
Related
i have a simple application which create a socket between java(server) and python(client).
The main function of the python code is to take data from user and send it to the server(java code)
here's the python code
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))
while True:
data = input("Enter data to send : ")
if(data == 'q'):
break
client_socket.sendall(data.encode('utf-8'))
client_socket.close()
and here's the java code
String fromclient;
ServerSocket Server = new ServerSocket (5000);
while(true)
{
Socket connected = Server.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connected.getInputStream()));
fromclient = inFromClient.readLine();
if ( fromclient.equals("q") ){
connected.close();
break;
}else {
System.out.println(fromclient);
}
}
when I write any text and click Enter, nothing goes to java code and nothing printed to the console, but when i send 'q' from python, the python code closed and all the data i wrote are now printed in java console.
I have no idea what is the reason of this, and how i can fix it.
The Java code waits for a line-break, but the Python part does not send one (input provides no line-break in the string it returns).
Try
client_socket.sendall((data+'\n').encode('utf-8'))
As #Kayaman suggests, the accept is in a wrong place (and also is the BufferedReader).
Socket connected = Server.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connected.getInputStream()));
while(true)
{
fromclient = inFromClient.readLine();
would be a better order.
Also, Python client does not send the 'q' in its current form. So the if with the fromclient.equals("q") will not close the socket, the code will just die on the next readLine() instead. Re-order the Python part too:
data = input("Enter data to send : ")
client_socket.sendall((data+'\n').encode('utf-8'))
if(data == 'q'):
break
I am having a little bit of trouble sending and receiving strings from client to server. Assume I have the sockets set up correctly.
This is what I am using to send/receive server side:
fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
toClient = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String clientInput;
clientInput = fromClient.readLine();
is how my server receives inputs from the client.
Client side same deal:
toServer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inputLine = bufferedReader.readLine(); //inputLine reads from the console
toServer.write(inputLine);
I can send a message to the sever and it will receive it but when I uncomment out this bit for the client to receive a response from the server:
// serverInput = fromServer.readLine();
//
// System.out.println(serverInput);
It will hang and the server side wont receive the initial message sent. I have no idea whats wrong and I just want to get a reply from the server. Any help is appreciated. Thanks
BufferedReader.readLine() will strip the newline character for you.
That means, at client side, inputLine does not have a trailing \n, which means the client did not send the end of line signal to the server and vice versa.
Client Side
toServer.write(...);
toServer.newLine(); // <--- send new line
toServer.flush(); // <--- flush buffered data
Server should do the similar thing.
I am trying to connect from a android emulator to a application on my desktop and send a line of text.
My app is able to connect to the server, but when ever i try to read data its always null
Server application running on my desktop:
ServerSocket ss = new ServerSocket(9001);
Socket cs = ss.accept();
if (cs.isConnected()) {
System.out.println("Client connected.");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(cs.getInputStream()));
String str = reader.readLine();
System.out.println("Data:" + str);
Client application running in android app emulator:
InetAddress addr = InetAddress.getByName("10.0.2.2");
Socket socket = new Socket(addr, 9001);
if (socket.isConnected()) {
Log.d("APP", "socket connected");
}
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String str = "this is a sample";
pw.write(str);
I can see that the isConnected function of the socket on both the client and server turns true.
But the Data printed on the server is always null.
Thanks
Try adding a flush call after the 'pw.write(str)' statement:
pw.flush();
I want to send a message with socket to an java server and it should response.
InputStreamReader inputStream = new InputStreamReader(server.getInputStream());
BufferedReader input = new BufferedReader(inputStream);
String clientSentence = input.readLine();
System.out.println(clientSentence);
This infinite, so I can't send a response to my php socket connection.
PHP:
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
$st="testSalt,broadcast";
$length = strlen($st);
socket_write($socket, $st, $length);
$resp = socket_read($socket, 1024);
Terminate strings from the PHP socket with a newline character to match the readLine statement of the Java server
$st = "testSalt,broadcast\n";
I don't know much about PHP, but it looks to me that if you send the contents of $st as-is, there won't be any newline character for the Java socket to read, and thus the input.readLine() call will hang until the Socket closes.
I am trying to read a string from php client socket in a java server socket. I am writing two strings in php socket client
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
socket_connect($this->socket, $this->host, $this->port)
$text = 'SGSA:PRINT:RECIBO';
socket_write($this->socket, $text, strlen($text));
$text = 'Mensagem para imprimir';
socket_write($this->socket, $text, strlen($text));
However when I read it in java like below
Socket clientSocket = server.accept();
InputStream in = clientSocket.getInputStream();
String msg;
try {
msg = IOUtils.toString( in );
} finally {
IOUtils.closeQuietly(in);
}
It reads two string together at once.
run-single:
SGSA:PRINT:RECIBOMensagem para imprimir
How can I read one string per time?