Java client and C ++ server send and receive via TCP Socket

I have a C ++ server and two clients (ruby and java). Everything works on a 64-bit Linux machine (java 1.7.0_17) The ruby ​​client works completely, but the java version creates problems.

In Java, I tried sending String from the client to the server. In fact, the server received the entire string, but the server believes that there is something more.

The ruby ​​client looks something like this:

socket = TCPSocket.open(@options[:host],@options[:port])
test = "Hello, World"
socket.puts test
socket.shutdown 1
response = socket.gets

Everything here works great. The ruby ​​client sends a string. The server receives this line and sends a response.

The Java version looks like this:

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 hand, 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;
}

Server output from Java message:

13
received: Hello, world!
message is now: Hello, world!

and then nothing happens. The problem is that:

recv(SocketFD, buffer, sizeof(buffer) - 1, 0)

gets into an infinite loop (or something like that). If I kill the Java client process or type something like:

pw.print(msg);
out.close();

server side output:

_sending reply: "Request unrecognized/invalid" request="Hello, world!"
send reply success
now close connection

( "send reply success" ), :

out.close();

. Socket .

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

pw.flush(); , "\n", "\ r", "\ r\n" "\n\r", , . 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);

.

ruby-client - socket.shutdownOutput(); (ruby: socket.shutdown 1) puts. java-client:

out.write(msg);
socket.shutdownOutput();

!

@Charly : "". - , ( ruby-client), . java- , ruby- (- ).

+5
3

PrintWriter ( autoflush true) println printf. (Javadoc). println OutputStreamWriter flush(). ( OutputStreamWriter).

+2

, , :

DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
dataOut.writeUTF(s);
dataOut.flush();
0
while ((recv_count = recv(SocketFD, buffer, sizeof(buffer) - 1, 0)) > 0) {
      if (recv_count == -1) {

I don't know what the problem is, but this code is definitely nonsense. For an internal test it is impossible to succeed.

0
source

All Articles