hw0921

文章详细描述了多线程和多进程发送接收TCP消息的C语言代码,以及广播和组播通信的实现方法,包括套接字创建、连接、数据传输和接收等操作。
摘要由CSDN通过智能技术生成

 多线程发送方

#include <myhead.h>
int main(int argc, const char *argv[])
{
		//创建流式套接字
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	printf("socket success\n");
	//填充服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(4444);
	sin.sin_addr.s_addr=inet_addr("192.168.125.230");

	//连接服务器
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		perror("connect");
		return -1;
	}
	printf("connect success\n");
	

	char buf[128]="";
	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf(" %s",buf);
	//	buf[strlen(buf)-1]='\0';

		write(cfd,buf,sizeof(buf));

	/*	res=recv(cfd,buf,sizeof(buf),0); 
		if(res<0)
		{
			perror("recv");
		}
		else if(0==res)
		{
			printf("服务器链接失败\n");
			break;
		}
	*/	printf("[192.168.122.84,4444]:%s\n",buf);
	}
	close(cfd);

	return 0;
}

多线程接收方

#include <myhead.h>
int main(int argc, const char *argv[])
{
		//创建流式套接字
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	printf("socket success\n");
	//填充服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(4444);
	sin.sin_addr.s_addr=inet_addr("192.168.125.230");

	//连接服务器
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		perror("connect");
		return -1;
	}
	printf("connect success\n");
	

	char buf[128]="";
	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf(" %s",buf);
	//	buf[strlen(buf)-1]='\0';

		write(cfd,buf,sizeof(buf));

	/*	res=recv(cfd,buf,sizeof(buf),0); 
		if(res<0)
		{
			perror("recv");
		}
		else if(0==res)
		{
			printf("服务器链接失败\n");
			break;
		}
	*/	printf("[192.168.122.84,4444]:%s\n",buf);
	}
	close(cfd);

	return 0;
}
ubuntu@ubuntu:TCP$ cat pth_ser.c
#include <myhead.h>
#define PORT 4444
#define IP "192.168.125.230"
struct Climsg{
	int newfd;
	struct sockaddr_in cin;
};
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");
	//bind
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
	printf("bind success\n");
	//listen
	listen(sfd,128);
	//存储客户端信息0
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	int newfd;
	pthread_t tid;
	struct Climsg info;
	while(1)
	{
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd<0)
		{
			perror("accept");
			return -1;
		}
		printf("connect success\n");
		printf("[%s:%d]\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		info.newfd=newfd;
		info.cin=cin;
		pthread_create(&tid,NULL,deal_cli_msg,(void*)&info);
		pthread_detach(tid);//分离线程
	}
	close(sfd);
	return 0;
}
void* deal_cli_msg(void *info)
{
	int newfd=((struct Climsg*)info)->newfd;
	struct sockaddr_in cin=((struct Climsg*)info);
	char buf[128];
	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接收数据
		res=recv(newfd,buf,sizeof(buf),0);
		if(res<0)
		{
			perror("recv");
			break;
		}
		else if(0==res)
		{
			printf("[%s:%d]客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
			break;
		}
		printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
		if(strcmp(buf,"quit")==0)
		{
			break;
		}
		//发送数据
		strcat(buf,"-_-_--");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			perror("send");
			break;
		}printf("发送成功\n");
	}
	close(newfd);
}

多进程发送方

#include <myhead.h>
int main(int argc, const char *argv[])
{
		//创建流式套接字
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	printf("socket success\n");
	//填充服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(4444);
	sin.sin_addr.s_addr=inet_addr("192.168.125.230");

	//连接服务器
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		perror("connect");
		return -1;
	}
	printf("connect success\n");
	

	char buf[128]="";
	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf(" %s",buf);
	//	buf[strlen(buf)-1]='\0';

		write(cfd,buf,sizeof(buf));

	/*	res=recv(cfd,buf,sizeof(buf),0); 
		if(res<0)
		{
			perror("recv");
		}
		else if(0==res)
		{
			printf("服务器链接失败\n");
			break;
		}
	*/	printf("[192.168.122.84,4444]:%s\n",buf);
	}
	close(cfd);

	return 0;
}

多线程接收方

