普通网友 2015-05-02 12:33 采纳率: 36.4%
浏览 3004
已采纳

Linux下的类似QQ聊天程序

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

这是server端代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

int main()
{
        int oldfd,newfd;
        struct sockaddr_in mysock;
        int n;
        char buf[1024];

        oldfd=socket(AF_INET,SOCK_STREAM,0);
        memset(&mysock,0,sizeof(mysock));
        mysock.sin_family=AF_INET;
        mysock.sin_port=htons(1025);
        mysock.sin_addr.s_addr=htonl(INADDR_ANY);

        bind(oldfd,(struct sockaddr*)&mysock,sizeof(mysock));
        listen(oldfd,13);
        while(1)
        {
                newfd=accept(oldfd,(struct sockaddr*)NULL,NULL);
                //read();
                n=read(newfd,buf,sizeof(buf));
                buf[n]=0;
                printf("%s\n",buf);
                write(newfd,buf,n);//再写到client上去
                close(newfd);
            }
        close(oldfd);
        return 0;
    }


以下是client端的代码:

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

int main(int argc,char **argv)
{
    int fd;
    int n;
    struct sockaddr_in mysock;
    char buf[1024];

    fd=socket(AF_INET,SOCK_STREAM,0);
    memset(&mysock,0,sizeof(mysock));
    mysock.sin_family=AF_INET;
    mysock.sin_port=htons(1025);
    inet_pton(AF_INET,argv[1],&mysock.sin_addr.s_addr);
    while(1){//这是在一次连接成功后添加的循环
        connect(fd,(struct sockaddr*)&mysock,sizeof(mysock));
        n=read(STDIN_FILENO,buf,sizeof(buf));
        write(fd,buf,n);

        read(fd,buf,sizeof(buf));
        buf[n]=0;
        printf("this is fankui information:%s\n",buf);

    }
    close(fd);
    return 0;
    }


  • 写回答

3条回答 默认 最新

  • Landpack 2015-05-05 04:31
    关注

    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 !!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配