Question

I am new to network programming, and have been learning this by writing small programs that make use of the Socket API. Currently, I am writing a simple echo server, that uses fork to create a copy of it, as soon as it gets a connect request, this adds up as in improvement over the previous Iterative server (here). However, after I start the server and fire up the client, and type a message on its console, it quits unexpectedly. Running the program under Gdb shows that SIGPIPE was delivered. But as far as I know as the socket is still valid, a SIGPIPE shouldn't have occured. Any kind of help involved is appreciated. Here is the client code

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd,i;
    struct sockaddr_in saddr;
    char buff[MAXCOUNT];
    char mesg[MAXCOUNT];
    sfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = AF_INET;
    inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
    saddr.sin_port = htons(5008);
    connect(sfd,(struct sockaddr*) &saddr,sizeof(saddr));
    fgets(buff,MAXCOUNT,stdin);
    send(sfd,buff,strlen(buff),0);
    if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
        perror("Nothing to read\n");
        exit(1);
    }
    printf("%s\n",mesg);
    exit(0);    
}

Here is the server code

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

#define MAXCOUNT 1024

int main(int argc, char* argv[])
{
    int sfd,nsfd,cn;
    pid_t c;
    char buf[MAXCOUNT];
    socklen_t clen;
    struct sockaddr_in caddr,saddr;

    sfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&saddr,0,sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    saddr.sin_port = htons(5008);

    bind(sfd,(struct sockaddr*) &saddr,0);

    listen(sfd,2);
    for (; ;) {
        clen = sizeof(caddr);
        nsfd = accept(sfd,(struct sockaddr*) &caddr, &clen);
        if( (c = fork()) == 0) {
            close(sfd);
            memset(buf,0,sizeof(buf));
            cn = recv(nsfd,buf,sizeof(buf),0);
            if ( cn == 0) {
                perror("Reading from the client socket failed\n PROGRAM CRASH :\n");
                exit(1);
            }
            buf[cn] = '\0';
            send(nsfd,buf,strlen(buf),0);
            close(nsfd);
            exit(0);
        }
    }
    return 0;
}
Was it helpful?

Solution

send(sfd,buff,strlen(buff),0);
if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
    perror("Nothing to read\n");
    exit(1);
}
printf("%s\n",mesg);

The %s format specifier is for C-style strings, not arbitrary data. And since you throw away the return value from recv, you have no way to know how many bytes you got.

Your client also doesn't shut down the socket gracefully or make sure it has received all the data the server may send. So it's possible that you're triggering an abnormal termination. The server closes the connection when it's done sending, so the client should keep trying to receive until it detects that the connection has closed.

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