Windows Socket Establishment
Results 1 to 6 of 6

Thread: Windows Socket Establishment

  1. #1

    Post Windows Socket Establishment

    Tutorial: Windows Socket Establishment

    This tutorial is primarly geared towards programmer who wishes to client/server application. The reason one might create such a program would be to establish a connection with a remote host to exchange files or 'program datagrams'. Also, it's very useful to learn just exactly how windows creates a socket connection.

    Step One

    Create skeleton WIN32 (MFC support) application using VC++. This allows us to gain library access to the CSocket object as well as many API functions.

    Step Two

    Create a global CSocket object. Make sure to include the proper headers. Next, 'create' the socket by first binding a 'port' to the object :

    AfxSocketInit ();
    CSocket * p_sock = new CSocket;
    p_sock->bind( 8880, ip ); //first parameter is the port, second is IP naturally
    p_sock->create( *remote port* , *Socket Type* SOCK_STREAM , *Remote IP*) ;

    Step Three

    Check for creation failure. This can be done with a UINT struct but is beyond the scope of the tutorial. Next, connect to the remote host with the port that the application (or service) specifies. Create buffer. After connection, either send or receive datagrams or TCP packets depending on with type of stream was chosen:

    void * buffer;

    p_sock->Connect( *IP string*, *Remote Port* );
    p_sock->Send ( *buffer* , sizeof(buffer) , *flags* = 0 );
    p_sock->Receive(*empty buffer* , sizeof(incoming buff), MSG_PEEK);

    Step 4
    Close Socket :

    p_sock->Close();

    Final Word

    Of course, This is extremely basic type of app, but could be built upon to customize user needs. Also, I did'nt get into CSocketFile and how it is serialized and sent over the stream. Maybe next time. This type of app can connect to any app which you have the port for and stream type. Use at your hearts desire.

    Scatman

  2. #2
    Senior Member tampabay420's Avatar
    Join Date
    Aug 2002
    Posts
    953
    would we want to put the recieve in a loop?
    Code:
    while (recieving==true) {
    p_sock->Receive(*empty buffer* , sizeof(incoming buff), MSG_PEEK); 
    }
    yeah, I\'m gonna need that by friday...

  3. #3
    No,
    In this case, you are going into server mode and would want to enable 'listen' mode. This automatically puts you in a loop and is open for remote requests.

    while (server_up == true) {
    if (0 != p_sock->Listen()) //CALL ISSUED
    p_sock->Recieve( *parameters*) ; //Acknowledge Call , Recieve datagrams
    }

    If listen gets a incoming 'call', is sends a message indicating just that. Proper message maps should be implemented. Is that ok?

    Scatman
    If the scatman can do it so can you.

  4. #4
    Senior Member tampabay420's Avatar
    Join Date
    Aug 2002
    Posts
    953
    much better, thanks for clearing that up!
    yeah, I\'m gonna need that by friday...

  5. #5
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Real programmers use Winsock, not MFC nonsense

    Good post though, for all those unreal programmers out there

  6. #6
    Technically that's not true,
    CSocket is a wrapper class or the WINSOCK api. Pretty unreal huh?

    Scatman
    If the scatman can do it so can you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •