gpt4 book ai didi

c - 发送包含多个数字的字符串 客户端/服务器 TCP

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:20 24 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;

}

最佳答案

问题在于客户端在每个值之后发送一个字符串终止符 (\0),因此当服务器打印整个消息时,它在第一个值之后停止打印。

这个特殊问题可以通过在发送时不包括字符串终止符来解决:

      write(simpleSocket, buffsend, strlen(buffsend)); /* Removed the +1 */

接收消息时需要添加字符串结束符:

    int length;
char buff[256];

while((length = read(simpleChildSocket, buff, 256-1)) > 0) {
buff[length] = `\0`;
cont++;
printf("Data received: %d %d %s\n", cont, length, buff);
}

另请注意,您正在使用基于流的套接字。这意味着套接字本身没有消息协议(protocol)的概念。参见 this question获取更多信息。

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

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