Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C/C++ socket recv() problem.

Posted on 2012-03-26
5
Medium Priority
?
934 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 360 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 35

Accepted Solution

by:
sarabande earned 440 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 35

Expert Comment

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

Sara
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

782 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