gpt4 book ai didi

c - 发送具有多个数字的唯一字符串客户端/服务器 TCP

转载 作者:行者123 更新时间:2023-11-30 16:46:04 27 4
gpt4 key购买 nike

我正在用 C 进行 TCP 客户端/服务器通信。客户端必须获取数据数量(不超过 6),获取数据并将它们以单个字符串消息发送到服务器,如下所示:4 11 21 9 11.
4是数据个数,11、21、9、11是数据。

然后,服务器必须读取消息,打印它并向客户端发送收到的数据数量(4)。

我为客户尝试过这个:

#include <stdio.h>      
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>




int main(int argc, char *argv[]) {

int simpleSocket = 0;
int simplePort = 0;
int returnStatus= 0;
char buffsend[256];
char buffrecv[256];
int n, i;
struct sockaddr_in simpleServer;

if (3 != argc) {

fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]);
exit(1);

}

/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (simpleSocket == -1) {

fprintf(stderr, "Could not create a socket!\n");
exit(1);

}
else {
fprintf(stderr, "Socket created!\n");
}

/* retrieve the port number for connecting */
simplePort = atoi(argv[2]);

/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
simpleServer.sin_port = htons(simplePort);

/* connect to the address and port with our socket */
returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

if (returnStatus == 0) {
fprintf(stderr, "Connect successful!\n");
}
else {
fprintf(stderr, "Could not connect to address!\n");
close(simpleSocket);
exit(1);
}





/* get the message from the server */



do {

printf("get numb of data or terminate(write '0'): ");
fgets(buffsend,256,stdin);
n=atoi(buffsend);
if(n>6) {
printf("Error\n");

}
else {
for(i=0;i<n;i++) {
printf("insert number: ");
fgets(buffsend,256,stdin);
write(simpleSocket, buffsend, strlen(buffsend)+1);


}



} while((n!=0) && (n>0));





close(simpleSocket);
return 0;

}

这对于服务器来说:

#include <stdio.h>      
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

int simpleSocket = 0;
int simplePort = 0;
int returnStatus = 0;
char buff[256];
char message[256];
int count;

struct sockaddr_in simpleServer;

if (2 != argc) {

fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);

}

simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (simpleSocket == -1) {

fprintf(stderr, "Could not create a socket!\n");
exit(1);

}
else {
fprintf(stderr, "Socket created!\n");
}

/* retrieve the port number for listening */
simplePort = atoi(argv[1]);

/* setup the address structure */
/* use INADDR_ANY to bind to all local addresses */
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
simpleServer.sin_port = htons(simplePort);

/* bind to the address and port with our socket */
returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer));

if (returnStatus == 0) {
fprintf(stderr, "Bind completed!\n");
}
else {
fprintf(stderr, "Could not bind to address!\n");
close(simpleSocket);
exit(1);
}

/* lets listen on the socket for connections */
returnStatus = listen(simpleSocket, 5);

if (returnStatus == -1) {
fprintf(stderr, "Cannot listen on socket!\n");
close(simpleSocket);
exit(1);
}

while (1)

{

struct sockaddr_in clientName = { 0 };
int simpleChildSocket = 0;
int clientNameLength = sizeof(clientName);

/* wait here */

simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength);

if (simpleChildSocket == -1) {

fprintf(stderr, "Cannot accept connections!\n");
close(simpleSocket);
exit(1);

}

/* handle the new connection request */
/* write out our message to the client */


while(read(simpleChildSocket, buff, 256))
cont++;
printf("%s", buff);
printf("Data received: %d %s\n", cont, buff);



close(simpleChildSocket);
}

close(simpleSocket);
return 0;

}

问题出在“for”cicle上,因为每次我插入一个数字时,服务器的缓冲区都会被清空,并且只打印最后收到的数字。如果您提供解决方案,谢谢。

最佳答案

在读取调用中,您可能会得到您提到的整个或部分字符串(4 11 21 9 11)

每次读取调用后,您需要解析收到的数据,检查您是否获得了整个字符串或部分字符串,如果您获得了部分字符串,请再次读取以获取其余数据。确认获得完整数据后,您需要调用 write 将 4 发送回您的客户端

然后返回再次读取通话

关于c - 发送具有多个数字的唯一字符串客户端/服务器 TCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43895523/

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