TCP本地通信
1)服务器
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<signal.h>
#include<pthread.h>
#include<sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
struct msg
{
int newfd;
struct sockaddr_un cun;
};
void* rcv_cli_msg(void*arg)
{
pthread_detach(pthread_self());
int newfd = (*(struct msg*)arg).newfd;
struct sockaddr_un cun = (*(struct msg*)arg).cun;
char buf[128]={};
while(1)
{
bzero(buf,sizeof(buf));
int res;
res = recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return (void*)(-1);
}
else if(res==0)
{
printf("newfd = %d\n",newfd);
printf("客户端退出\n");
break;
}
printf("newfd = %d %s\n",newfd,buf);
}
close(newfd);
pthread_exit(NULL);
return 0;
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("create socket success\n");
struct sockaddr_un sun;
sun.sun_family = AF_LOCAL;
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);
pthread_t tid;
struct msg cliInfo;
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cun,&addrlen);
if(newfd <0)
{
perror("accept");
return -1;
}
printf("newfd = %d \n",newfd,);
cliInfo.newfd = newfd;
cliInfo.cun = cun;
if(pthread_create(&tid,NULL,rcv_cli_msg,(void*)&cliInfo)!=0)
{
ERR_MSG("pthread_create\n");
return -1;
}
}
close(sfd);
return 0;
}
2)客户端
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <signal.h>
#include<stdio.h>
#include<sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int sfd = socket(AF_LOCAL,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;
}
char buf[128] = "";
ssize_t res;
while(1)
{
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
res = send(sfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(sfd);
return 0;
}
UDP本地通信
1)服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>
#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;
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./udp_unix");
if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cun, &addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s]:%s\n", cun.sun_path, buf);
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
}
close(sfd);
return 0;
}
2)客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>
#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 sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./udp_unix");
struct sockaddr_un xun;
xun.sun_family = AF_UNIX;
strcpy(xun.sun_path,argv[1]);
if(bind(sfd, (struct sockaddr*)&xun, sizeof(xun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char str[128] = "";
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
char buf[128] = "";
while(1)
{
bzero(str,sizeof(str));
printf("请输入>>>");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
if(sendto(sfd,str,sizeof(str),0,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
bzero(buf, sizeof(buf));
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cun, &addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s]:%s\n",cun.sun_path, buf);
}
close(sfd);
return 0;
}