C语⾔实现简易⽹络进程及线程间通信1.单进程通信客户端代码
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<netinet/in.h>
#include<string.h>
void usage(const char *str)
{
printf("%s [IP][PORT] \n",str);
}
//  ./clie 127.0.0.1 8000
int main(int argc, char *argv[])
{
if(argc != 3)
{
usage(argv[0]);
exit(1);
}
struct sockaddr_in serv_addr;
int sockfd;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("socket");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret = 0;
ret = connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); if(ret < 0)
{
perror("connect");
exit(2);
}
int n = 0;
char buf[1024] = { 0 };
while(1)
{
printf("client# ");
fflush(stdout);
int n = read(0,buf,sizeof(buf)-1);
buf[n-1] = '\0'; //换⾏符不读。
write(sockfd,buf,strlen(buf));
n = read(sockfd,buf,sizeof(buf)-1);
buf[n] = 0;
printf("server echo#:%s\n",buf);
}
return0;
}
2.单进程通信服务端代码
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<netinet/in.h>
#include<string.h>
void usage(const char *str)
{
printf("%s [IP][PORT]\n",str);
}
/
/./serv 127.0.0.1 8000
int main(int argc ,char *argv[])
{
if(argc != 3)
{
usage(argv[0]);
exit(1);
}
struct sockaddr_in serv_addr,clie_addr;
int listenfd,connectfd;
int ret ;
socklen_t clie_addr_len;
listenfd = socket(AF_INET,SOCK_STREAM,0);
if(listenfd < 0)
{
perror("socket");
exit(2);
}
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons( atoi( argv[2] ) );
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
ret = bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
if(ret < 0)
{
perror("bind");
exit(3);
}
ret = listen(listenfd,20);
if(ret < 0)
{
perror("listen");
exit(4);
}
while(1)
{
clie_addr_len = sizeof(clie_addr);
connectfd = accept(listenfd,(struct sockaddr*)&clie_addr,&clie_addr_len);
if(connectfd < 0)
{
perror("accept");
perror("accept");
exit(5);
}
printf("clie ip %s,clie port %d\n",inet_ntoa(clie_addr.sin_addr),ntohs(clie_addr.sin_port));
int n = 0;
char buf[1024] = { 0 } ;
while(1)
{
n = read(connectfd,buf,sizeof(buf)-1);  //最多读sizeof(buf)-1,留下⼀个空间放\0    if(n ==0 )
{
printf("clie quit!\n");
break;
}
else if(n < 0)
{
exit(6);
}
else
{
buf[n] = 0;
printf("client say:%s\n",buf);
write(connectfd,buf,strlen(buf));
}
}
}
close(listenfd);
close(connectfd);
return0;
}
3.多进程通信服务端代码
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<netinet/in.h>
#include<stdlib.h>
static int startup(const char *_ip,int _port)
{
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
//  int on = 1;
//  int ret = setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
//  if(ret < 0)
{
//      perror("setsockopt");
//      exit(5);
}
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(_port);
c语言struct头文件serv_addr.sin_addr.s_addr = inet_addr(_ip);
serv_addr.sin_addr.s_addr = inet_addr(_ip);
if(bind (sock,(struct sockaddr*)& serv_addr,sizeof(serv_addr) )  < 0    )
{
perror("bind");
exit(3);
}
if(listen(sock,10) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
static void usage(const char *str)
{
printf("Usage:%s [IP] [PORT]\n",str);
}
int main(int argc,char *argv[])
{
if(argc != 3)
{
usage(argv[0]);
return1;
}
int listen_sock = startup(argv[1],atoi(argv[2]));
struct sockaddr_in clie_addr;
socklen_t len = sizeof(clie_addr);
while(1)
{
int sock = accept(listen_sock ,(struct sockaddr*)&clie_addr,&len );
if(sock > 0)
{
pid_t id = fork();
if(id == 0) //child
{
if(fork() > 0)//⼦进程退出,让孙⼦进程办事,孙⼦进程成为孤⼉进程,被1号进程回收。                    exit(0);
char buf[BUFSIZ];
while(1)
{
ssize_t s = read(sock,buf,sizeof(buf)-1);
if(s > 0)
{
buf[s] = 0;
printf("client say#:%s\n",buf);
write(sock,buf,strlen(buf));
}
else if(s == 0)
{
printf("client quit!\n");
break;
}
else
break;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。