实现一个答题抱答系统,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 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
- ¥15 找一位技术过硬的游戏pj程序员
- ¥15 matlab生成电测深三层曲线模型代码
- ¥50 随机森林与房贷信用风险模型
- ¥50 buildozer打包kivy app失败
- ¥30 在vs2022里运行python代码
- ¥15 不同尺寸货物如何寻找合适的包装箱型谱
- ¥15 求解 yolo算法问题
- ¥15 虚拟机打包apk出现错误
- ¥15 用visual studi code完成html页面