Question

I have this simple client-server written in C below. The client sends 2 numbers to the server and the server sends back to the client the sum of the 2 numbers. My problem is with sending back the sum from server to the client, I can't see why it does not work. it doesn't give any error, just the receivedData variable is not filled with the desired int. The client successfully sends the packet with the 2 numbers to the server and the server successfully receives them.

Please note that this is just a didactic example, and I didn't use threads for handling clients. It's just one client and one server.

Here is the server:

// SERVER
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

#define PORT 2012

struct Packet
{
    int a;
    int b;
};


int main()
{
    struct sockaddr_in clientAddress;
    int sd;

    if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("Error creating socketsss\n");
    }

    memset(&clientAddress, 0, sizeof(clientAddress));

    clientAddress.sin_family = AF_INET;
    clientAddress.sin_port = htons(PORT);
    clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(sd, (struct sockaddr*)&clientAddress, sizeof(clientAddress)) < 0)
    {
        printf("Bind error\n");
    }

    if(listen(sd, 5) < 0)
    {
        printf("Listen error\n");
    }

    unsigned int len = 0;

    int fd = accept(sd, (struct sockaddr*)&clientAddress, &len);

    struct Packet received;

    if(recv(fd, &received, sizeof(struct Packet), 0) < 0)
    {
        printf("Receive error");
    }

    int sum = received.a + received.b;

    printf("Numarul ce trebui trimis este %d\n", sum);

    if(send(sd, &sum, sizeof(sum), 0) < 0)
    {
        printf("Error sending data\n");
    }

    return 0;
}

And here is the client:

// CLIENT
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SERVER_IP "127.0.0.1"
#define PORT 2012

struct Packet
{
    int a;
    int b;
};

int main()
{
    struct sockaddr_in serverAddress;
    int sd;

    if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf("Error creating socket\n");
    }

    memset((char *)&serverAddress, 0, sizeof(serverAddress));

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
    serverAddress.sin_port = htons(PORT);

    if ((connect(sd,(struct sockaddr *)&serverAddress, sizeof(serverAddress))) < 0)
    {
        printf("Error connectiong to server!\n");
    }

    struct Packet packToSend;
    packToSend.a = 14;
    packToSend.b = 21;

    if(send(sd, &packToSend, sizeof(packToSend), 0) < 0)
    {
        printf("Error sending data\n");
    }

    int receivedData = 0;

    if(recv(sd, &receivedData, sizeof(int), 0) < 0)
    {
        printf("Receive error\n");
    }

    printf("I received %d \n", receivedData);

    return 0;
}

Thanks!

Was it helpful?

Solution

You are sending on the listening socket, not the one you accepted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top