周六就应该休息,一天就忙了两小时也不是我的错喵

目录

UDP的小总结

01:使用select实现一个基于UDP的一对一即时聊天程序。 

1.0 复读机服务器和树洞客户端

2.0 byby不了一点的敬业服务器!!!

今天到此为止!!!!

UDP的小总结

client :  socket -->            sendto / recvfrom --> close
server :  socket --> bind   --> recvfrom /sendto

int sockect (int domain,int type,int protocol);
//domain      AF_INET       AF_INET6
//type        SOCK_STREAM   SOCK_DGAM
//protocol    IPPROTO_TCP   IPPRPTP_UDP

ssize_t sendto (int sockfd,const void* buff,size_t len,int flags,
const struct sockaddr*dest_addr,socklen_t addrlen);

ssize_t recvfrom (int sockfd,const void* buff,size_t len,int flags,
const struct sockaddr*dest_addr,socklen_t addrlen);

01:使用select实现一个基于UDP的一对一即时聊天程序。 

1.0 复读机服务器和树洞客户端
#include <func.h>
//server.c
int main()
{
    //addr
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(8082);
    addr.sin_addr.s_addr=inet_addr("192.168.235.128");
    socklen_t len=sizeof(addr);

    //soekct 
    int sofd=socket(AF_INET,SOCK_DGRAM,0);
    //bind
    int ret=bind(sofd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret==-1){};
    //readfds  old_fds fd_max buff[]
    fd_set readfds;
    fd_set old_fds;
    FD_SET(sofd,&readfds);//stdin server加入监听
    FD_SET(STDIN_FILENO,&readfds);
    old_fds=readfds;
    char buff[4096]={0};
    int fd_max=sofd;
    
    while(1){
        readfds=old_fds;
        select(fd_max+1,&readfds,NULL,NULL,NULL);
        if(FD_ISSET(sofd,&readfds)){
            ret=recvfrom(sofd,buff,4096,0,NULL,NULL);
            if(ret<0){};
            if(strcmp(buff,"byby\n")==0){close(sofd);}
            printf("----%s\n",buff);
        }
        if(FD_ISSET(STDIN_FILENO,&readfds)){
            memset(buff,0,sizeof(buff));
            fgets(buff,4096,stdin);
            ret=sendto(sofd,buff,strlen(buff)+1,0,
                   (struct sockaddr*)&addr,sizeof(struct sockaddr));
            if(ret<0){};
        }
    }
    return 0;
}
#include <func.h>
//client.c
int main()
{
    //addr
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(8082);
    addr.sin_addr.s_addr=inet_addr("192.168.235.128");
    socklen_t len=sizeof(addr);

    //soekct 
    int sofd=socket(AF_INET,SOCK_DGRAM,0);
    //readfds  old_fds fd_max buff[]
    fd_set readfds;
    fd_set old_fds;
    FD_SET(sofd,&readfds);//stdin server加入监听
    FD_SET(STDIN_FILENO,&readfds);
    old_fds=readfds;
    char buff[4096]={0};
    int fd_max=sofd;
    
    while(1){
        readfds=old_fds;
        select(fd_max+1,&readfds,NULL,NULL,NULL);
        if(FD_ISSET(sofd,&readfds)){
            recvfrom(sofd,buff,4096,0,NULL,NULL);
            if(strcmp(buff,"byby\n")==0){close(sofd);}
            printf("----%s\n",buff);
        }
        if(FD_ISSET(STDIN_FILENO,&readfds)){
            memset(buff,0,sizeof(buff));
            fgets(buff,4096,stdin);
            sendto(sofd,buff,strlen(buff)+1,0,
                   (struct sockaddr*)&addr,sizeof(struct sockaddr));
        }
    }
    return 0;
}
2.0 byby不了一点的敬业服务器!!!

#include <func.h>
//client.c
int main()
{
    //addr
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(8083);
    addr.sin_addr.s_addr=inet_addr("192.168.235.128");
    socklen_t len=sizeof(addr);

    //soekct 
    int sofd=socket(AF_INET,SOCK_DGRAM,0);
    //bind
    int ret=bind(sofd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret==-1){};
    //readfds  old_fds fd_max buff[]
    fd_set readfds;
    fd_set old_fds;
    FD_SET(sofd,&readfds);//stdin server加入监听
    FD_SET(STDIN_FILENO,&readfds);
    old_fds=readfds;
    char buff[4096]={0};
    int fd_max=sofd;
        
    struct sockaddr_in cline_addr;
    socklen_t addr_len;
    while(1){
        readfds=old_fds;
        select(fd_max+1,&readfds,NULL,NULL,NULL);
        if(FD_ISSET(sofd,&readfds)){
            ret=recvfrom(sofd,buff,4096,0,(struct sockaddr*)&cline_addr,&addr_len);
            if(ret<0){};
            if(strcmp(buff,"byby")==0){close(sofd);}
            printf("----%s\n",buff);
        }
        if(FD_ISSET(STDIN_FILENO,&readfds)){
            memset(buff,0,sizeof(buff));
            fgets(buff,4096,stdin);
            ret=sendto(sofd,buff,strlen(buff)+1,0,
                   (struct sockaddr*)&cline_addr,sizeof(struct sockaddr));
            if(ret<0){};
        }
    }
    return 0;
}

开心!!!!!!开心!!!!!!!开心!!!!!!! 
723的作业会写了喵!!!!!聪明小辉!!!

今天到此为止!!!!!

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部