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
Solved

C/C++ socket recv() problem.

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

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 33

Expert Comment

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

Sara
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

808 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