开发者社区> 问答> 正文

Linux下的类似QQ聊天程序

想在Linux下用socket套接字写个类似于windows下的QQ聊天程序,但是遇到不能循环发送和接受的问题,希望能向各位大侠请教,以下是代码:
这是server端代码:
screenshot
screenshot

展开
收起
杨冬芳 2016-07-11 17:22:27 2433 0
1 条回答
写回答
取消 提交回答
  • IT从业

    I assume you can make sense of my code!
    so i just show you code below,if you have any question,
    just let me know !!1

    server.c

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    
    #define BUF_SIZE 1024
    #define LISTEN_PORT 8889
    #define LISTEN_NO 10
    #define err_exit(msg) (perror(msg),(exit(EXIT_FAILURE)))
    
    int sock_make(void);
    void read_from(int fd);
    void send_to(int fd);
    
    int main(void)
    {
        int sockfd,connfd;
        struct sockaddr_in clientaddr;
        int n;
        char buf[BUF_SIZE];
        char recvbuf[BUF_SIZE];
        char sendbuf[BUF_SIZE];
    
    
        sockfd=sock_make();
        while(1){
            //If you wanna know which client and what the ip is 
            //You should no set the the argu being NULL ..
    
            if((connfd=accept(sockfd,(struct sockaddr*)NULL,NULL))<0)
            {
                err_exit(">>accept");
            }else{
                while(1){
                    puts("-----------wait message---------");
    KEEP_READ:
                    n=read(connfd,recvbuf,sizeof(recvbuf));
                    //recvbuf[n]=0;
                    printf("%s\n",recvbuf);
                    if(strncmp(recvbuf,".",1)) goto KEEP_READ;
                    puts("-----------Please input----------");
    KEEP_SEND:
                    n=read(STDIN_FILENO,sendbuf,sizeof(sendbuf));
                    sendbuf[n]=0;
                    write(connfd,sendbuf,sizeof(sendbuf));
                    if(strncmp(sendbuf,".",1)) goto KEEP_SEND;
    
                }
                close(connfd);
            }
        }
        close(sockfd);
        return 0;
    }
    int sock_make(void)
    {
        struct sockaddr_in clientaddr;
        int sockfd;
        memset(&clientaddr,SOCK_STREAM,sizeof(clientaddr));
        clientaddr.sin_family=AF_INET;
        clientaddr.sin_port=htons(LISTEN_PORT);
        clientaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
        {
            err_exit(">>socket");
        }
    
        if(bind(sockfd,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0)
        {
            err_exit(">>bind");
        }
    
        listen(sockfd,LISTEN_NO);
    
        return sockfd;
    }
    

    client.c

     #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    
    
    #define BUF_SIZE 1024
    #define LISTEN_PORT 8889
    #define LISTEN_NO 10
    #define err_exit(msg) (perror(msg),(exit(EXIT_FAILURE)))
    
    int main(int argc,char * argv[])
    {
        int sockfd;
        int n;
        struct sockaddr_in serverAddr;
        char sendbuf[BUF_SIZE];
        char recvbuf[BUF_SIZE];
    
        if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
        {   
            err_exit(">>socket");
        }   
        memset(&serverAddr,0,sizeof(serverAddr));
    
        serverAddr.sin_family=AF_INET;
    
        serverAddr.sin_port=htons(LISTEN_PORT);
        inet_pton(PF_INET,argv[1],&serverAddr.sin_addr.s_addr);
    
        while(1)
        {   
            if((connect(sockfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr)))<0)
            {
                err_exit(">>connect");
            }else{
                while(1){
    
                    puts("--------------Please input-----------");
    KEEP_SEND:
                    n=read(STDIN_FILENO,sendbuf,sizeof(sendbuf));
                    sendbuf[n]=0;
                    write(sockfd,sendbuf,sizeof(sendbuf));
                    if(strncmp(sendbuf,".",1)) goto KEEP_SEND;
                    puts("-------------Wait message------------");
    KEEP_READ:
                    n=read(sockfd,recvbuf,sizeof(recvbuf));
                    //recvbuf[n]=0; //EOF ..
                    printf("%s\n",recvbuf);
                    if(strncmp(recvbuf,".",1)) goto KEEP_READ;
                }
                close(sockfd);
            }
        }   
        return 0;
    }
    

    Please ,run the client as :

     ./client 127.0.0.1 
    if you run the both on both terminal 
    and you see something below:
    
    terminal 1 (run server)
    
     landpack@landpack-VirtualBox:~/ak/csdn$ ./server
    -----------wait message---------
    hello
    
    I am Landpack
    
    .
    
    -----------Please input----------
    Hey Landpack !
    good to know you
    .
    -----------wait message---------
    
    terminal 2 (run client)
    
     landpack@landpack-VirtualBox:~/ak/csdn$ ./client 127.0.0.1
    --------------Please input-----------
    hello
    I am Landpack
    .
    -------------Wait message------------
    Hey Landpack !
    
    good to know you
    
    .
    
    --------------Please input-----------
    
    
    All suggestion are welcome !
    Have fun !!
    
    2019-07-17 19:54:26
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载