Tushar Maroo
Tushar Maroo

Reputation: 345

Connection refused error in socket programming

This code is generating "Connection Failed error", (the error generating portion is commented below in the code) even when i am supplying the correct input format eg.

./Client ip text portno

./Client 127.0.0.1 "tushar" 7100

//AUTHOR: TUSHAR MAROO
    //Client.c
    //header files used
    #include <stdio.h> 
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <netinet/in.h>


//constants
#define RCVBUFFERSIZE 32
//functions used
void DieWithError(char *errorMessage);

//main program
int main(int argc, char *argv[]){
int sock;
struct sockaddr_in serverAddr;
unsigned short serverPort;
char *serverIp;
char *message;
unsigned int messageLength;
char buffer[RCVBUFFERSIZE];

//condition check deplyed for nuber of arguements not for data in arguements
if((argc<3) || (argc>4)){
    fprintf(stderr,"Format: %s <Server's IP> <Your Message> <Port Number>\n",argv[0]);
    exit(1);
}

serverIp = argv[1];
message = argv[2];

if(argc == 4){
    serverPort = atoi(argv[3]);
} else {
    serverPort = 7;
}

//create a socket and check success and handle error
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
    fprintf(stderr, "Socket Creation Fail");

//server details
//bzero((struct sockaddr_in *)(&serverAddr),sizeof(serverAddr));
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(serverIp);
serverAddr.sin_port = htons(serverPort);
printf("tusharmaroo");

//not working why??
//if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    //DieWithError("Connection Error..");
    //fprintf(stderr,"Connection error");
//this snippet also not working 
if (connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    DieWithError("connect() failed");

printf("connected....");
messageLength = strlen(message);
if(send(sock, message, messageLength, 0) > 0)
    printf("message sent....");
    
    
close(sock);    
exit(0);
}

//AUTHOR TUSHAR MAROO
//SERVER CODE
//header files
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>

//constants declared
#define ALLOWEDCONNECTIONS 5

//external functions
void DieWithError(char *error);
void ClientHandle(int sock);

//main code
int main(int argc, char argv[]){

    int serverSock;
    int clientSock;
    struct sockaddr_in serverAddr;
    struct sockaddr_in clientAddr;
    unsigned int serverPort;
    unsigned int clientLength;
    
    if(argc != 2){
        fprintf(stderr,"Format: %d <Port No.>", argv[0]);
        //DieWithError("Pass Correct Number of Arguements...");
        exit(1);
    }
    
    if((serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        DieWithError("Socket not Created");
        exit(1);
    }
    
    serverPort = htons((argv[1]));
    //assign address to the server
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = htons(serverPort);
    
    //socket has been created now bind it to some ip and port
    if((bind(serverSock,(struct sockaddr *)&serverAddr,sizeof(serverAddr))) < 0){
        DieWithError("Binding Failed");
    }
    
    if(listen(serverSock,5) < 0){
        DieWithError("Listen Failed");
    }
    for(;;){
        clientLength = sizeof(clientAddr);
        if((clientSock = accept(serverSock, (struct sockaddr *) &clientAddr, &clientLength)) < 0){
            DieWithError("Accept() failed");
            exit(1);
        }
        
        printf("Handling Client %s ",inet_ntoa(clientAddr.sin_addr));
    
    }
return 0;   
}

Upvotes: 3

Views: 8888

Answers (3)

Tanmoy Bandyopadhyay
Tanmoy Bandyopadhyay

Reputation: 985

This is wrong in the server code

 serverPort = htons((argv[1]));

This should be

serverPort = htons(atoi(argv[1]));

Upvotes: 1

Vorsprung
Vorsprung

Reputation: 34397

works for me

client and server are ubuntu 12.04

for server, run in a shell

nc -l 9999

This is on a host with the address "192.168.56.13"

for client, compile code above with "DieWithError" fixed up

void DieWithError(char *errorMessage) {  printf("%s",errorMessage); exit(1); }

cc -o foo foo.c
./foo 192.168.56.13 "hello" 9999</strike>

replace the DieWithError() with perror() Then I would guess that it will print out "connection refused" as you seem to have a networking problem with getting the server running on the correct address.

However, if the address in your client is correct the nc program WILL print "hello"

you just altered your program the previous version worked for me. The current version, I don't know if it does.

Like everyone else is saying, use perror() to get proper diagnostics

Upvotes: 0

Paul Rubel
Paul Rubel

Reputation: 27242

Are you sure there are no firewall rules causing troubles for you? Ensure that.

If the connect fails you should be able to print out the error using perror or strerror:

perror("Could not connect:");

Upvotes: 0

Related Questions