网络编程DAY4

流式域套接字

服务器端

#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;
}
 


  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值