zoukankan      html  css  js  c++  java
  • socket 聊天室实现

    server

    #include <stdio.h>
    #include <sys/types.h>          /* See NOTES */
    #include <sys/socket.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void handler(int sig)
    {
            printf("recv a sig=%d
    ",sig);
            exit(1);
    }
    
    
    int main(void)
    {
            int listenfd;
            listenfd = socket(PF_INET,SOCK_STREAM,0);
            if(listenfd < 0)
            {
                    printf("creat socket failed
    ");
                    exit(1);
            }
            struct sockaddr_in servaddr;
            memset(&servaddr,0,sizeof(servaddr));
            servaddr.sin_family = AF_INET;
            servaddr.sin_port = htons(5188);
            servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//host any address
    
            int n=1;
            if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n))<0)
            {
                    printf("set resueaddr failed
    ");
                    exit(1);
             }
    
            if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
            {
                    printf("bind fialed
    ");
                    exit(1);
            }
            if(listen(listenfd,SOMAXCONN)<0)
            {
                    printf("listen failed
    ");
                    exit(1);
            }
            struct sockaddr_in peeraddr;
            socklen_t peerlen = sizeof(peeraddr);
            int conn;
            if((conn=accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen))<0)
            {
                    printf("accept failed
    ");
                    exit(1);
            }
            printf("ip=%s, port=%d
    ",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
    
            pid_t pid;
            pid = fork();
            if (pid == -1)
            {
                    printf("server fork failed
    ");
                    exit(1);
            }
    
            if(pid == 0) //send to client data
            {
                    char recvbuf[1024];
                    while (1)
                    {
                            memset(recvbuf,0 ,sizeof(recvbuf));
                            int ret = read(conn,recvbuf,sizeof(recvbuf));
                            if( ret == -1)
                            {
                                    printf("server read failed
    ");
                                    exit(1);
                            }
                            else if (ret == 0)
                            {
                                    printf("client close
    ");
                                    break;
                            }
                            fputs(recvbuf,stdout);
                    }
                    printf("parent close 
    ");
                    kill(pid,SIGUSR1);
                    close(conn);
                    exit(1);
            }
            else //recv from client data
            {
                    signal(SIGUSR1,handler);
                    char sendbuf[1024]={0};
                    while(fgets(sendbuf,sizeof(sendbuf),stdin) != NULL)
                    {
                            write(conn,sendbuf,strlen(sendbuf));
                            memset(sendbuf, 0 ,sizeof(sendbuf));
                    }
                    printf("server childpid close
    ");
                    close(conn);
                    exit(1);
            }
            close(listenfd);
            return 0;
    }

    client

    #include <stdio.h>
    #include <sys/types.h>          /* See NOTES */
    #include <sys/socket.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void handler(int sig)
    {
            printf("recv a sig=%d
    ",sig);
            exit(1);
    }
    
    int main(void)
    {
            int sock;
            sock = socket(PF_INET,SOCK_STREAM,0);
            if(sock < 0)
            {
                    printf("creat socket failed
    ");
                    exit(1);
            }
            struct sockaddr_in servaddr;
            memset(&servaddr,0,sizeof(servaddr));
            servaddr.sin_family = AF_INET;
            servaddr.sin_port = htons(5188);
            //servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//host any address
            servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
            if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
            {
                    printf("connect failed 
    ");
                    exit(1);
            }
            pid_t pid;
            pid=fork();
            if(pid == -1)
            {
                    printf("client fork failed
    ");
                    exit(1);
            }
            if(pid == 0) // recv from server data
            {
                    char recvbuf[1024] = {0};
                    while(1)
                    {
                            memset(recvbuf,0,sizeof(recvbuf));
                            int ret = read(sock,recvbuf,sizeof(recvbuf));
                            if(ret == -1)
                            {
                                    printf("read faileed
    ");
                                    exit(1);
                            }
                            else if(ret ==0)
                            {
                                    printf("server close
    ");
                                    break;
                            }
                            fputs(recvbuf,stdout);
                    }
                    close(sock);
                    kill(getppid(),SIGUSR1);
            }
            else //send to server data
            {
                    signal(SIGUSR1,handler);
                    char sendbuf[1024] = {0};
                    while(fgets(sendbuf,sizeof(sendbuf),stdin) != NULL)
                    {
                            write(sock, sendbuf,strlen(sendbuf));
                            memset(sendbuf,0,sizeof(sendbuf));
                    }
                    close(sock);
            }
            return 0;
    }
  • 相关阅读:
    理解爬虫原理
    中文词频统计与词云生成
    复合数据类型,英文词频统计
    字符串操作、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    作业四-结对项目
    大数据应用期末总评
    分布式文件系统HDFS 练习
    安装Hadoop
    《恶魔人crybaby》豆瓣短评爬取
  • 原文地址:https://www.cnblogs.com/Malphite/p/7531682.html
Copyright © 2011-2022 走看看