/服务器的代码
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SEVER_IP "192.168.11.224"
#define PORT_NUM 1111
typedef struct sockaddr SA;
typedef struct sockaddr_in SIN;
int main(int argc,char *argv[])
{
int sockfd;
SIN seraddr = {0};
int ret,ret1,ret2;
int res,res1,res2;
int reuse = 1;
int clifd1,clifd2;
bzero(&seraddr,sizeof(SIN));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(PORT_NUM);
seraddr.sin_addr.s_addr = inet_addr(SEVER_IP);
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
{
printf("socketfd error\r\n");
exit(0);
}
printf("sockfd = %d\r\n",sockfd);
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
ret=bind(sockfd,(SA *)&seraddr,sizeof(SA));
if(ret == -1)
{
printf("failed\r\n");
exit(0);
}
printf("ret = %d\r\n",ret);
ret1 = listen(sockfd,5);
if(ret1 == 0)
{
printf("listen success\r\n");
}
SIN cliaddr = {0};
socklen_t addrlen;
clifd1 = accept(sockfd,(SA *)&cliaddr,&addrlen);//accept函数是有阻塞功能
printf("clifd1 = %d\r\n",clifd1);
clifd2 = accept(sockfd,(SA *)&cliaddr,&addrlen);//accept函数是有阻塞功能
printf("clifd2 = %d\r\n",clifd2);
if(clifd1 == -1)
{
perror("accept failed\r\n");
exit(0);
}
if(clifd2 == -1)
{
perror("accept failed\r\n");
exit(0);
}
res = fork();
while(1)
{
if(res == 0)
{
while(1)
{
char r_buf[20] = {0};
recv(clifd1,r_buf,sizeof(r_buf),0);
send(clifd2,r_buf,sizeof(r_buf),0);
}
}
else
{
while(1)
{
char t_buf[20] = {0};
recv(clifd2,t_buf,sizeof(t_buf),0);
send(clifd1,t_buf,sizeof(t_buf),0);
}
}
}
close(sockfd);
close(clifd1);
close(clifd2);
return 0;
}
================================================
//客户端的代码
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SEVER_IP "192.168.11.224"
#define PORT_NUM 1111
typedef struct sockaddr SA;
typedef struct sockaddr_in SIN;
int main(int argc,char *argv[])
{
int serfd;
int clidfd;
SIN seraddr = {0};
int ret,ret1,ret2;
int rec_len = 0;
bzero(&seraddr,sizeof(SIN));
serfd = socket(AF_INET,SOCK_STREAM,0);
if(serfd == -1)
{
perror("socket failed\r\n");
exit(0);
}
printf("%d\r\n",serfd);
bzero(&seraddr,sizeof(SIN));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(PORT_NUM);
seraddr.sin_addr.s_addr = inet_addr(SEVER_IP);
serfd = socket(AF_INET,SOCK_STREAM,0);
ret = connect(serfd,(SA *)&seraddr,sizeof(SA));
if(ret == -1)
{
perror("connect failed\r\n");
exit(0);
}
printf("connect success\r\n");
int res;
res = fork();
if(res == 0)
{
char t_buf[20] = {0};
while(1)
{
scanf("%s",t_buf);
send(serfd,t_buf,sizeof(t_buf),0);
}
}
else
{
char r_buf[20] = {0};
while(1)
{
recv(serfd,r_buf,sizeof(r_buf),0);
printf("A:%s\r\n",r_buf);
}
}
close(serfd);
return 0;
}
文章来源:https://www.toymoban.com/news/detail-637193.html
文章来源地址https://www.toymoban.com/news/detail-637193.html
到了这里,关于多进程利用TCP进行信息群发功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!