problems with TCP/IP programming

This is a discussion on problems with TCP/IP programming within the Networking/Device Communication forums, part of the General Programming Boards category; I have a simple server programm with a while loop like this one Code: while(recv(sock, &i, sizeof(int), 0) > 0) ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    problems with TCP/IP programming

    I have a simple server programm with a while loop like this one



    Code:
    while(recv(sock, &i, sizeof(int), 0) > 0)
    {
        cnt++;
    
        i = i / 10;
    
        send(sock, &i, sizeof(int), MSG_NOSIGNAL);
    
        cout << "\nReceived number is: " << i;
        cout << "\nRequest # " << cnt << "\n";
    }
    The value i is always sent back and correctly received by my client. The client itself only sends an int value once, gets it back, prints it, and then exits. It does this without presenting a single problem.

    What happens is: I start the server, and then the client. The first time around the client functions normally, but the server does not increase cnt, nor prints the statements inside the loop to cout. I can't understand because the results I get from the client make it obvious that these lines do execute:

    Code:
      i = i / 10;
    
      send(sock, &i, sizeof(int), MSG_NOSIGNAL);
    but the other ones apparently don't, since the second time I execute the client (without having turned off the server), cnt is equal to 1, and not too as it should be. From the second iteration onwards everything works fine, as if the first one had not ocurred at all.

    Can anyone help me out? Thanks in advance.
    Last edited by jalnewbie; 06-29-2007 at 01:51 PM. Reason: I messed up in one of the lines of code

  2. #2
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Can I see more of the server code? I'm a little rusty, but you have to make the server listen, and accept the new connection each time the client connects, so maybe something is wrong there?
    Programming Your Mom. http://www.dandongs.com/

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Code:
    #include <iostream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>
    
    #define PUERTO 25500
    
    bool establecer_socket(int &sock, sockaddr_in &dir);
    
    int mandar(int sock, int *buf, int longitud)
    {
      return (send(sock, buf, longitud, MSG_NOSIGNAL));
    }
    
    int recibir(int sock, int *buf, int longitud)
    {
      return (recv(sock, buf, longitud, 0));
    }
    	
    int main()
    {
      using namespace std;
    	
      int sock_handle, dimension, nueva_sock;
      sockaddr_in direccion;
      int i, cnt;
      
      cnt = 0;        // contador para el numero de peticiones que resuelva
      
      // preparar a la socket para ser usada antes de bind
      
      if(establecer_socket(sock_handle, direccion))
      {
        cout << "ESCUCHANDO!...\n";
      } 
      else
      {
        cout << "ERROR EN LA CONEXION...";
        exit(1);
      }
     
      listen(sock_handle, 5);
      
      while(true)
      {
        dimension = sizeof(direccion);
    
        nueva_sock = accept(sock_handle, (sockaddr *) &direccion, (socklen_t *) &dimension);
        
        if(nueva_sock < 0)
        {
          exit(1);
        }
    
        while(recibir(nueva_sock, &i, sizeof(i)) > 0)
        {
          using namespace std;
    	    
          cnt = cnt + 1;
    	   
          i = i / 10;
    
          cout << "\nNumero rebotado: " << i;
          cout << "  # de solicitud: " << cnt;
          
          mandar(nueva_sock, &i, sizeof(i));	
        }
        
        close(nueva_sock);
      } 
      
      return 0;
    }
    
    bool establecer_socket(int &sock, sockaddr_in &dir)
    {
      bool result = true;
      int opt = 1;        // opciones que se pasan con setsockopt
      
      result = ( (sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0);
    
      if(result)
      {
        result = ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &opt, sizeof(opt)) == 0 );
        
        memset(&dir, 0, sizeof(sockaddr_in));
        
        dir.sin_family = AF_INET;
        dir.sin_port = htons(25500);
        dir.sin_addr.s_addr = INADDR_ANY;
      }
        
      if(result)
      {
        result = (bind(sock, (sockaddr *) &dir, sizeof(dir)) == 0); 
      }
     
      return result;
    }
    Here is the server code. As I had already said, it is fairly short. Let me make the comment once again, that the client itself gets replies from the server every time. Hence I know they are communicating so the problem lies elsewhere. By the way, sorry about the Spanish, I didn't think I'd have to post the code on a forum to seek help so some of the parts are like that, though I thrust it is by no means critical.
    Last edited by jalnewbie; 06-29-2007 at 04:36 PM. Reason: there is some spanish in there

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Thanks anyway. It was a problem with cout.flush(), once I used it the problem went away. Anyway please let me know how I can improve upon the way I ask for help so that next time I don't have to answer my own question. I'm just a newbie so it really would help to get the assistance of those who are better prepared than me. Then again, on two past ocassions in which I sought help little was provided, so that I respectfully ask you to tell me all that is wrong in the way I pose questions. I do have a tendency to run into weird bugs.

  5. #5
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    You actually posed your question rather well. giving a decent section of code, along with a clear description of the problem. In this case, it is likely that not many are experienced with sockets, and of those that are, even a smaller percentage are experienced with working with sockets under UNIX. Sometimes it will take several hours, or even a day or two, for people with the right expertise to notice a problem like this.

    As far as your code goes, it looks nice. Just a few small things to note:
    1. #define PEURTO 25500 should be const int PEURTO = 25500. Constants are preferred over DEFINEs in C++ because they offer type safety (plus in more complicated circumstances macros can cause issues).
    2. You only need "using namespace std;" once. It's pretty standard to put it after your includes.
    3. Instead of using cout.flush(), use "endl" where you put "\n":
    Code:
    cout << endl << "Numero rebotado: " << i;
    It inserts a newline and forces a flush.
    4. Use cnt++ instead of cnt = cnt + 1. It is more concise.

    Sorry to nitpick. I just thought I'd point out some small things you could change to keep your code cleaner. Besides that, it looks great.
    Programming Your Mom. http://www.dandongs.com/

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Moved to Networking/Device Communication.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. Problems
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 02-28-2002, 02:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21