I am currently trying to send strings via sockets between Java and c. I am able to either send a String to the client (c) from the server (java), or vice versa, but not BOTH, which is how I need to communicate between the two. In my c (client) code, as soon as I insert the read portion, the code haults.
Here are my two portions of code. It is safe to assume the connection between the sockets is successful.
java:
private void handshake(Socket s) throws IOException{
this.out = new PrintStream(s.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String key = in.readLine(); //get key from client
if(!key.equals(CLIENTKEY)){
System.out.println("Received incorrect client key: " + key);
return;
}
System.out.println("received: " + key);
System.out.println("sending key");
out.println("serverKEY"); //send key to client
System.out.println("sent");
}
c:
int n;
n = write(sockfd,"clientKEY",9);
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
n = read( sockfd,recvBuff,255 );
if (n < 0)
{
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",recvBuff);
Modify your C send code:
char clientKey[] = "clientKEY\n"
n = write(sockfd,clientKey, strlen(clientKey));
It's better to use a variable for clientKey and then call strlen so you don't have to count char's manually. As Jiri pointed out, Java's readLine function is probably expecting a newline char that it's never getting so it hangs.
It seems to me that the C/C++ server sends a clientKEY message to the Java client. The Java client reads a line, i.e. waits till it receives the \n character from the C/C++ server. However, it is never sent by the C/C++ server and so the Java client waits... forever.
Related
I am trying to send receive data using a Python server and a Java client. First, Java sends a JSON in string to Python Server. After the string received, Python server will send a JSON back to the client. After the client receives the JSON from the server, it again sends a JSON in string to server. (Client sends the same message all the time) This is a recursive process.
ISSUE: After when I execute both Python server and Java, Python server receives the message sent by the Java Client and it sent back the JSON. But in the client side, the message sent by the server didnt receive.
Server.py
import socket
import threading
import json
import numpy
HEADER_INITIAL = 25
PORT = 1234
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
def handle_client(self, conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg = conn.recv(HEADER_INITIAL).decode(FORMAT)
if msg:
print("[DATA] RECEIVED"+ str(msg))
x = {
"Sentence": "This is a value"
}
y = json.dumps(x)
conn.send(y.encode(FORMAT))
conn.send("\n".encode(FORMAT));
conn.close()
Client.java
try (Socket socket = new Socket(Address, Port)) {
InputStream input = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
int character;
StringBuilder data = new StringBuilder();
while(true){
Thread.sleep(4000);
String strJson = "{'message':'Hello World'}";
JSONObject jsonObj = new JSONObject(strJson);
writer.println(jsonObj.toString());
while((character = reader.read()) != -1) {
data.append((char) character);
}
System.out.println(data);
}
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
UPDATE
Here is the debug output.
I first started the server and then started client. Initially server receives the {'message':'Hello World'} value which is sent by the client and the server sends back the value of the x variable to the client. Then the server receives nothing from the client, but the client prints the value of x continuously.( System.out.println(data);) I tried to send dynamic values from the server to client, but the client prints only the value which is sent by the server in the first time.
You don't provide any debugging output so it's difficult to be 100% sure this is the entire cause. However, it seems pretty evident that this section of your client code isn't correct:
while((character = reader.read()) != -1) {
data.append((char) character);
}
System.out.println(data);
The server is holding the connection open forever (nothing ever sets connected to false). And so in the loop above, the character returned by reader.read will never be -1 because -1 is only returned at "end of stream". End of stream will only occur when the server closes its socket -- or is otherwise disconnected.
You should add a check for the newline to break out of the read loop:
if (character == '\n')
break;
or you could add it to the while condition:
while ((character = reader.read()) != -1 && character != '\n') {
...
Your code overall lacks appropriate handling of possible exceptional conditions. For example, if the client disconnects, your server will never exit its loop. It will call recv, get back an empty string (signifying "end of file" on the connection), and so will correctly bypass sending a response, but it will then simply go back and execute recv again, get an empty string again, and so forth forever.
Also, your python code makes the implicit assumption that the recv returns exactly the single string that was sent by the client, which is not guaranteed. If the client sends a 20 character string for example, it's possible that the first server recv call returns the first 10 characters, and the next call returns the rest.
(In practice, given the sleep in the client side code, that's unlikely to be a problem in this snippet of code, but one should program defensively because in a real production program, there will inevitably be a race or edge case that will do exactly this and it will cause the client and server to get out of sync and be difficult to debug.)
I was trying this code. it works fine but if I remove \n in String str it doesn't work I mean It was able to compile without \n but it didn't give me output.
public class Test {
// Use try-with-resources to close a socket.
public static void main(String args[]) throws Exception {
int c;
// Create a socket connected to internic.net, port 43. Manage this
// socket with a try-with-resources block.
try (Socket s = new Socket("whois.internic.net", 43)) {
// Obtain input and output streams.
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// Construct a request string.
String str = (args.length == 0 ? "MHProfessional.com" : args[0]) + "\n"; // <- herer
// Convert to bytes.
byte buf[] = str.getBytes();
// Send request.
out.write(buf);
// Read and display response.
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
}
// The socket is now closed.
}
}
The server you're talking to reads data up until an end-of-line (\n) character -- that's just the way it works, but it's far from unusual. It's possible that other end-of-line sequences will be accepted as well.
The server has to have some way to know the client has finished sending data. It will probably know if the client closes its connection, but by then it's too late to respond to the client.
I'm trying to read the data back from a server program that I didn't write. The server program doesn't send any kind of end of transmission character and it doesn't close the socket once it sends a response. There is a button I can press on the server to close the connection manually and if I leave a little bit of a timeout on the (android)client side so I have time to press it, I do get the data from the server into my client app. Otherwise it just eventually times out and I get no response. Since I can't control the protocol on the server side how can I close the connection and get the response from the server? Thanks for any help.
cSocket.setSoTimeout(timeOut);
cOut = new PrintWriter(cSocket.getOutputStream(), true);
cOut.println(msgIn);
cIn = new BufferedReader(new InputStreamReader(
cSocket.getInputStream()));
int intTest;
while ((intTest = cSocket.getInputStream().read()) != -1) {
System.out.println(cIn.readLine());
Message from the server looks like this:
char(60)...char(62)char(60)...char(62)char(60)...char(62)char(60)...char(62)
... is random data inside the chars doesn't duplicate.
*Final edit
Got it working like this:
int c;
int intCount = 0;
StringBuilder response= new StringBuilder();
while ((c = cIn.read()) != -1) {
response.append( (char)c ) ;
if (c == 62) {
intCount = intCount + 1;
}
if (intCount >=4) {
cSocket.close();
String result = response.toString();
System.out.println(result);
break;
}
}
while ((intTest = cSocket.getInputStream().read()) != -1) {
System.out.println(cIn.readLine());
This doesn't make any sense. Just because you read a byte doesn't mean you can read another whole line, and you're throwing away the byte you did read. It should be:
while ((intTest = cSocket.getInputStream().read()) != -1) {
System.out.println(intTest);
And intTest is a poor name for an input byte. There's no 'test' about it. I would call it i, or b.
I have been dealing with one problem all day and I googled it in many different ways but I just can not find a solution, so I guest is time to ask:
I'm trying to code two apps: the client and the server with java using sockets and everything except one thing works fine: the client won't receive all the messages but only pair ones.
What I am doing is looping a 2D array to send every "x" time a message from the server to the client but for example if the loop send 8 messages the client only receive messages 2, 4, 6 and 8.
I printed in my server side console the output just as a verification that the array is property filled and the server display all the messages but not the client.
Im using this:
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF(getMACAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
while (true)
{
String input = in.readUTF();
if (input.equals("finish"))
{
System.out.println("Server has closed the connection\nGoodBye!");
System.exit(0);
}
else
{
System.out.println("Server says " + in.readUTF());
}
}
The problem is that you read input for comparison to "finish", and then you read a whole new string that you never compare.
Instead, you should print input in the else branch of the conditional, i.e.
while (true) {
String input = in.readUTF();
if (input.equals("finish")) {
System.out.println("Server has closed the connection\nGoodBye!");
System.exit(0);
} else {
System.out.println("Server says " + input);
// ^^^^^
}
}
I have a C++ server and two clients (ruby and java).
Everything is running on a 64-bit linux-machine (java 1.7.0_17)
The ruby client is fully working, but the java version makes problems.
In Java I tried to send a String from the client to the server. Actually the Server received the entire String, but the server thinks there is still something more to receive.
The ruby client looks a little bit like this:
socket = TCPSocket.open(#options[:host],#options[:port])
test = "Hello, World"
socket.puts test
socket.shutdown 1
response = socket.gets
Everything here is working fine. The ruby client sends a string. The server receives that string and sends a reply.
The Java Version looks like:
String ip = "127.0.0.1";
int port = 6686;
java.net.Socket socket = new java.net.Socket(ip,port);
OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
InputStreamReader in = new InputStreamReader(socket.getInputStream());
String msg = "Hello, world!";
//send
PrintWriter pw = new PrintWriter(out, true);
pw.print(msg);
pw.flush();
// I also tried: out.write(msg); out.flush(); nothing changed
//receive the reply
BufferedReader br = new BufferedReader(in);
char[] buffer = new char[300];
int count = br.read(buffer, 0, 300);
String reply = new String(buffer, 0, count);
System.out.println(reply);
socket.close();
On the other side there is a C++ Server:
string receive(int SocketFD) {
char buffer[SOCKET_BUFFER_SIZE];
int recv_count;
// empty messagestring
string message = "";
// empty buffer
memset(buffer, 0, sizeof(buffer));
while ((recv_count = recv(SocketFD, buffer, sizeof(buffer) - 1, 0)) > 0) {
/*if (recv_count == -1) {
cout << "failed." << endl;
break;
}*/
cout << recv_count << endl;
if (ECHO_SOCKETS) cout << "received: " << buffer << endl;
message.append(buffer);
memset(buffer, 0, sizeof(buffer));
if (ECHO_SOCKETS) cout << "message is now: " << message << endl;
}
return message;
}
The server output from the Java-message is:
13
received: Hello, world!
message is now: Hello, world!
and then nothing happens.
The problem is that:
recv(SocketFD, buffer, sizeof(buffer) - 1, 0)
is catched in an endless loop (or something like that).
If I kill the Java-client process or I type something like:
pw.print(msg);
out.close();
the output on the server side is:
_sending reply: "Request unrecognized/invalid" request="Hello, world!"
send reply success
now close connection
This output is right (except "send reply success"), but in case of adding:
out.close();
the client can't receive the reply of the server. Because the Socket is closed.
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:864)
at MyServer.writeMessage(MyServer.java:56)
at MyServer.test(MyServer.java:42)
at MyServer.main(MyServer.java:30)
Edit
I tried to call pw.flush(); and different delimiters like "\n", "\r", "\r\n" and "\n\r" but the server still thinks there is still something to read. I also tried to use DatagramSockets:
java.net.DatagramSocket dSocket = new java.net.DatagramSocket();
InetAddress address = InetAddress.getByName("localhost");
String msg = "Hello, world!";
byte[] buf = msg.getBytes();
java.net.DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 6686);
But the server can't accept the packet.
Solution
The ruby-client does something like a socket.shutdownOutput(); (ruby: socket.shutdown 1) after the call of puts. I changed the java-client-code:
out.write(msg);
socket.shutdownOutput();
and it works!
As #Charly said: I have to define a "protocol". In my case I'm not allowed to change any communication related code (in the server and the ruby-client) because this functionality is used by a another group of researchers. So I've to modify my java-client in that way, that it does the exact same things at the exact same time as the ruby-client (something like a protocol).
PrintWriter buffer (when autoflush is true) is only flushed by calling println or printf. Calling print may not flush the buffer (Javadoc). Try calling println or use a OutputStreamWriter directly and flush().
Be aware of using the right charset (You can set it up in OutputStreamWriter constructor).
Close the stream respectively flush it in a way like this:
DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
dataOut.writeUTF(s);
dataOut.flush();
while ((recv_count = recv(SocketFD, buffer, sizeof(buffer) - 1, 0)) > 0) {
if (recv_count == -1) {
I don't know what your problem is but this code is certainly nonsense. It is impossible for the inner test ever to succeed.