如何从客户端到服务器发送整数数组?

时间:2019-03-28 12:52:51

标签: c server client

我不知道如何从客户端向服务器发送整数数组。由于某些原因,我在服务器端发送的固定数组在收到时将只写入数组的前两个值。因此,它适用于array的前两个值,但现在适用于其他值,我不知道为什么。

客户代码

#include<stdio.h>      //printf
#include<string.h>     //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h>  //inet_addr
#include <fcntl.h>     //for open
#include <unistd.h>    //for close

#include <sys/types.h>  //sendto
#include <sys/socket.h> //sendto

#define DEFAULT_BUFLEN      512
#define DEFAULT_CLIENT_PORT     27016
#define DEFAULT_SERVER_PORT     27015
#define NUM_OF_PAR      7

int main(int argc, char *argv[])
{
    int sock;
    struct sockaddr_in server, client;
    char message[DEFAULT_BUFLEN];

        //Create socket
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock == -1)
        {
            printf("Could not create socket");
        }
        puts("Socket created");

        server.sin_addr.s_addr = inet_addr("ip_address");
        server.sin_family = AF_INET;
        server.sin_port = htons(DEFAULT_SERVER_PORT);

        //Connect to remote server
        if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            perror("connect failed. Error");
            return 1;
        }

        puts("Connected\n");

    int array[] = {1, 12, 3, 3, 300, 5, 3};

    printf("Sending array\n");
    for(int i = 0; i < NUM_OF_PAR; i++){
        printf("%d  ", array[i]);       
    }

    if(send(sock, array, NUM_OF_PAR, 0) < 0)    
    {
        printf("\nArray is not successfully sent\n");
    }
    else
    {
        printf("\nArray is sent\n");
    }

    printf("\n");

        close(sock);

        return 0;
}

服务器代码

#include<stdio.h>   //printf    
#include<string.h>  //strlen
#include<sys/socket.h>  //socket
#include<arpa/inet.h>   //inet_addr
#include<unistd.h>      //write

#include<stdlib.h>

#include <sys/types.h>  //recvfrom
#include <sys/socket.h> //recvfrom

#define DEFAULT_BUFLEN      512
#define DEFAULT_CLIENT_PORT     27016
#define DEFAULT_SERVER_PORT     27015
#define NUM_OF_PAR      7

int main(int argc , char *argv[])
{
    int socket_desc, client_sock, c, read_size;
    struct sockaddr_in server, client;
    char client_message[DEFAULT_BUFLEN];
    int sock;

    //Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
            printf("Could not create socket");
        }
        puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(DEFAULT_SERVER_PORT);

        //Bind
        if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            //print the error message
            perror("bind failed. Error");
            return 1;
        }
        puts("bind done");

        //Listen
        listen(socket_desc, 1);

        //Accept and incoming connection
        puts("Waiting for incoming connections...");
        c = sizeof(struct sockaddr_in);

        //accept connection from an incoming client
        client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
        if(client_sock < 0)
        {
            perror("accept failed");
            return 1;
        }
        puts("Connection accepted");


        //Receive a message from client
    int arrayReceived[NUM_OF_PAR];

    while((read_size = recv(client_sock, &arrayReceived, NUM_OF_PAR, 0)) > 0){
        printf("Received well\n");  
    }

    if(read_size == 0)
        {
            puts("Client disconnected");
        }
        else if(read_size == -1)
        {
            perror("recv failed");
        }

    printf("Received parameters:    \n");
    for(int i = 0; i < NUM_OF_PAR; i++)
    {
        printf("%d  ", arrayReceived[i]);
    }

    printf("\n");

        close(sock);

        return 0;
}

我想在服务器端写出我从客户端发送的所有数组值。

1 个答案:

答案 0 :(得分:0)

sendrecv期望size参数以字节为单位。

因此,使用NUM_OF_PAR就像只发送NUM_OF_PAR/4整数一样。 (一个整数是4个字节)

要纠正此问题,您可以编写:

/* using the sizeof of you array */

if(send(sock, array, sizeof array, 0) < 0)    

/* ... */

while((read_size = recv(client_sock, arrayReceived, sizeof array_received, 0)) > 0){

/* using the size of one element in the array times the size of one element*/

if(send(sock, array, sizeof * array * NUM_OF_PAR, 0) < 0)    

/* ... */

while((read_size = recv(client_sock, arrayReceived, sizeof *array_received * NUM_OF_PAR, 0)) > 0){