Solved

C/C++ socket recv() problem.

Posted on 2012-03-26
5
927 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

617 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