套接字发送二进制数据而不是文本数据

时间:2017-12-04 19:44:25

标签: c sockets

我有使用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中套接字的基本练习。

1 个答案:

答案 0 :(得分:1)

下面:

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

sizeof(msg)是指针的大小。请传输strlen(msg1) + 1或(更好)声明msg1

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)。然后,无论其长度如何,无论是否为空终止,您都会输出收到的任何内容。

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

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 ) ;