qq_38696192 2018-06-13 06:10 采纳率: 100%
浏览 750

求源码(TCP或者UDP)C语言

实现一个答题抱答系统,1)服务端出题,题目来自题库文件2)多个客户端可同时与服务端连接,抢答题目,题目有1人答对或答题超时,服务端换下一题3)答题结束,服务端向客户端公布成绩和排名

  • 写回答

2条回答 默认 最新

  • ZhihengTao 2018-06-13 07:34
    关注

    采用Linux的socket机制通信就行了, 以下是本人稍稍封装的头文件sock.h, 若是能解决你的问题, 再加上源文件

    #ifndef __SOCK_H
    #define __SOCK_H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stddef.h>
    #include <string.h>
    
    /* ===== API for simple INET/UNIX socket ===== */
    
    
    /*
       Create a server
       If port is valid, create an INET server.
       Otherwise, create an UNIX server.
       Return serverfd for OK, negetive value for ERR
       Example:
           [INET Server]
           int serverfd;
           if ((serverfd = sock_server(2333, NULL, 5)) < 0)
           {
               prinf("Error: fail to create server\n");
           }
           [UNIX Server]
           int serverfd;
           if ((serverfd = sock_server(-1, "test.sock", 5)) < 0)
           {
               prinf("Error: fail to create server\n");
           }
    */
    int sock_server(int port, const char *sockfile, int max_connection);
    
    /*
       Accept a client and create a session
       Return sessionfd for OK, negetive value for ERR
       Example:
           int sessionfd;
           if ((sessionfd = sock_accept(serverfd)) < 0)
           {
               prinf("Error: fail to accept client\n");
           }
    */
    int sock_accept(int serverfd);
    
    /*
       Create a client connected to a server
       If host and port are valid, create an INET client.
       Otherwise, create an UNIX client.
       Return clientfd for OK, negetive value for ERR
       Example:
           [INET Client]
           int clientfd;
           if ((clientfd = sock_client("127.0.0.1", 2333, NULL)) < 0)
           {
               printf("Error: fail to create client\n");
           }
           [UNIX Client]
           int clientfd;
           if ((clientfd = sock_client(NULL, -1, "test.sock")) < 0)
           {
               prinf("Error: fail to create client\n");
           }
    */
    int sock_client(const char *host, int port, const char *sockfile);
    
    /*
       Close a server/client/session
       Example:
           close(clientfd);
           close(sessionfd);
           close(serverfd);
    */
    void sock_close(int fd);
    
    
    /*
       Send message
       Return message length sent for OK, negetive value for ERR
       Example:
           if ((sock_send(clientfd, msg, msg_len) < 0)
           {
               printf("Error: fail to send message\n");
           }
    */
    ssize_t sock_send(int sockfd, const void *buf, size_t len);
    
    /*
       Receive message
       Return message length received for OK, negetive value for ERR
       Example:
           if ((msg_len = sock_recv(sessionfd, buf, MAX_LEN) < 0)
           {
               printf("Error: fail to receive message\n");
           }
    */
    ssize_t sock_recv(int sockfd, void *buf, size_t len);
    
    /*
       Sock Example:
    
       [Sever]
        int serverfd;
        int sessionfd;
        char buf[512];
        int len;
        if ((serverfd = sock_server(-1, "test.sock", 5)) < 0)
        {
            exit(-1);
        }
        if ((sessionfd = sock_accept(serverfd)) < 0)
        {
            exit(-2);
        }
        if ((len = sock_recv(sessionfd, buf, 512)) > 0)
        {
            buf[len] = 0;
            printf("Recv: %s\n", buf);
        }
        sock_close(sessionfd);
        sock_close(serverfd);   
    
       [Client]
        int clientfd;
        char buf[512];
        int len;
        if ((clientfd = sock_client(NULL, -1, "test.sock")) < 0)
        {
            exit(-1);
        }
        sprintf(buf, "Hello world");
        len = strlen(buf);
        if ((len = sock_send(clientfd, buf, len)) > 0)
        {
            printf("Send: %s\n", buf);
        }
        sock_close(clientfd);
    */
    
    #endif
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