unix TCP服务器
#include "head.h"
void hand(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
if(signal(17,hand)<0)
{
_errmsg("signal");
return -1;
}
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
_errmsg("socket");
return -1;
}
if(access("./unix",F_OK)==0)
{
unlink("./unix");
}
printf("%d\n",__LINE__);
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
_errmsg("bind");
return -1;
}
if(listen(sfd,128)<0)
{
_errmsg("listen");
return -1;
}
printf("%d\n",__LINE__);
struct sockaddr_un nun;
socklen_t addrlen2=sizeof(nun);
char buf[128];
ssize_t res;
bzero(buf,sizeof(buf));
pid_t cpid;
int newfd;
while(1)
{
printf("%d\n",__LINE__);
newfd=accept(sfd,(struct sockaddr*)&nun,&addrlen2);
if(newfd<0)
{
_errmsg("accept");
return -1;
}
while(1)
{
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
_errmsg("recv");
return -1;
}
else if(0 == res)
{
break;
}
buf[strlen(buf)-1]='\0';
strcat(buf,"@_@");
res=send(newfd,buf,sizeof(buf),0);
if(res<0)
{
_errmsg("send");
return -1;
}
}
printf("%d\n",__LINE__);
exit(0);
close(newfd);
}
close(sfd);
return 0;
}
unix TCP客户端
#include "head.h"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
_errmsg("socket");
return -1;
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
_errmsg("connect");
return -1;
}
while(1)
{
char buf[128];
ssize_t res;
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
res=send(sfd,buf,sizeof(buf),0);
if(res<0)
{
_errmsg("send");
return -1;
}
res=recv(sfd,buf,sizeof(buf),0);
if(res<0)
{
_errmsg("recv");
return -1;
}
}
close(sfd);
return 0;
}
unix UDP服务器
#include "head.h"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0)
{
_errmsg("socket");
return -1;
}
if(access("./myunix",F_OK) ==0)
{
unlink("./myunix");
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
socklen_t addrlen1=sizeof(sun);
strcpy(sun.sun_path,"./myunix");
if(bind(sfd,(struct sockaddr*)&sun,addrlen1)<0)
{
_errmsg("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_un nun;
socklen_t addrlen2=sizeof(nun);
char buf[128];
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&nun,&addrlen2);
if(res<0)
{
_errmsg("recvfrom");
return -1;
}
printf("%s:%s\n",nun.sun_path,buf);
strcat(buf,"@_@");
res=sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&nun,addrlen2);
if(res<0)
{
_errmsg("send");
return -1;
}
printf("%d\n",__LINE__);
}
close(sfd);
return 0;
}
unix UDP客户端
#include "head.h"
int main(int argc, const char *argv[])
{
char sockname[20]="";
scanf("%s",sockname);
while(getchar()!=10);
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0)
{
_errmsg("socket");
return -1;
}
printf("socket success\n");
struct sockaddr_un xun;
xun.sun_family=AF_UNIX;
strcpy(xun.sun_path,sockname);
socklen_t addrlen1=sizeof(xun);
if(bind(sfd,(struct sockaddr*)&xun,addrlen1)<0)
{
_errmsg("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_un nun;
nun.sun_family=AF_UNIX;
strcpy(nun.sun_path,"./myunix");
char buf[128];
ssize_t res;
struct sockaddr_un cun;
socklen_t addrlen2=sizeof(cun);
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf,"quit")==0)
{
unlink(sockname);
break;
}
res=sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&nun,sizeof(nun));
if(res<0)
{
_errmsg("sendto");
return -1;
}
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&addrlen2);
if(res<0)
{
_errmsg("recvfrom");
return -1;
}
printf("%s %s \n",cun.sun_path,buf);
}
close(sfd);
return 0;
}