流式域套接字
服务器端
#include "head.h"
int main(int argc, const char *argv[])
{
int afd=socket(AF_UNIX,SOCK_STREAM,0);
if(afd==-1){
perror("socket error");
return -1;
}
printf("socket success afd=%d\n",afd);
if(access("./unix",F_OK)==0){
if(unlink("./unix")!=0){
perror("access error");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(afd,(struct sockaddr*)&sun,sizeof(sun))==-1){
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(afd,128)==-1){
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_un cun;
socklen_t socklen=sizeof(cun);
int newfd=accept(afd,(struct sockaddr*)&cun,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
printf("[%s]:连接成功,newfd=%d",cun.sun_path,newfd);
char rbuf[128]="";
while(1){
bzero(rbuf,sizeof(rbuf));
int ret=recv(newfd,rbuf,sizeof(rbuf),0);
if(ret==0){
printf("断开连接\n");
}
printf("[%s]:%s\n",cun.sun_path,rbuf);
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
close(newfd);
close(afd);
return 0;
}
客户端
#include "head.h"
int main(int argc, const char *argv[])
{
int bfd=socket(AF_UNIX,SOCK_STREAM,0);
if(bfd==-1){
perror("socket error");
return -1;
}
printf("socket success bfd=%d\n",bfd);
if(access("./linux",F_OK)==0){
if(unlink("./linux")!=0){
perror("access error");
return -1;
}
}
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./linux");
if(bind(bfd,(struct sockaddr*)&cun,sizeof(cun))==-1){
perror("bind error");
return -1;
}
printf("bind success\n");
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(connect(bfd,(struct sockaddr*)&sun,sizeof(sun))==-1){
perror("connect error");
return -1;
}
printf("connect success\n");
char wbuf[128]="";
while(1){
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]='\0';
if(strcmp(wbuf,"quit")==0){
break;
}
send(bfd,wbuf,strlen(wbuf),0);
bzero(wbuf,sizeof(wbuf));
recv(bfd,wbuf,sizeof(wbuf),0);
printf("发送的客户端信息为:%s\n",wbuf);
}
close(bfd);
return 0;
}
报式域套接字
服务器
#include "head.h"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd==-1){
perror("socket error");
return -1;
}
printf("socket success");
if(access("./unix",F_OK)==0){
if(unlink("./unix")!=0){
perror("unlink error");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
struct sockaddr_un cun;
socklen_t socklen=sizeof(cun);
char rbuf[128]="";
while(1){
bzero(rbuf,sizeof(rbuf));
recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cun,&socklen);
printf("[%s]:%s\n",cun.sun_path,rbuf);
strcat(rbuf,"*_*");
if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cun,sizeof(cun))==-1){
perror("write error");
return -1;
}
printf("发送成功\n");
}
close(sfd);
return 0;
}
客户端
#include "head.h"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(cfd==-1){
perror("socket error");
return -1;
}
printf("socket success");
if(access("./linix",F_OK)==0){
if(unlink("./linix")!=0){
perror("unlink error");
return -1;
}
}
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./linix");
if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
char wbuf[128]="";
while(1){
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
sendto(cfd,wbuf,strlen(wbuf),0,(struct sockaddr*)&sun,sizeof(sun));
printf("发送成功\n");
recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);
printf("收到服务器消息为:%s\n",wbuf);
}
close(cfd);
return 0;
}
进程实现
#include "head.h"
#define SER_IP "192.168.125.71"
#define SER_PORT 8888
void handler(int signo){
if(signo==SIGCHLD){
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
int main(int argc, const char *argv[])
{
int afd=socket(AF_INET,SOCK_STREAM,0);
if(afd==-1){
perror("socket error\n");
return -1;
}
printf("socket success afd=%d\n",afd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(afd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(afd,128)==-1){
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
while(1){
int newfd=accept(afd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
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(pid==0){
close(afd);
char rbuf[128]="";
while(1){
bzero(rbuf,sizeof(rbuf));
int ret=recv(newfd,rbuf,sizeof(rbuf),0);
if(ret==0){
printf("客户端已下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
strcat(rbuf,"*V*");
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
close(newfd);
exit(EXIT_SUCCESS);
}
}
close(afd);
return 0;
}
线程实现
#include "head.h"
#define SER_IP "192.168.125.71"
#define SER_PORT 8888
struct MsgInfo{
int newfd;
struct sockaddr_in cin;
};
void *deal_cli_msg(void *arg){
int newfd=((struct MsgInfo*)arg)->newfd;
struct sockaddr_in cin=((struct MsgInfo*)arg)->cin;
char rbuf[128]="";
while(1){
bzero(rbuf,sizeof(rbuf));
int ret=recv(newfd,rbuf,sizeof(rbuf),0);
if(ret==0){
printf("客户端已下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
strcat(rbuf,"*V*");
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int afd=socket(AF_INET,SOCK_STREAM,0);
if(afd==-1){
perror("socket error\n");
return -1;
}
printf("socket success afd=%d\n",afd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(afd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(afd,128)==-1){
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
while(1){
int newfd=accept(afd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
printf("[%s:%d]:已连接,newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
struct MsgInfo info={newfd,cin};
pthread_t tid=-1;
if(pthread_create(&tid,NULL,deal_cli_msg,&info)!=0){
printf("线程创建失败\n");
return -1;
}
pthread_detach(tid);
}
close(afd);
return 0;
}