gpt4 book ai didi

c - Linux TCP 服务器,每 X 秒向几个客户端发送 RAW 数据

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:27 25 4
gpt4 key购买 nike

我正在编写简单的 TCP 服务器,我发现了一些问题。也许你能帮我一点忙。所以,我先写了一个回显服务器(测试与计算机客户端的连接)。它工作正常,但现在我需要稍微改变一下。服务器应在连接时向客户端发送 char[100],并每隔 X 秒/分钟向每个客户端发送相同的 char[]。

我尝试了许多更改,但应用程序只是崩溃了。评论了我在这段代码中的一些“错误”。

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

/* BufferLength is 100 bytes */
#define BufferLength 100
/* Server port */
#define SERVPORT 6000



int main(){

/* Variable and structure definitions. */

int sd, wyslij, sd2, rc, length = sizeof(int);
int totalcnt = 0, on = 1;
char temp;
char buffer[BufferLength];
struct sockaddr_in serveraddr;
struct sockaddr_in their_addr;

fd_set read_fd;
struct timeval timeout;
timeout.tv_sec = 15;
timeout.tv_usec = 0;

char datadata[100] = "This is a test string from server lol!!! ";

/* Get a socket descriptor */
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Server-socket() error");
/* exit */
exit (-1);
}else
printf("Server-socket() is OK\n");

/* Allow socket descriptor to be reusable */

if((rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0){
perror("Server-setsockopt() error");
close(sd);
exit (-1);
}else
printf("Server-setsockopt() is OK\n");


/* bind to an address */
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

printf("Using %s, listening at %d\n", inet_ntoa(serveraddr.sin_addr), SERVPORT);


if((rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0){
perror("Server-bind() error");
/* Close the socket descriptor */
close(sd);
/* and just exit */
exit(-1);
}else
printf("Server-bind() is OK\n");

/* queue up to 10 clients */

if((rc = listen(sd, 10)) < 0){
perror("Server-listen() error");
close(sd);
exit (-1);
}else
printf("Server-Ready for client connection...\n");

/* accept() the incoming connection request. */
int sin_size = sizeof(struct sockaddr_in);

if((sd2 = accept(sd, (struct sockaddr *)&their_addr, &sin_size)) < 0){
perror("Server-accept() error");
close(sd);
exit (-1);
}else
printf("Server-accept() is OK\n");

/*client IP*/

printf("Server-new socket, sd2 is OK...\n");
printf("Got connection from the client: %s\n", inet_ntoa(their_addr.sin_addr));

/* Wait for up to 15 seconds on */
/* select() for data to be read. */

FD_ZERO(&read_fd);
FD_SET(sd2, &read_fd);
rc = select(sd2+1, &read_fd, NULL, NULL, &timeout);


/* rc = write(sd2, datadata, sizeof(datadata)); */
if((rc == 1) && (FD_ISSET(sd2, &read_fd))){

/* rc = write(sd2, datadata, sizeof(datadata)); */

/* Read data from the client. */
totalcnt = 0;

while(totalcnt < BufferLength){
/* read() from client */

rc = read(sd2, &buffer[totalcnt], (BufferLength - totalcnt));

if(rc < 0){
perror("Server-read() error");
close(sd);
close(sd2);
exit (-1);
}else if (rc == 0){
printf("Client program has issued a close()\n");
close(sd);
close(sd2);
exit(-1);
}
else{
totalcnt += rc;
printf("Server-read() is OK\n");
}
}
}else if (rc < 0){
perror("Server-select() error");
close(sd);
close(sd2);
exit(-1);
}
/* rc == 0 */
else{
printf("Server-select() timed out.\n");
close(sd);
close(sd2);
exit(-1);
}


/* Shows the data */

printf("Received data from the client: %s\n", buffer);

/* write() some bytes of string, */
/* back to the client. */

printf("Server-Echoing back to client...\n");
rc = write(sd2, datadata, sizeof(datadata));

if(rc != totalcnt){
perror("Server-write() error");
/* Get the error number. */
rc = getsockopt(sd2, SOL_SOCKET, SO_ERROR, &temp, &length);

if(rc == 0){
/* Print out the asynchronously */
/* received error. */
errno = temp;
perror("SO_ERROR was: ");
}else
printf("Server-write() is OK\n");
close(sd);
close(sd2);
exit(-1);
}

/* Close the connection to the client and */
/* close the server listening socket. */

close(sd2);
close(sd);
exit(0);

return 0;
}

非常感谢小伙伴们!

最佳答案

您可能想看看 D.J. Bernstein 的 tcpserver(参见 http://cr.yp.to/ucspi-tcp/tcpserver.html)。基本上,您可以简单地在 tcpserver 下运行您的 C 程序,tcpserver 将处理所有事情,包括设置套接字、列出您正在使用的任何端口上的传入连接等。当传入连接到达您指定的端口时, tcpserver 将生成您的程序的一个实例,并将来自客户端的传入信息通过管道传输到您程序的 STDIN,并将来自您程序的 STDOUT 的传出信息通过管道传输回客户端。通过这种方式,您可以专注于程序的核心逻辑(并简单地读/写到 stdout/stdin),并让 tcpserver 处理所有繁重的工作,直到套接字等。

关于c - Linux TCP 服务器,每 X 秒向几个客户端发送 RAW 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17924847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com