0

My Java server sends an Integer and a String (to a C client):

   DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

   dos.writeInt(ClientNumber); //send the Integer

   String randomString= getRandomValue(10,20);
   dos.writeUTF(randomString);  //send the String
   String clientString=din.readLine();

The C code for the client that's reading them is:

if( recv( to_server_socket, &reply, sizeof( reply ), MSG_WAITALL ) != sizeof( reply ) )
    {
        printf( "socket read failed");
        exit( -1 );
    }
char buf[50];
int byte_count;
byte_count = recv(to_server_socket, buf, sizeof buf, 0);
printf("recv()'d %d bytes of data in buf\n", byte_count)

Until here, it works fine.

Now, I want to send another String to the Client. So, I tried just adding the line:

dos.writeUTF("blabla");

It's still working and when I tried to get the client to read it, I added:

byte_count2 = recv(to_server_socket, buf2, sizeof buf2, 0);
   printf("recv()'d %d bytes of data in buf\n", byte_count2);

And it doesn't work. The client receives the number and the first String but it doesn't send anything and doesn't receive the "blabla" string. I'm not sure if the problem is in the client or the server.

Can anyone explain to me what I'm doing wrong?

1
  • If you post an expanded version of your previous question, you should delete the old one to avoid duplicates. Commented May 5, 2013 at 13:49

2 Answers 2

1

Try closing your dos(DataOutpuStream) after every write. You may try to check first if flush helps.

Sign up to request clarification or add additional context in comments.

1 Comment

i don't understand you, if i close the dos i will get an error. flush dosent help. But if i change String clientString=din.readLine(); with readUTF() it work perfectly with the java Client . So i think the problem is the Client . I don't understand why if i trie to recive something even the send before it don't work anymore
1

You are mixing your protocols. I suggest you use either binary or text wire format. It's not clear which one you are trying to use.

I suggest text wire format as it is easier to work with in this case. i.e. don't DataInputStream or DataOutputStream as these are for binary formats.

Instead you can use BufferedReader for reading lines of text and PrintWriter for writing lines of text. You can test your server works by connecting to it with telnet i.e. if it doesn't work with telnet, it won't work with C.

Once this is working, get your C client to work as well. BTW You shouldn't assume that one write translates to one read. You are writing a Stream of data, not messages.

Comments

Your Answer

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.