socket programming in devc compiler

Notes on socket programming under devic

1. You need to check the following command in tool T - > compile option C - > compiler, and then enter - lwsock32 in the input box. Click OK to close the window. (otherwise, there will be many undefined compilation errors when running the program). The steps are as follows:


2. You do not need to add the "ා" pragma comment(lib, "ws2_. Lib") statement in the code to run the source file normally!

My simple socket learning demo

server.c

#include<stdio.h>
#include<winsock.h>
int main()
{
	//=======================Defining variables=======================//
	char sendBuffer[100];									//Buffer to send data
	char receiveBuffer[100];								//Buffer to receive data
	int sendLength;											//Length of data sent
	int receiveLength;										//Length of received data
	int length;												//Indicates the size of SOCKADDR
	 
	SOCKET socketServer;									//Server Socket 
	SOCKET socketClient;									//Client's connection socket
	
	SOCKADDR_IN serverAddress;								//server address
	SOCKADDR_IN	clientAddress;								//Client address
	
	WORD wVersonRequested;									//Word: unsigned short
	WSADATA wsaData;										//Library version information structure
	int error;												//Expressing errors
	
	//====================Initialize socket library====================//
	wVersonRequested=MAKEWORD(2,2);							//Define version type, combine two bytes into one word, with low byte in front and high byte in back
	error=WSAStartup(wVersonRequested,&wsaData);			//Load the socket library and initialize the ws2-32.dll dynamic link library 
	if(error!=0)
	{
		printf("Failed to load socket library!\n");
		return 0;											//Program end 
	}
	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
	{
		WSACleanup();										//Non conformity 
		return 0;											//Program end 
	}
	
	//=====================Set connection address=====================//
	serverAddress.sin_family=AF_INET;						//Address family, for AF? INET, note that only it is not network byte order
	serverAddress.sin_addr.S_un.S_addr=htonl(INADDR_ANY);	//Host address
	serverAddress.sin_port=htons(5050);						//Port number
	
	//===================Create server socket===================// 
	socketServer=socket(AF_INET,SOCK_STREAM,0);				//AF? INET for address family, sock? Stream for streaming socket TCP, specific address family related protocols
	
	//===========Bind socket to a local address and port===========// 
	/*socketServer Is socket, (SOCKADDR *) & serveraddress is server address*/ 
	if(bind(socketServer,(SOCKADDR*)&serverAddress,sizeof(SOCKADDR))==SOCKET_ERROR)
	{
		printf("Binding failed!\n");
	}
	else
	{
		printf("Bind successfully address port!\n");	
	} 
	
	//=================Set socket to listen=================// 
	if(listen(socketServer,10)<0)							//Monitor status, prepare for connection, the maximum number of waits is 10 
	{
		printf("Listening failed!\n"); 
	}
	else
	{
		printf("Monitoring...\n");	
	} 
	
	//=======================Receive connection=======================//
	length=sizeof(SOCKADDR);
	/*Receive the client's send request and wait for the client's connection request*/
	socketClient=accept(socketServer,(SOCKADDR*)&clientAddress,&length);
	if(socketClient==SOCKET_ERROR)
	{
		printf("Receive connection failed!\n");
	}
		
	//=======================Chat=======================//
	while(1)												//Infinite cycle 
	{	
		/*receive data*/
		receiveLength=recv(socketClient,receiveBuffer,100,0);
		if(receiveLength<0)
		{
			printf("Failed to receive!\n");
			printf("Program exit!\n");
			break;
		}
		else
		{
			printf("Client : %s\n",receiveBuffer);			//Display data sent by client 
		}
		
		/*send data*/
		printf("Please enter a message:");
		gets(sendBuffer);
//		scanf("%s",sendBuffer);
		sendLength=send(socketClient,sendBuffer,100,0);	
		if(sendLength<0)
		{
			printf("Sending failed!\n");
		}
		else
		{
			printf("Sending succeeded!\n");	
		}
	}
	
	//==============Release socket, close dynamic library==============//
	closesocket(socketClient);							//Turn off socket resources for clients 
	closesocket(socketServer);							//Turn off socket resources for the server 
	WSACleanup();										//Close dynamic link library 
	return 0;
}

client.c

#include<stdio.h>
#include<winsock.h>

int main()
{
	//=======================Defining variables=======================//
	char sendBuffer[100];									//Buffer to send data
	char receiveBuffer[100];								//Buffer to receive data
	int sendLength;											//Length of data sent
	int receiveLength;										//Length of received data
	
	SOCKET socketClient;									//Client's connection socket
	SOCKADDR_IN serverAddress;								//Server address information structure
	
	WORD wVersonRequested;									//Word: unsigned short
	WSADATA wsaData;										//Library version information structure
	int error;												//Expressing errors
	
	//====================Initialize socket library====================//
	wVersonRequested=MAKEWORD(2,2);							//Define version type, combine two bytes into one word, with low byte in front and high byte in back
	error=WSAStartup(wVersonRequested,&wsaData);			//Load the socket library and initialize the ws2-32.dll dynamic link library 
	if(error!=0)
	{
		printf("Failed to load socket library!\n");
		return 0;											//Program end 
	}
	if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
	{
		WSACleanup();										//Non conformity 
		return 0;											//Program end 
	}
	
	//=====================Set connection address=====================//
	serverAddress.sin_family=AF_INET;						//Address family, for AF? INET, note that only it is not network byte order
	serverAddress.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");	//Host address
	serverAddress.sin_port=htons(5050);						//Port number 
	
	//=====================Connect to server=====================//
	socketClient=socket(AF_INET,SOCK_STREAM,0);				//The client creates a socket, but does not need to bind. It only needs to establish a connection with the server
	/*To connect to the server*/
	if(connect(socketClient,(SOCKADDR*)&serverAddress,sizeof(SOCKADDR))==SOCKET_ERROR)
	{
		printf("Connection failed!\n");	
		return -1;
	} 
	else
	{
		printf("Connection succeeded!\n");
	}
	
	//=======================Chat=======================//
	while(1)
	{
		/*send data*/
		printf("Enter data to send:");
		gets(sendBuffer);
//		scanf("%s",sendBuffer);
		sendLength=send(socketClient,sendBuffer,100,0);		//send data
		if(sendLength<0)
		{
			printf("Sending failed!\n");	
		} 
		else
		{
			printf("Sending succeeded!\n");	
		}
		
		/*receive data*/
		receiveLength=recv(socketClient,receiveBuffer,100,0);
		if(receiveLength<0)
		{
			printf("Failed to receive!\n");
			printf("End of program!\n");
			break;
		}
		else
		{
			printf("Server:%s\n",receiveBuffer);
		}
	}
	
	//==============Release socket, close dynamic library==============//
	closesocket(socketClient);							//Turn off socket resources for clients 
	WSACleanup();										//Close dynamic link library 
	return 0;
}

demo run results

Run server.c first

Next, run client.c

The client sends a greeting to the server

At this time, the server receives the message

The server sends a message to the client

Client receives results

Climbing pit diary

Although the description is so simple, the blogger came to this conclusion after a variety of experiments because the functions such as WSAStartup were undefined and looked up the data all afternoon. Therefore, this article is made as a memorial. It's a long way to go and...

Published 11 original articles, won praise 4, visited 446
Private letter follow

Tags: socket network Programming

Posted on Wed, 12 Feb 2020 13:22:52 -0500 by hostcord