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