Solved

C/C++ socket recv() problem.

Posted on 2012-03-26
5
913 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
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 90 total points
Comment Utility
'-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
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 110 total points
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
is there anything open where we could help?

Sara
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now