Oyeniyi Abiola Peace
Oyeniyi Abiola Peace

Reputation: 421

My TCP socket server not sending and receiving

I am trying to write a TCP socket server for a client then i ran into some problems. The server is not sending/receiving data. However, it can listen to and accept new clients.

I have tried "nc 127.0.0.1 -l 10001" to test the client and it works well. The server at one time could send and receive but encoded characters. I made some modifications and since then i have been seeing errors.

The exit Message can be commented or remove as it was declared in custom.h.

Any idea will be appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#include <arpa/inet.h>
#include <netinet/in.h>

#include "custom.h"

#define PORT 10001
#define MAXIMUM_CONNECTION 20
#define DATA_SIZE 100

int main() 
{
    int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct sockaddr_in serverAddr, clientAddr;
    memset(&serverAddr, '\0', sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
        exitMessage("Failed to address to port");
    }

    if(listen(sock, MAXIMUM_CONNECTION) < 0) {
        exitMessage("Server is deaf could not listen to incoming messages");    
    }

    char buffer[DATA_SIZE], data[DATA_SIZE];

    while(1) {
        memset(buffer, '\0', DATA_SIZE);
        memset(data, '\0', DATA_SIZE);

        socklen_t clientAddrSize = sizeof(clientAddr);

        if(accept(sock, (struct sockaddr *) &clientAddr, &clientAddrSize) < 0) {
            exitMessage("Could not accept new connections");
        }

        if(recv(sock, buffer, DATA_SIZE - 1, 0) < 0) {
            exitMessage("Failed to receive data from client");      
        }

        printf("\nReceived: \'%s\' to client\n", buffer);
        printf("input data for client: ");

        int index = 0;
        while((data[index++] = getchar()) != '\n');

        if(send(sock, data, strlen(data), 0) < 0) {
            exitMessage("Failed sending to client");
        }
    }
}

Upvotes: 1

Views: 449

Answers (1)

dbush
dbush

Reputation: 223689

The socket you listen on is only for accepting new connections, not reading/writing data. The accept function returns a file descriptor for the accepted socket. That is the one you should be calling recv and send on.

int newsock;
if((newsock = accept(sock, (struct sockaddr *) &clientAddr, &clientAddrSize)) < 0) {
    exitMessage("Could not accept new connections");
}

if(recv(newsock, buffer, DATA_SIZE - 1, 0) < 0) {
    exitMessage("Failed to receive data from client");      
}

printf("\nReceived: \'%s\' to client\n", buffer);
printf("input data for client: ");

int index = 0;
while((data[index++] = getchar()) != '\n');

if(send(newsock, data, strlen(data), 0) < 0) {
    exitMessage("Failed sending to client");
}

Upvotes: 2

Related Questions