Connection-oriented socket communication in Windows

Source: Internet
Author: User

Server code:

# Include <stdio. h>
# Include <Winsock2.h>
# Include <windows. h>
Dword winapi ReceiveData (LPVOID param );
Void main ()
{
WSADATA wsaData;
Int ret = WSAStartup (MAKEWORD (2, 2), & wsaData );
If (ret ){
Printf ("failed to initialize the windsock 2.2 network library \ n ");
Return;
}
SOCKADDR_IN servieAddr;
ServieAddr. sin_family = AF_INET;
ServieAddr. sin_addr.s_addr = inet_addr ("192.168.0.115 ");
ServieAddr. sin_port = htons (9000 );
SOCKET listenSocket;
ListenSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
If (INVALID_SOCKET = listenSocket ){
Printf ("initialize socket \ n ");
WSACleanup ();
Return;
}
Ret = BIND (listensocket, (sockaddr *) & servieaddr, sizeof (servieaddr ));
If (ret = socket_error ){
Printf ("failed to bind listening socket err = % d \ n", wsagetlasterror ());
Closesocket (listensocket );
Wsacleanup ();
Return;
}
Ret = listen (listensocket, 1 );
If (ret = socket_error ){
Printf ("failed err = \ n", wsagetlasterror ());
Closesocket (listensocket );
Wsacleanup ();
Return;
}
Sockaddr_in clientaddr;
Socket clientsocket;
Int clientlen = sizeof (clientaddr );
// Enable a thread to make listensocket wait for a connection
While (1 ){
Clientsocket = accept (listensocket, (sockaddr *) & clientaddr, & clientlen );
If (clientsocket = invalid_socket ){
Printf ("failed to set accept ret = % d \ n", wsagetlasterror ());
Break;
}
Else {
// Receives received data from a new connection. Enable a thread to receive data.
Printf ("receive a client connection \ n ");
DWORD threadID;
HANDLE hThreadID;
HThreadID = CreateThread (0, 0, ReceiveData, (void *) & clientSocket, 0, & threadID );
If (hThreadID = NULL ){
//
Printf ("failed to receive a client connection and create receiving thread ");
}
}
}
Closesocket (listenSocket );
If (WSACleanup () = SOCKET_ERROR )){
Printf ("failed to clean up the windsock 2.2 network library \ n ");
Return;
}
Return;
}

Dword winapi ReceiveData (LPVOID param ){
Char recvBuf [255];
Memset (recvBuf, 0, 10 );
SOCKET clientSocket = * (SOCKET *) param;
Int ret = recv (clientSocket, recvBuf, 255, 0 );
If (ret> 0 ){
Printf ("received data: % s \ n", recvBuf );
}
Closesocket (clientSocket );
Return 0;
}

Client:

WSADATA wsaData;
Ret = WSAStartup (MAKEWORD (2, 2), & wsaData );
If (ret ){
Return;
}
SOCKADDR_IN connServerAddr;
ConnServerAddr. sin_family = AF_INET;
ConnServerAddr. sin_addr.s_addr = inet_addr ("192.168.0.115 ");
ConnServerAddr. sin_port = htons (9000 );
SOCKET connSocket;
ConnSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
If (INVALID_SOCKET = connSocket ){
Closesocket (connSocket );
WSACleanup ();
Return;
}
Ret = connect (connSocket, (SOCKADDR *) & connServerAddr, sizeof (connServerAddr ));
If (ret = SOCKET_ERROR ){
Closesocket (connsocket );
Wsacleanup ();
Return;
}
// Connection successful
Char * Buf = "this is test client .....................";
Ret = Send (connsocket, Buf, strlen (BUF), 0 );
If (ret = socket_error ){
Closesocket (connsocket );
Wsacleanup ();
Return;
}
Closesocket (connsocket );
Wsacleanup ();

Next, how to define the communication protocols between the two parties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.