Solved

C/C++ socket recv() problem.

Posted on 2012-03-26
5
926 Views
Last Modified: 2012-06-27
I have a small C++ server and a java client. I send data from the client to the server. The recv() function of the server side does not receive the the data. I receive the size -1. But the otherway about works well (i.e. from server to the client). Here is my scaled down code.

Server.....
Sever users the following read() to read data from the socket...
void read (   )  
    {
        char buffer [ 100 ];
        memset ( buffer, 0, 100 );

        int receiveSize = ::recv ( mSocket, buffer, 100, 0 );

        std::cout << "Received Size : " << receiveSize << std::endl;
}

//The recvSize is -1 above.

Client ......
My Client uses the following simple java function to write the data to the socket:
public void writeValue(Socket requestSocket)
 {
        try{
               DataOutputStream dOut = new DataOutputStream(requestSocket.getOutputStream());
 
              System.out.println("writing a char...");
              dOut.writeChar('p');

              dOut.close();
        }
        catch  (Exception exp)
        {
            exp.printStackTrace();
        }
    }


Again:
1. Connection is perfer
2. Data sending from server to client is perfect
2. Problem is server receiving data from the client.

Can someone throw some light? BTW I work in a UNIX environment.
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 90 total points
ID: 37768683
'-1' indicates an error condition (see http://linux.die.net/man/2/recv) - what is 'errno' in that case, i.e.

#include <errno.h>

void read (   )  
    {
        char buffer [ 100 ];
        memset ( buffer, 0, 100 );

        int receiveSize = ::recv ( mSocket, buffer, 100, 0 );

        std::cout << "Received Size : " << receiveSize << std::endl;

        if (-1 == receiveSize) std::cout << "Error: " << errno << std::endl;
}

Open in new window

0
 
LVL 12

Expert Comment

by:satsumo
ID: 37769280
Following on from jkr, the problem is likely to be somewhere else in the code.  The socket setup, connecting, binding and listening process.  Can you post some more code?
0
 
LVL 34

Accepted Solution

by:
sarabande earned 110 total points
ID: 37770638
you know that the server needs a second socket for each client accepted?

means the server would call accept to get a new socket handle and then would use that socket handle for doing communication with the client. you often would do that in a separate thread per client cause otherwise the server was blocked when calling recv. you could set the socket to non-blocking but then the -1 return was ok if errno is EWOULDBLOCK. or you need to do a select with timeout for the client socket and only call recv if select returns 1.

Sara
0
 

Author Closing Comment

by:prain
ID: 37771125
Actually I found the problem. I was setting nonBlocking as TRUE. But then I wrote a small C++ server. That threw me into a dilema because the C++ server reads the data from the client even if the nonBlocking is TRUE or FALSE. I cannot figure out why the data is not read (and returns -1)only when a Java Server is running.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 37771172
is there anything open where we could help?

Sara
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question