Home > Enterprise >  Error in running a client-server socket program in c
Error in running a client-server socket program in c

Time:12-03

I am new to socket programming. I am trying to run the client and server code and send messages from client to server and vice versa but I am facing an error where the connection of the client to the server is refused.
This is the error message:

Connection Failed: Connection refused

Here is the code:

Server

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

void error(const char *err){
    perror(err); //error function that takes error number and outputs text description
    exit(1);
}

int main(int argc, char *argv[]){ //argc = number of parameters (2 in our case which is filename and port number) 
                                 //argv will contain tthe filename and port number
    if (argc < 2){
        fprintf(stderr, "Port number not provided. Program Terminated\n");
        exit(1);
    }
    int sockfd, newsockfd, portno, n;
    char buffer[255];
    struct sockaddr_in serv_addr, cli_addr;
    socklen_t clilen;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0){
        error("Error opening Socket");
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sockfd,(struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
        error("Binding Failed");
    }

    listen(sockfd, 5);
    clilen = sizeof(cli_addr);

    newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);

    if (newsockfd < 0){
        error("Error on Accept");
    }

    while(1){
        bzero(buffer, 255);
        n = read(newsockfd, buffer, 255);
        if(n < 0){
            error("Read Failed");
        }
        printf("Client: %s\n", buffer);
        bzero(buffer, 255);
        fgets(buffer, 255, stdin);

        n = write(newsockfd, buffer, strlen(buffer));
        if(n < 0){
            error("Write Failed");
        }
        int i = strncmp("Exit", buffer, 4);
        if(i == 0){
            break;
        }
    }
    close(newsockfd);
    close(sockfd);
    return 0;
}

Client

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

void error(const char *err){
    perror(err); //error function that takes error number and outputs text description
    exit(1);
}

int main(int argc, char *argv[]){
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    
    char buffer[255];
    if(argc < 3){
        fprintf(stderr, "usage %s hostname port\n", argv[0]);
        exit(1);
    }

    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0){
        error("ERROR opening socket");
    }
    server = gethostbyname(argv[1]);
    if(server == NULL){
        fprintf(stderr,"Error, No Such Host");
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *) server->h_addr , (char *) &serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    if(connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr))<0){
        error("Connection Failed");
    }
    while (1)
    {
        bzero(buffer, 255);
        fgets(buffer, 255, stdin);
        n = write(sockfd,buffer, strlen(buffer));
        if(n < 0){
            error("Write Failed");
        }
        bzero(buffer, 255);
        n = read(sockfd, buffer, 255);
        if(n < 0){
            error("Read Failed");
        }
        printf("Server: %s",buffer);

        int i = strncmp("Exit", buffer, 4);
        if(i == 0){
            break;
        }
    }
    close(sockfd);
    return 0;

}

To run the server code I used this command:
./server 9999

To run the client code I used this command:
./client 127.0.0.1 9999

CodePudding user response:

In the server program, you forgot to set serv_addr.sin_port before calling bind.

You should change the lines

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;

to:

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
  • Related