ubuntu@ubuntu:TCP$ cat tcp_ser.c
#include <myhead.h>
#define PORT 4444
#define IP "192.168.125.230"
void handler(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);
	return;
}
int main(int argc, const char *argv[])
{
	//17信号
	if(signal(SIGCHLD,handler)==SIG_ERR)
	{
		ERR_MSG("signal");
		return -1;
	}
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");
	//bind
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
	printf("bind success\n");
	//listen
	listen(sfd,128);
	//存储客户端信息0
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	int newfd;
	pid_t p;
	while(1)
	{
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		printf("1\n");
		if(newfd<0)
		{
			perror("accept");
			return -1;
		}
		printf("connect success\n");
		printf("[%s:%d]\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		p=fork();
		if(p==0)
		{

			//接收
			char buf[128]="";
			size_t res=0;
			while(1)
			{
				bzero(buf,sizeof(buf));
				res=recv(newfd,buf,sizeof(buf),0); 
				if(res<0)
				{
					perror("recv");
				}
				else if(0==res)
				{
					printf("客户端链接失败,客戶端IP:%s\n,客戶端端口:%d\n",\
						inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
					break;
				}
				printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
			}
			close(newfd);
			exit(0);
		}
	close(newfd);
	}
	return 0;
}

广播发送方

#include <myhead.h>
#define PORT 4444
#define IP "192.168.125.255"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int cfd=socket(AF_INET,SOCK_DGRAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");
	int a=1;
	if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&a,sizeof(a))<0)
	{
		printf("setsockopt broadcast");
	}
	printf("a=%d\n",a);
	printf("设置广播成功\n");

	//服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	
	char buf[128]="";
	ssize_t res=0;
	socklen_t addrlen=sizeof(sin);
	while(1)
	{
		//发送
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf("%s",buf);
		sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen);
		printf("发送成功\n");
		//接收
	/*	bzero(buf,sizeof(buf));
		res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen); 
		if(res<0)
		{
			perror("recv");
		}
		printf(":%s\n",buf);
*/
	}
	close(cfd);
	return 0;
}

广播接收方

#include <myhead.h>
#define PORT 4444
#define IP "192.168.125.255"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");
/*	int size1=0;
	socklen_t len=sizeof(size1);
	getsockopt(sfd,SOL_SOCKET,SO_RCVBUF,&size1,&len);
	printf("size1=%d\n",size1);
*/	//bind
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
	printf("bind success\n");
	//客户端地址
	struct sockaddr_in cin;
	char buf[128]="";
	ssize_t res=0;
	socklen_t addrlen=sizeof(cin);
	while(1)
	{
		//接收
		bzero(buf,sizeof(buf));
		res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen); 
		if(res<0)
		{
			perror("recv");
		}
		printf(":%s\n",buf);
		//与发送信息的客户端绑定
/*		connect(sfd,(struct sockaddr*)&cin,addrlen);
		if(strcmp(buf,"quit")==0)
		{
			sin.sin_family=AF_UNSPEC;
			printf("断开连接\n");
		}
		//发送
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf("%s",buf);
		sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen);
		printf("发送成功\n");
		*/
	}
	close(sfd);
	return 0;
}

组播接收方

#include <myhead.h>
#define PORT 4444
#define GIP "224.5.5.5"
#define LIP "192.168.125.230"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");
	//加入多播组
	struct ip_mreqn mq;
	mq.imr_multiaddr.s_addr=inet_addr(GIP);//组播ip
	mq.imr_address.s_addr=inet_addr(LIP);//本地ip
	mq.imr_ifindex=2;//网络设备索引号
	if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq))<0)
	{
		perror("setsockopt(group)");
		return -1;
	}
	printf("加入多播组[%s]成功\n",GIP);
	//服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(LIP);
	bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
	printf("绑定地址信息成功\n");

	struct sockaddr_in cin;
	char buf[128]="";
	ssize_t res=0;
	socklen_t addrlen=sizeof(cin);
	while(1)
	{
		//接收
		bzero(buf,sizeof(buf));
		res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(res<0)
		{
			perror("recv");
		}
		printf(":%s\n",buf);
	}
	close(sfd);
	return 0;
}

组播发送方

#include <myhead.h>
#define PORT 4444
#define GIP "224.5.5.5"
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int cfd=socket(AF_INET,SOCK_DGRAM,0);
	printf("socket success\n");
	//允许端口被重复使用
	int reuse=1;
	setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0;
	printf("允许端口被重复使用\n");

	//服务器地址
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(GIP);
	
	char buf[128]="";
	ssize_t res=0;
	socklen_t addrlen=sizeof(sin);
	while(1)
	{
		//发送
		bzero(buf,sizeof(buf));
		printf("输入:");
		scanf("%s",buf);
		sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen);
		printf("发送成功\n");
		//接收
	/*	bzero(buf,sizeof(buf));
		res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen); 
		if(res<0)
		{
			perror("recv");
		}
		printf(":%s\n",buf);
*/
	}
	close(cfd);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值