文章目录
-
- 1. TCP多进程并发服务器
-
- 2. TCP多线程并发服务器
-
- 3. TCP本地通信
-
- 4. UDP本地通信
-
1. TCP多进程并发服务器
代码示例
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
#define PORT 8888
#define IP "192.168.31.127"
typedef void(*sighandler_t)(int);
int rcv_cli_msg(int newfd,struct sockaddr_in cin);
void handler(int signum)
{
while(waitpid(-1,NULL,WNOHANG) > 0 );
}
int main(int argc, const char *argv[])
{
sighandler_t s = signal(17,handler);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
pid_t pid = fork();
if(pid>0)
{
close(newfd);
}
else if(0 == pid)
{
close(sfd);
rcv_cli_msg(newfd,cin);
close(newfd);
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
int rcv_cli_msg(int newfd,struct sockaddr_in cin)
{
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s : %d] newfd = %d客户端退出\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s : %d] newfd = %d : %s \n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
}
}
2. TCP多线程并发服务器
代码示例
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
#define PORT 8888
#define IP "192.168.31.127"
void* rcv_cli_msg(void* arg);
struct msg
{
int newfd;
struct sockaddr_in cin;
};
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
struct msg cliInfo;
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
cliInfo.newfd = newfd;
cliInfo.cin = cin;
pthread_t tid;
if(pthread_create(&tid,NULL,rcv_cli_msg,(void*)&cliInfo)!=0)
{
ERR_MSG("pthread_create");
return -1;
}
}
close(sfd);
return 0;
}
void* rcv_cli_msg(void* arg)
{
pthread_detach(pthread_self());
int newfd = ((struct msg*)arg)->newfd;
struct sockaddr_in cin = ((struct msg*)arg)->cin;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
break;
}
else if(0 == res)
{
printf("[%s : %d] newfd = %d客户端退出\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s : %d] newfd = %d : %s \n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
break;
}
printf("send message success\n");
}
close(newfd);
pthread_exit(NULL);
}
3. TCP本地通信
代码示例
服务器
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
int sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
if(access("./unix",F_OK) == 0)
{
if(unlink("./unix")<0)
{
ERR_MSG("unlink");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun)) < 0 )
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
int newfd = accept(sfd,(struct sockaddr*)&cun,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("newfd = %d客户端退出\n",newfd);
break;
}
printf("%s %d %s\n",cun.sun_path,newfd,buf);
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send message success\n");
}
close(newfd);
close(sfd);
return 0;
}
客户端
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
int sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128] = "";
ssize_t res;
while(1)
{
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(send(sfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
bzero(buf,sizeof(buf));
res = recv(sfd,buf,sizeof(buf),0);
if(res < 0 )
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("server is off-line\n");
break;
}
printf("%ld : %s\n",res,buf);
}
close(sfd);
return 0;
}
4. UDP本地通信
代码示例
服务器
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
int sfd = socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
if(access("udpser_unix",F_OK)==0)
{
if(unlink("udpser_unix") < 0)
{
ERR_MSG("unlink");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"udpser_unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("bind");
return -1;
}
char buf[128];
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
while(1)
{
bzero(buf,sizeof(buf));
ssize_t res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("%s : %s\n",cun.sun_path,buf);
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
close(sfd);
return 0;
}
客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, " __%d__ ", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
if(argc < 2)
{
fprintf(stderr, "请输入\n");
return -1;
}
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
if(access(argv[1],F_OK)==0)
{
if(unlink(argv[1]) < 0)
{
ERR_MSG("unlink");
return -1;
}
}
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path,argv[1]);
if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"udpser_unix");
struct sockaddr_un rcv_addrmsg;
socklen_t addrlen = sizeof(rcv_addrmsg);
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
bzero(buf, sizeof(buf));
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcv_addrmsg, &addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("%s\n", buf);
}
close(sfd);
return 0;
}