gpt4 book ai didi

c - Socket发送二进制数据而不是文本数据

转载 作者:行者123 更新时间:2023-11-30 20:20:01 26 4
gpt4 key购买 nike

我有示例代码,用于使用 c 在 UNIX 上发送和接收有关套接字的数据。

这是服务器代码:

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

int main(int argc, char* argv[]) //server-side
{
int socket_desc, c, new_socket, val;
char reply[2];
//char* msg;
char *msg1, *msg2, *msg3;
msg1 = "Your number is less greater than the guessed number\n";
msg2 = "Your number is greater than the guessed number\n";
msg3 = "You guessed right\n";
struct sockaddr_in server, client;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);

srand(time(NULL));
int num = rand() % 99 + 1;

if(socket_desc < 0){
printf("Couldn't create socket\n");
return 1;
}

server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(8888);

if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0){
printf("Bind error\n");
return 1;
}
printf("Binding done\n");
listen(socket_desc, 3);
printf("Waiting for incoming connections\n");
c = sizeof(struct sockaddr_in);

printf("Accepted\n");
while(new_socket = accept(socket_desc, (struct sockaddr *)&client,(socklen_t*) &c)){
recv(new_socket, reply, strlen(reply), 0);
val = snprintf(reply, strlen(reply), "%d", val);

if(num < val){
send(new_socket, msg1, sizeof(msg1), 0);
} else if(num > val){
send(new_socket, msg2, sizeof(msg2), 0);
} else{
send(new_socket, msg3, sizeof(msg3), 0);
}
//send(new_socket, reply, sizeof(reply), 0);
}

return 0;
}

这是客户端代码:

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

int main(int argc, char* argv[]) //client-side
{
int socket_desc, num;
char data[2];
char serv_reply[50];
struct sockaddr_in server;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);

if(socket_desc < 0){
printf("Couldn't create socket\n");
return 1;
}

server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(8888);

//int nread = 23;
//write(socket_desc, &nread, sizeof(nread));

int conn = connect(socket_desc, (struct sockaddr *)&server, sizeof(server));
if(conn == -1){
printf("Cannot establish a connection\n");
return 1;
}
printf("Connected\n");

printf("Enter a number:\n");
scanf("%d", &num);
sprintf(data, "%d", num);
if(send(socket_desc, data, strlen(data), 0) < 0){
printf("Send was unsuccessful\n");
return 1;
}
puts("Data sent\n");
if(recv(socket_desc, serv_reply, strlen(serv_reply), 0) < 0){
printf("Cannot receive data\n");
return 1;
}
puts(serv_reply);
return 0;
}

我希望看到一条消息,指示随机数是否小于、大于或等于输入数字。但我看到的是:

Send compilation result

感谢您的帮助,在你们提出问题之前,这不是作业,只是了解 UNIX 中套接字的基本练习。

最佳答案

这里:

send(new_socket, msg1, sizeof(msg1), 0);

sizeof(msg) 是指针的大小。传输 strlen(msg1) + 1 或(更好)声明 msg1 et al 因此:

const char msg1[] = "Your number is less greater than the guessed number\n";
const char msg2[] = "Your number is greater than the guessed number\n";
const char msg3[] = "You guessed right\n";

这里还有:

if(recv(socket_desc, serv_reply, strlen(serv_reply), 0) < 0){
printf("Cannot receive data\n");
return 1;
}
puts(serv_reply);

strlen(serv_reply) 未定义,因为 serv_reply 尚未初始化。在这里,您确实需要sizeof(serv_reply)。然后,您将输出收到的任何内容,无论其长度如何,也无论它是否为 nul 终止。

您需要输出接收到的片段,直到没有收到任何内容为止,并且不要假设 nul 终止。例如:

int received = 0 ;
do
{
received = recv( socket_desc,
serv_reply,
sizeof(serv_reply) - 1,
0 ) ;
if( received > 0 )
{
serv_reply[received] = '\0' ;
printf( "%s", serv_reply ) ;
}
} while( received > 0 ) ;

关于c - Socket发送二进制数据而不是文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47640847/

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