Server Socket Code

// Module: EchoServer.cpp // Author: Rajinder Yadav // Date: Sept 5, 2007 // #include "stdafx.h" #include <iostream> #include <process.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // Initialize WinSock2.2 DLL // low word = major, highword = minor WSADATA wsaData = {0}; WORD wVer = MAKEWORD(2,2); int nRet = WSAStartup( wVer, &wsaData ); if( nRet == SOCKET_ERROR ) { // WSAGetLastError() cout << "Failed to init Winsock library" << endl; return -1; } cout << "Starting server" << endl; // name a socket WORD WSAEvent = 0; WORD WSAErr = 0; // open a socket // // for the server we do not want to specify a network address // we should always use INADDR_ANY to allow the protocal stack // to assign a local IP address SOCKET hSock = {0}; hSock = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ); if( hSock == INVALID_SOCKET ) { cout << "Invalid socket, failed to create socket" << endl; return -1; } Rajinder Yadav Copyright © 2007 www.devmentor.org // name socket sockaddr_in saListen = {0}; saListen.sin_family = PF_INET; saListen.sin_port = htons( 10000 ); saListen.sin_addr.s_addr = htonl( INADDR_ANY ); // bind socket's name nRet = bind( hSock, (sockaddr*)&saListen, sizeof(sockaddr) ); if( nRet == SOCKET_ERROR ) { cout << "Failed to bind socket" << endl; //shutdown( hSock ); closesocket( hSock ); return -1; } while( true ) { cout << "Listening for connections" << endl; // listen nRet = listen( hSock, 5 ); // connection backlog queue set to 10 if( nRet == SOCKET_ERROR ) { int nErr = WSAGetLastError(); if( nErr == WSAECONNREFUSED ) { cout << "Failed to listen, connection refused" << endl; }

0 ). // close client connection closesocket( hClient ). nLeft. if( nData == SOCKET_ERROR ) { cout << "Error receiving data" << endl. . closesocket( hSock ). } // clear data buffer memset( &wzRec. } nLeft -= nData. // echo data back to client iPos = 0. connection failed" << endl.else { cout << "Call to listen failed" << endl. } nLeft -= nData. memset( &wzRec. } while( nLeft > 0 ). if( nData == SOCKET_ERROR ) { cout << "Error sending data" << endl. 0. sizeof( wzRec ) ). &wzRec[iPos]. iPos += nData. } closesocket( hSock ). } // loop cout << "Shutting down the server" << endl. int iPos = 0. nLeft = 512. hClient = 0. (sockaddr*)&saClient. 0 ). } cout << "Connection estabilished" << endl. iPos += nData. break. sizeof( wzRec ) ). int nSALen = sizeof( sockaddr ). &nSALen ). return -1.org // process data char wzRec[512] = {0}. int nLeft = 512. cout << "Data Recieved: " << wzRec << endl. if( hClient == INVALID_SOCKET ) { cout << "Invalid client socket. int nData = 0. break. Rajinder Yadav Copyright © 2007 www. &wzRec[iPos]. Rajinder Yadav Copyright © 2007 www. } // connect sockaddr_in saClient = {0}. } while( nLeft > 0 ). SOCKET hClient = accept( hSock.org // close server socket nRet = closesocket( hSock ). // perform a lowercase comparison if( _stricmp( wzRec.devmentor. nLeft.devmentor. do { nData = recv( hClient. return -1. "!shutdown" ) == 0 ) { break. 0. do { nData = send( hClient.

} cout << "Server is offline" << endl. } // Release WinSock DLL nRet = WSACleanup(). return -1. if( nRet == SOCKET_ERROR ) { cout << "Error cleaning up Winsock Library" << endl.hSock = 0.cpp . } Client Socket Code // Module: EchoClient. return 0. if( nRet == SOCKET_ERROR ) { cout << "Error failed to close socket" << endl.

SOCKET hServer = {0}. WORD wVer = MAKEWORD(2. // open a socket // // for the server we do not want to specify a network address // we should always use INADDR_ANY to allow the protocal stack // to assign a local IP address hServer = socket( AF_INET. 2007 // #include "stdafx. if( nData == SOCKET_ERROR ) { cout << "Error sending data" << endl. int nData = 0. (sockaddr*)&saServer. argv[1] ). if( nRet == SOCKET_ERROR ) { cout << "Failed to init Winsock library" << endl. Rajinder Yadav Copyright © 2007 www. . 1024.org // connect nRet = connect( hServer.2 DLL // low word = major. return -1. closesocket( hServer ). } cout << "Opening connection to server" << endl. saServer. IPPROTO_IP ).sin_addr. int nLeft = 512.0.2). saServer. &wzRec[iPos]. saServer. sizeof( sockaddr ) ). highword = minor WSADATA wsaData = {0}. return -1. } do { nData = send( hServer. if( nRet == SOCKET_ERROR ) { cout << "Connection to server failed" << endl. if( argc == 2 ) { // copy input string from command argument strcpy_s( wzRec.1" ). if( hServer == INVALID_SOCKET ) { cout << "Invalid socket. SOCK_STREAM. // process data char wzRec[1024] = "Hello from client!!!". nLeft.0. return -1. int main(int argc. break.h" #include <iostream> using namespace std. char* argv[]) { // Initialize WinSock2. cout << "Sending data to server" << endl. } // name a socket sockaddr_in saServer = {0}. WORD WSAErr = 0.sin_port = htons( 10000 ).devmentor.sin_family = PF_INET. 0 ). int nRet = WSAStartup( wVer. failed to create socket" << endl.s_addr = inet_addr( "127. &wsaData ). } cout << "Connected to server" << endl. WORD WSAEvent = 0.// Author: Rajinder Yadav // Date: Sept 5. int iPos = 0.

} nLeft -= nData. nLeft = 512.devmentor.} nLeft -= nData. if( nRet == SOCKET_ERROR ) { // WSAGetLastError() cout << "Error trying to perform shutdown on socket" << endl. do { nData = recv( hServer. iPos += nData. return -1. hServer = 0. break. } . if( nData == SOCKET_ERROR ) { cout << "Error receiving data" << endl. 0 ). } // Release WinSock DLL nRet = WSACleanup(). // clear data buffer memset( &wzRec. } while( nLeft > 0 ). nLeft. 0. iPos += nData.org // shutdown socket nRet = shutdown( hServer. Rajinder Yadav Copyright © 2007 www. } cout << "Data sent successfully" << endl. } while( nLeft > 0 ). cout << "Closing connection" << endl. if( nRet == SOCKET_ERROR ) { cout << "Error failed to close socket" << endl. &wzRec[iPos]. cout << "Data Echoed: " << wzRec << endl. sizeof( wzRec ) ). if( nRet == SOCKET_ERROR ) { cout << "Error cleaning up Winsock Library" << endl. return 0. iPos = 0. SD_BOTH ). } // close server socket nRet = closesocket( hServer ). return -1.

Sign up to vote on this title
UsefulNot useful