Winsock confusion

This is a discussion on Winsock confusion within the Windows Programming forums, part of the Platform Specific Boards category; Okely dokely, I'm stumped. ... I followed that tutorial (Johnnie's winsock tut or whatever) and now I've no idea what ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    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,659
    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, 07: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, 11:11 AM
  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

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