2022-10-20,tcp以及udp的socket示例

1:tcp
sever

#include
#include 
#include 

#include           /* See NOTES */
#include 
#include
#include
#include

#define PORT 8888
#define IP "192.168.8.192"
#define SIZE 128
#define MSG_ERR(msg) do{\
	fprintf(stderr,"%d",__LINE__);\
	perror(msg);\
	return -1;\
}while(0)

int main(int argc,const char *argv[])
{
	//1:创建TCP IPV4套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd)
		MSG_ERR("socket");
	//2:绑定服务器ip port
	struct sockaddr_in sever;
	sever.sin_family = AF_INET;
	sever.sin_addr.s_addr = inet_addr(IP);
	sever.sin_port = htons(PORT);

	if(bind(sfd,(struct sockaddr *)&sever,sizeof(sever)) == -1)
		MSG_ERR("bind");
	//3:设置监听模式
	if(listen(sfd,128)  == -1)
		MSG_ERR("listen");
	//4:阻塞接受客户端连接
	struct sockaddr_in client;
	socklen_t len = sizeof(client);

	int newfd = accept(sfd,(struct sockaddr*)&client,&len);
	if(-1 == newfd)
		MSG_ERR("accept");
	//5:接收处理客户端请求
	char buff[SIZE] = "";
	while(1)
	{
		int res;
		if((res = recv(newfd,buff,sizeof(buff),0) )== -1)
			MSG_ERR("recv");
		else if (res == 0)
		{
			printf("对端关闭\n");
			break;
		}
		printf("[%s :%d] %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buff);
		sleep(1);
	//6:发送处理
	strcpy(buff,"hello client");
	send(newfd,buff,sizeof(buff),0);
	}
	//7:关闭退出
	close(newfd);
	close(sfd);
}

client

#include
#include 
#include 

#include           /* See NOTES */
#include 
#include
#include
#include

#define PORT 8888
#define IP "192.168.8.192"
#define SIZE 128
#define MSG_ERR(msg) do{\
	fprintf(stderr,"%d",__LINE__);\
	perror(msg);\
	return -1;\
}while(0)

int main(int argc,const char *argv[])
{
	//1:创建TCP IPV4套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd)
		MSG_ERR("socket");
	/*2:绑定客户端ip port(可以不绑定,系统默认生成)

	if(bind(sfd,(struct sockaddr *)&sever,sizeof(sever)) == -1)
		MSG_ERR("bind");
	
		*/
	struct sockaddr_in sever;
	sever.sin_family = AF_INET;
	sever.sin_addr.s_addr = inet_addr(IP);
	sever.sin_port = htons(PORT);
	//3:连接服务器
	if(connect(sfd,(struct sockaddr *)&sever,sizeof(sever)) == -1)
		MSG_ERR("accept");
	char buff[SIZE] = "";
	//4:发送处理
	while(1)
	{
	strcpy(buff,"hello sever");
	send(sfd,buff,sizeof(buff),0);
	sleep(1);
	//5:接收服务器反馈
		if(recv(sfd,buff,sizeof(buff),0) == -1)
			MSG_ERR("recv");
		printf("[%s :%d] %s\n",inet_ntoa(sever.sin_addr),ntohs(sever.sin_port),buff);
	}	
	//7:关闭退出
	close(sfd);
}

2:udp
sever

#include
#include 
#include 

#include           /* See NOTES */
#include 
#include
#include
#include

#define PORT 8888
#define IP "192.168.8.192"
#define SIZE 128
#define MSG_ERR(msg) do{\
	fprintf(stderr,"%d",__LINE__);\
	perror(msg);\
	return -1;\
}while(0)



int main(int argc,const char *argv[])
{
	//1:创建套接字
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(-1 == sfd)
		MSG_ERR("socket");
	//2:绑定服务器套接字ip port
	struct sockaddr_in sever;
	sever.sin_family = AF_INET;
	sever.sin_addr.s_addr = inet_addr(IP);
	sever.sin_port = htons(PORT);
	if(bind(sfd,(struct sockaddr *)&sever,sizeof(sever)) == -1)
		MSG_ERR("bind");
	//3:接受客户端请求
	char buff[SIZE] = "";
	struct sockaddr_in recv;
	socklen_t len = sizeof(recv);

while(1)
{
	strcpy(buff,"hello sever");
	if(recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&recv,&len) == -1)
		MSG_ERR("recvfrom");
	printf("[%s : %d] %s\n",inet_ntoa(recv.sin_addr),ntohs(recv.sin_port),buff);
	//4:处理请求
	sleep(1);
	strcpy(buff,"hello client");
	if(sendto(sfd,buff,sizeof(buff),0,(struct sockaddr*)&recv,len) == -1)
		MSG_ERR("sendto");
}
	//5:关闭
	close(sfd);
}

clien

#include
#include 
#include 

#include           /* See NOTES */
#include 
#include
#include
#include

#define PORT 8888
#define IP "192.168.8.192"
#define SIZE 128
#define MSG_ERR(msg) do{\
	fprintf(stderr,"%d",__LINE__);\
	perror(msg);\
	return -1;\
}while(0)



int main(int argc,const char *argv[])
{
	//1:创建套接字
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(-1 == sfd)
		MSG_ERR("socket");
	/*
	 * 2:绑定客户端套接字ip port(不绑定,系统会默认自己绑定)
	if(bind(sfd,(struct sockaddr *)&sever,sizeof(sever)) == -1)
		MSG_ERR("bind");
	*/
	struct sockaddr_in sever;
	sever.sin_family = AF_INET;
	sever.sin_addr.s_addr = inet_addr(IP);
	sever.sin_port = htons(PORT);
	//
	char buff[SIZE] = "";
	struct sockaddr_in recv;
	socklen_t len = sizeof(recv);

while(1)
{
	//3:发送请求
	strcpy(buff,"hello sever");
	if(sendto(sfd,buff,sizeof(buff),0,(struct sockaddr*)&sever,sizeof(sever)) == -1)
		MSG_ERR("sendto");
	printf("发送成功\n");
	sleep(1);

	//4:服务器请求
	if(recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&recv,&len) == -1)
		MSG_ERR("recvfrom");
	printf("[%s : %d] %s\n",inet_ntoa(recv.sin_addr),ntohs(recv.sin_port),buff);
}
	//5:关闭
	close(sfd);
}

你可能感兴趣的:(udp,tcp/ip,网络)