Thread: Winsock confusion

  1. #1

    Winsock confusion

    Okely dokely, I'm stumped. ... I followed that tutorial (Johnnie's winsock tut or whatever) and now I've no idea what I'm doing ...

    I understand setting up the connection and closing it (this is a listening app) but how do I get data sent to this? I didn't understand what was going on in the sending recieving bit of the tutorial, it didn't explain it enough IMO ...

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NET_ERR -1
    #define NET_OK   0
    
    bool CheckData ( SOCKET listen, SOCKET client ) {
        
        
        return true;
    }
    
    int ReportError ( LPSTR function, int id ) {
    
        FILE* ErrorLog = fopen ( "errors.log", "a" );
        char buff[256];
        sprintf ( "%s: Returned error id - %i", function, id );
        fputs ( buff, ErrorLog );
        fclose ( ErrorLog );
    }
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow ) {
        
        WORD SockVer;
        WSADATA WsaData;
    
        bool alive = true;
       
        SockVer = MAKEWORD ( 1, 1 );
        WSAStartup ( SockVer, &WsaData );
        
        SOCKET Sock;
        Sock = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );
        if ( Sock == INVALID_SOCKET ) {
            
            ReportError ( "socket()", WSAGetLastError ( ) )
            WSACleanup ( );
            return NET_ERR;
        }
        
        SOCKADDR_IN ServerInfo;
        ServerInfo.sin_family = AF_INET;
        ServerInfo.sin_addr.S_addr = INADDR_ANY;
        ServerInfo.sin_port = htons ( 8888 );
        
        if ( bind ( Sock, (LPSOCKADDR)&ServerInfo, sizeof ( struct sockaddr ) == SOCKET_ERROR ) {
        
            ReportError ( "bind()", WSAGetLastError ( ) );
            WSACleanup ( );
            return NET_ERR;
        }
        
        if ( listen ( Sock, 10 ) == SOCKET_ERROR ) {
        
            ReportError ( "listen()", WSAGetLastError ( ) );
            WSACleanup ( );
            return NET_ERR;
        }
        
        SOCKET Client;
        Client = accept ( Sock, NULL, NULL );
        
        if ( Client == INVALID_SOCKET ) {
            
            ReportError ( "accept()", WSAGetLastError ( ) );
            WSACleanup ( );
            return NET_ERR;
        }
        
        while ( alive ) {
            
            alive = CheckData ( Sock, Client );
        }
        
    }
    That's what I have so far. Now basically what I want to do is while the server thingy hasn't been told to disconnect, check for a message sent. So I need to scan the stream for data I guess which I want to do in the CheckData function.

    Is there a simple way to check for say an int incoming? I just want to do a switch on the number and if it's X do this if Y do that etc ...

    Alternatively a better more in depth tutorial would be much better ...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's another reference you can study...
    http://www.sockets.com/winsock.htm

    gg

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I highly recommend Network Programming for Microsoft Windows, Second Edition by Anthony Jones and Jim Ohmund.

    Kuphryn

  4. #4
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Originally posted by kuphryn
    I highly recommend Network Programming for Microsoft Windows, Second Edition by Anthony Jones and Jim Ohmund.

    Kuphryn
    Ah, me too, excellent read.

    trainee

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. 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
  4. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 10:50 AM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM