Thread: Cannot read incoming data from socket

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    Cannot read incoming data from socket

    Hi

    When I run the executable, it first receives the data and displays the data itself and the length of data in bytes. But, when I send another package to the server, it does not display anything!

    Here is a prt from server code
    Code:
       
    char buffer[128];
    .....
    .....
    while(1)
       {            
        if ((newsock=accept( sock,(sockaddr *)&remoteAddr,NULL))==SOCKET_ERROR)
         // some error checking
           int len;
           len=recv(newsock,buffer,sizeof(buffer),0);
         // some error checking
           
           cout << "Received data : "<< buffer << ". " << len << " bytes length." << endl;
           
       }
    I think, what this code should does is to display the incoming data and return back to begining of while loop.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    try shutting down the incoming connection before sending the response:

    shutdown(sock, SD_RECEIVE);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Why should I shut down the connection. "sock" is created out of the while loop and used for listening the connection.

    The client program has a code like this
    Code:
      while(1) {
       cin >> buffer;
       len=send(serverSocket,buffer,strlen(buffer),0);
         cout << len << " bytes sent\n";
    
       }
    When sending first data, everything is ok! After that the server will not display any message that says something about arrival of any data!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Is this a blocking or nonblocking socket? If it's a non blocking socket then there might not be any data available at the time you called recv and the next time tru the loop newsock is reassigned by accept (memory leak).

    If you have a blocking socket then you might only recv the first few bytes depending on how much was available at the time recv was called.

    You should call closesocket on newsock before you reasign it and you need to keep calling recv untill the amount of data you expected is received or you receive some deliminator.
    Last edited by Quantum1024; 03-03-2006 at 09:46 AM.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    The socket is blocking and te data I sent is transmitted successfully (it is 7-8 bytes long).

    I tried closesocket() in the server code at the end of the while loop(). The result is the same, when I sent any data with client, it says "blabla bytes data sent", and the server only at first time displays "Received data : data itself comes here. blabla bytes length". But when i try to sent another data, the server does not receive any data.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Why should I shut down the connection. "sock" is created out of the while loop and used for listening the connection.

    the traditional form of http treats each send/recv call as a single session - a new connection is established for each session. you should be able to alter this behavior by sending the server a header indicating that the connection should be kept open - I've never used this feature so I can't say what exactly it would be. refer to the http rfc's to be sure.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    I write a while(1) loop for the recv() call. That works!

    Here is ths complete source code, you can check it. (Written with Dev-C++)
    http://www4.gantep.edu.tr/~fni18444/trial.zip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplexing read socket and stdin
    By redcameron in forum Networking/Device Communication
    Replies: 8
    Last Post: 04-06-2009, 05:34 PM
  2. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  3. Read data from my .txt?
    By Mech0z in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 09:55 AM
  4. read data in files
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-18-2002, 11:45 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM