David Franz
David Franz

Reputation: 5

Reason for connection error in socket programming (on client side)?

I'm having issues with my connect() method on the client side of my socket programming. I'm not sure if the issue is with my code or my method of running it. I'm running it in two seperate terminal windows - one for the server (which I'm running first) with the command './server 8080' and one for the client with the command './client 4 8080 hello'. When I run my code, the server program stops in the while loop just after the printf("this prints\n") line. I presume this means that it is waiting for a client to connect to it. The client program fails on the connect() call, and prints out my error message "Connection Failed". My code is posted below.

Server Code:

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

#define bufsize 1024

void eatZombies(int n){
    wait3(NULL,WNOHANG,NULL); // Nom Nom 
}

int main(int argc, char *argv[]){
    int sock, length, msgsock, status;
    struct sockaddr_in server;
    pid_t id;       
    signal(SIGCHLD, &eatZombies);
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(atoi(argv[1])); // this time 1st arg is port#
    if(bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0){
        printf("Error binding the socket\n");
        exit(0);
    }

    if(listen(server_fd, SOMAXCONN) < 0){
        printf("Error listening for connections\n");
        exit(0);
    }
    char buffer[1024] = {0}; 
    char *hello = "Hello from server"; 
    int addrlen = sizeof(server);    
    while(1){
        printf("this prints\n");
        int client_fd = accept(server_fd, (struct sockaddr *)&server, (socklen_t*)&addrlen);
        printf("this doesnt\n");
        if(client_fd < 0){
            printf("Error accepting connection\n");
            exit(0);
        }
        // the next call makes a new child process that will actually handle the client.
        id = fork();
        // when id == 0, this is the child and needs to do the work for the server. 
        // when if > 0, this is the parent, and it should just loop around,
        // when id < 0, we had an error.
        if(id > 0){
            continue;
        }
        else if(id < 0){
            printf("Error\n");
            exit(0);
        }  
        read(client_fd, buffer, 1024); 
        printf("%s\n", buffer); 
        write(client_fd, hello, strlen(hello), 0); 
        printf("Hello message sent\n");
        exit(0);
    }
    return 0;
}

Client Code:

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

#define bufsize 1024

int main(argc, argv) int argc; char *argv[];{
    int sock, rval;
    struct hostent *host;
    struct sockaddr_in server;  // not a pointer
    char buf[bufsize];
    printf("%d\n", argc);
    if(argc != 4){
        printf("usage:\ntcpclient hostname port string\n\n");
        return(-1);
    }
    // look up hostname (server) using DNS
    if ((host = gethostbyname(argv[1])) == 0) {
        fprintf(stderr, "%s: unknown host\n", argv[1]); 
        return(-1);  
    }
    // Set up fields for socket to point to host and port
    bcopy(host->h_addr, &server.sin_addr, host->h_length);
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    // Create socket 
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock < 0){
        printf("Socket Creation Failed\n");
        exit(0);
    }
    // connect (3-way handshake)
    if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0){ 
        printf("Connection Failed\n"); 
        exit(0); 
    } 
    // Copy the arg into buf so we can send it to the server
    strncpy(buf, argv[3], bufsize);
    // Send sentence to server
    send(sock, buf, strlen(buf), 0);
    printf("Message sent\n");
    // read response from server
    rval = read(sock, buf, bufsize);
    // print result to window
    fprintf(stdout,"%s\n", buf);
    close(sock);
}

Upvotes: 0

Views: 2279

Answers (1)

Jeroen ter Hofstede
Jeroen ter Hofstede

Reputation: 58

When running ./client 4 8080 hello, 4 is the host name. You meant to call ./client localhost 8080 hello.

So it was just a mistake in calling the application, not in the code.

Upvotes: 3

Related Questions