Thread: Winsock Help

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    18

    Winsock Help

    I am trying to code a basic command line server, basically so I can telnet to it and send data and the server will print it out.

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock2.h>
    
    
    int main()
    
    {
    int servSock;
    int clntSock;
    struct sockaddr_in echoServAddr;
    struct sockaddr_in echoClntAddr;
    unsigned short echoServPort;
    unsigned int clntLen;
    WSADATA wsaData;
    char echoBuffer[100];
    int recvMsgSize;
    
    
    WSAStartup(MAKEWORD(2, 0), &wsaData);
    servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
    
    echoServAddr.sin_family = AF_INET;
    echoServAddr.sin_addr.s_addr = htons(INADDR_ANY);
    echoServAddr.sin_port = htons(21);
    
    
    bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr));
    listen(servSock, 5);
    clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen);
    
    while (1)
    {
    sleep(2000);
    
    recv(servSock, echoBuffer, 0, 0);
    printf("%s", echoBuffer);
    
    }
    
    printf("%s", echoBuffer);
    return 0;
    
    }

    I want to print out all the data received, when I use a loop though it like prints out things in the buffer that haven't been sent "ZZZZZZZ"

    So basically can anybody help me.
    Sorry for not giving much information.

  2. #2
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    look at the MSDN page for recv. you're not specifying the size of the buffer.
    {RTFM, KISS}

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    in addition to what spoon_ said in his post, you need to recv() on "clntSock" cause thats the connected socket returned by accpet().

    also, data received by recv() is not terminated with null. So using printf() on the the received string without null terminator can be dangerous as it may cause segmentation fault. So make sure you null terminate the echoBuffer with null after receive returns like this.

    Code:
    int rc;
    
    clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen);
    
    while (1)
    {
        
        rc = recv(clntSock, echoBuffer, sizeof(echoBuffer) - 1, 0); /* Always check return value of recv() */
      
        if (rc == 0) { printf("Connection closed\n"); exit(0);}
        if (rc == -1) {printf("Error in recv\n"); exit(0);}
        
        echoBuffer[rc] = '\0';    /* null terminated the received buffer */
        printf("%s", echoBuffer);
    
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    18
    Thank you for your help.

    nkhambal when I use that code it tells me, that there is an error with the winsock.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    What errors?

    Check the return value of accept() ("clntSock") before using it in recv().

    Code:
    clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen);
    
    if (clntSock < 0 )
    {
       printf("Error in accpet\n");
       exit(1);
    }
    
    /* recv here if everything is okey */
    Last edited by nkhambal; 09-03-2005 at 01:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM