There are many website which teach you socket communication in windows using winsock. So if you have come to this site looking for some tutorial I would recommend you to go directly to microsoft website. Then why I am writing this blog you ask - well this is kind of a remembrance post of what I did in socket communication.
I have started developing a basic Http server [If you have followed my previous posts then sorry for one more started project]. In this first step is establishing the socket communication between client and server. So as of now I am able to do the socket communication between server and client. Basically server will wait for a client connection, once connected to client will wait to receive a message from the client. Then displays the received message. It will send a message to client and finally closes the connection. For handling client connections it is creating a new thread each time a new connection is created with client. To test server I have created a basic client. Client will connect to the server, send a message and wait for message from server. Once received, it will display that message and closes the connection.
Steps for socket connection from server side-
- Create a socket
- Bind to the socket
- Wait for client connections i.e. listen to connections
- Accept those connections
- Send/Receive messages from/to that connection
- Shutdown the connection
- Close the socket
Before creating the socket we need to initialize the winsock which we do using WSAStartup function. Then we need to define the address, protocal and socket types for which socket being created. Once these are defined then we will create a socket. Once socket is created, that socket has to be binded to the address and port like 8080 port. After this server will listen for new connections from the clients. Using accept function call we accept the new client connection. Once accepted we can send messages to client as well as receive messages from client. Once done we shutdown the connection, close the socket and cleanup the winsock using WSACleanup.
WSAStartup(MAKEWORD(2, 2), &wsData)
structServerAddrInfo.ai_family = AF_INET;
structServerAddrInfo.ai_socktype = SOCK_STREAM;
structServerAddrInfo.ai_protocol = IPPROTO_TCP;
structServerAddrInfo.ai_flags = AI_PASSIVE;
nReturnValue = getaddrinfo(NULL, "8080", &structServerAddrInfo, &structServerAddrResult);
scListenSocket = socket(structServerAddrResult->ai_family, structServerAddrResult->ai_socktype, structServerAddrResult->ai_protocol);
nReturnValue = bind(scListenSocket, structServerAddrResult->ai_addr, structServerAddrResult->ai_addrlen);
nReturnValue = listen(scListenSocket, SOMAXCONN);
scClientSocket = accept(scListenSocket, NULL, NULL)
nReturnValue = recv(scClientSocket, szRecieveMsg, 512, 0);
nReturnValue = send(scClientSocket, sTestMessage, 35, 0);
nReturnValue = shutdown(scClientSocket, SD_SEND);
closesocket(scClientSocket);
WSACleanup();
Steps for socket connection in client:
- Create socket
- Connect to server
- Send/Receive messages to/from server
- Close socket
Similar to server here also first we need to call WSAStartup function. Once done create a socket that can connect to server using same port and server address. Once connected to server client can send messages to server as well as recieve messages from the server. Once all done, it can close the socket created. Finally call the WSACleanup()
nReturnValue = WSAStartup(MAKEWORD(2, 2), &wsData);
structClientaddr.ai_family = AF_INET;
structClientaddr.ai_protocol = IPPROTO_TCP;
structClientaddr.ai_socktype = SOCK_STREAM;
nReturnValue = getaddrinfo(sServerName, "8080", &structClientaddr, &structServeraddrInfo);
scConnectSocket = socket(structAddrIterator->ai_family, structAddrIterator->ai_socktype, structAddrIterator->ai_protocol);
nReturnValue = connect(scConnectSocket, structAddrIterator->ai_addr, structAddrIterator->ai_addrlen);
nReturnValue = send(scConnectSocket, sTestMessage, strnlen_s(sTestMessage, 33), 0);
nReturnValue = recv(scConnectSocket, szRecieveMessage, 512, 0);
closesocket(scConnectSocket);
WSACleanup();
I agree, I have not explained it in deep, may be sometime later. Code of this can be found at gitbhub