Sample - Simple Client/Server Connection Using Sockets: Notes Code
#include "winsock2.h" // reference ws2_32.lib in project settings #include "iostream.h" // cout #define IP_ADDRESS "127.0.0.1" #define PORT_NUMBER 10000 #define MAX_PENDING_CONNECTS 3 void main() { /****************************************************************************** Initialization of WinSock Library *******************************************************************************/ WSADATA wsaData; WSAStartup( MAKEWORD( 2, 0), &wsaData ); /****************************************************************************** 1.a: Opening a socket *******************************************************************************/ SOCKET st = socket( PF_INET, SOCK_STREAM, 0 ); if ( st == INVALID_SOCKET ) { cout << "Invalid Socket:" << endl; cout << WSAGetLastError() << endl; } /******************************************************************************* 1.b: Naming a socket ********************************************************************************/ u_long lBinaryIP = inet_addr( IP_ADDRESS ); u_short iNetworkPort = htons( PORT_NUMBER ); SOCKADDR_IN sinServer; sinServer.sin_family = AF_INET; //PF_NET for WinSock 1.1 only! sinServer.sin_addr.s_addr = lBinaryIP; sinServer.sin_port = iNetworkPort; /******************************************************************************* 1.c: Binding name with a socket (server only) ********************************************************************************/ int iStatus = bind( st, (LPSOCKADDR)&sinServer, sizeof(SOCKADDR_IN) ); if( iStatus == SOCKET_ERROR ) { cout << "Binding Error:" << endl; cout << WSAGetLastError() << endl; } else { cout << "Binding Passed ..." << endl; } /******************************************************************************* 2: Turning Socket into Listening State (server-specific) ********************************************************************************/ iStatus = listen( st, MAX_PENDING_CONNECTS ); if( iStatus == SOCKET_ERROR ) { cout << "Listen Error:" << endl; cout << WSAGetLastError() << endl; } else { cout << "Listen Passed ..." << endl; } /******************************************************************************* Between steps 2 and 3 a client connect to a server and server: 3: Accepts Incoming Connection ********************************************************************************/ SOCKET stAssociated; // second parameter is not NULL if we need info on newly created socket // (as third paramer - pointer to length of sockaddr_in structure) stAssociated = accept( st /* a socket that listens */, NULL, NULL ); if( stAssociated == INVALID_SOCKET ) { cout << "Accept Error" << endl; cout << WSAGetLastError() << endl; } else { cout << "Accept Passed ..." << endl; } /******************************************************************************* Now client and server are both recognized each other and can exchange message. Here server sends a message to client (client makes recv()): 4: Server Sends a message to client ********************************************************************************/ char szSendString[100]; strcpy( szSendString, "This message is sent by the Server ..."); if( send( stAssociated /*must be a socket created with accept*/, szSendString, strlen(szSendString), 0 /*recommended*/ ) == SOCKET_ERROR ) { cout << "Send Error:" << endl; cout << WSAGetLastError() << endl; } else { cout << "Message Send ..." << endl; } Sleep(100000); //to prevent closing of a connection WSACleanup(); }
2002, Netston Consulting