|
-
June 16th, 2003, 09:34 PM
#1
Member
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
-
June 16th, 2003, 10:03 PM
#2
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...

-
June 16th, 2003, 10:11 PM
#3
Member
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.
-
June 16th, 2003, 10:13 PM
#4
much better, thanks for clearing that up!
yeah, I\'m gonna need that by friday...

-
June 16th, 2003, 11:27 PM
#5
Real programmers use Winsock, not MFC nonsense 
Good post though, for all those unreal programmers out there
-
June 17th, 2003, 12:26 AM
#6
Member
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
-
Forum Rules
|
|