Thread: Simple? winsock client - server problem

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Angry Simple? winsock client - server problem

    I've created a simple winsock program, where i send a string from the client to the server. The problem is that the server enters an infinite loop, and receives the same string over and over again.. What's the error here(The functions are standard Winsock, th "lp"-prefix is because i'm linking explicit)?

    Client:
    Code:
    do{
      cout<<"Enter data to send:\n";
      memset(buf, 0, 100);
      cin.getline(buf,100, '\n');
      if(send(theSocket, buf, strlen(buf), MSG_DONTROUTE) == SOCKET_ERROR){
        errHand("Error at send()");
      }else{
        recv(theSocket, buf, 100, MSG_DONTROUTE);
        cout<<"Status: "<<buf<<"\n";
      }
    }while(buf[0] != 'q');
    Server:
    Code:
    // Send/receive from the client:
    while(buf[0] != 'q'){
      memset(buf, 0, 100);
      if((iBytes=lpRecv(theClient, buf, 100, MSG_PEEK)) == SOCKET_ERROR)
        SendMessage(dat1->list1, LB_ADDSTRING, 0, (long)"Error at recv()");
      else if(iBytes>0){
        buf[iBytes]='\0';
        sprintf(buf, "n:%i: %s", iBytes, buf);
        SendMessage(dat1->list1, LB_ADDSTRING, 0, (long)buf);
        strcpy(buf, "o");
        lpSend(theClient, buf, strlen(buf), MSG_DONTROUTE);
      }else
        buf[0]='\0';
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>iBytes=lpRecv(theClient, buf, 100, MSG_PEEK)
    Why are you peeking at the data?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    It shouldn't be possible not to see such an obvious error..!

    Thanks, Hammer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Client - Server problems (Async Sockets )
    By Cpp_Noob in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-19-2009, 07:12 AM
  2. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 04:01 AM
  3. Multi-Threaded Server Help
    By jonswits20 in forum C# Programming
    Replies: 5
    Last Post: 04-17-2007, 11:05 PM
  4. client -server problem
    By hegdeshashi in forum C Programming
    Replies: 1
    Last Post: 06-13-2006, 11:13 PM
  5. Replies: 7
    Last Post: 04-03-2003, 11:53 PM