4

I am completely new to programming in unix and have written the following code for client and server programming. When I try to run the client code it says "Connection refused". Could somebody please tell me what could be the reason of it.

Server Code :

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

int main(void)
{
    int sockid,newsockid;
    socklen_t addr_size;
    char *msg="What a beautiful morning!";
    int len, bytes_sent;
    sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid==-1)
    {
        perror("socket");
        exit(1);
    }
    else
        printf("created");
    struct sockaddr_in serveraddr,clientaddr;
    bzero((char *)&serveraddr,sizeof(serveraddr));
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(7400);
    serveraddr.sin_addr.s_addr=INADDR_ANY;

    if(bind(sockid,(struct sockaddr *)&serveraddr,sizeof(serveraddr))<0)
    {
        perror("bind");
        return -1;
    }

    listen(sockid,5);
    addr_size=sizeof(clientaddr);
    newsockid=accept(sockid,(struct sockaddr *)&clientaddr,&addr_size);
    len = strlen(msg);
    bytes_sent = send(sockid, msg, len, 0);
    close(sockid);
}

Client Code :

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

int main(void)
{
    int byte_count;
    struct sockaddr_in serveraddr;
    char *servername;
    char buf[256];
    socklen_t addr_size;
    int sockfd;

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    bzero(&serveraddr,sizeof(serveraddr));
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(11378);
    servername=gethostbyname("localhost");
    inet_pton(AF_INET,servername,&serveraddr.sin_addr);

    addr_size=sizeof(serveraddr);
    if(connect(sockfd,(struct sockaddr *)&serveraddr,addr_size)==-1)
    {
        perror("connect");
        exit(1);
    }

    byte_count = recv(sockfd, buf, sizeof buf, 0);
    printf("recv()'d %d bytes of data in buf\n", byte_count);

    close(sockfd);
}

An early help would be appreciated. Thanks.

dragosht
  • 3,237
  • 2
  • 23
  • 32
user2725511
  • 43
  • 1
  • 1
  • 5
  • `Connection refused` means "*no such phone number, the number you dialed is unknown*" in terms of ip-address and/or port-number. – alk Aug 28 '13 at 13:27
  • 1
    Double check how `servername` is initialised. And: **Do** compile with **all warnings on** (`-Wall -Wextra -pedantic` for gc) then **fix** the code **until no more warnings** pop up! – alk Aug 28 '13 at 13:31
  • i've made a few changes.. disregard the changes of port no. Is there anything else that I need to do? – user2725511 Aug 28 '13 at 16:14
  • There is a conceptual problem: The client's call to `recv()` would block as its trying to receive more than the server will be sending. – alk Aug 28 '13 at 16:22
  • 1
    "*An early help would be appreciated*": I could send you my hourly rate, if you are interested. – alk Aug 28 '13 at 16:28
  • I wrote this, to (in an ironical way) indicate, that I have the impression you aren't well organised. How I get this idea: Why is a learner in a hurry? The only answer I find is: He/she is late with homework. However I should have added a wink to my previous comment. I obviously missed it, and I'm sorry for this, so here it is: ;-) – alk Aug 28 '13 at 16:42
  • I'm sorry I didn't get it at first but I'm really stuck here and that comment kinda pissed me off. I'm sorry for the comment though. – user2725511 Aug 28 '13 at 16:46

4 Answers4

4
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(7400);
inet_pton(AF_INET,servername,&serveraddr.sin_addr); // here

You're passing servername to inet_pton() but it is not initialized ! So inet_pton() will fail. You should check its return value.

servername=gethostbyname(); //here
addr_size=sizeof(serveraddr);

The second problem is that you're not using gethostbyname() correctly.Take a look at the manpage, you will see that gethostbyname() is taken arguments and it returns a pointer to a struct hostent, not a pointer to char like you did. Your compiler doesn't warn you about this because you don't include netdb.h.

You should check the return values of all the functiond that you are using, it's avoid problems like that. You should enable some flags of your compiler (like alk said in the question comments, -W -Wextra -Wall -pedantic are really great flags).

nouney
  • 4,363
  • 19
  • 31
  • I have rectified both of the mistakes that you asked me to.. It's still not working. Yet again it shows "Connection refused". – user2725511 Aug 28 '13 at 14:06
  • 1
    Okay.. The updated code is such.. sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&serveraddr,sizeof(serveraddr)); serveraddr.sin_family=AF_INET; serveraddr.sin_port=htons(11378); servername=gethostbyname("localhost"); inet_pton(AF_INET,servername,&serveraddr.sin_addr); – user2725511 Aug 28 '13 at 14:19
  • 1
    Please update the code **in your question** (don't post the updated code in a comment, it's hard to read it :) ) – nouney Aug 28 '13 at 14:32
  • i have changed the port no. in both the codes to be 11378.. kindly ignore that change. – user2725511 Aug 28 '13 at 14:35
  • I would sincerely appreciate it if the help could be provided early. – user2725511 Aug 28 '13 at 14:57
  • 2
    Add some error checking to your code, and read [this question](http://stackoverflow.com/questions/2865583/gethostbyname-in-c) about `gethostbyname("localhost")`. – nouney Aug 28 '13 at 15:01
  • I tried the same code earlier as from the question you asked me to refer to and it showed an error as I am storing the value in "servername" here.. if i change the type to struct hostent *, then the whole context in the program will have to be changed. Would that not be wrong? – user2725511 Aug 28 '13 at 15:08
  • @user2725511 Oh yes that's right I didn't even notice that ... `gethostbyname()` returns indeed a pointer to a `struct hostent`. Take a look at the the [Beej's tutorial](http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html), there is some examples and explanations. – nouney Aug 28 '13 at 15:17
  • I changed the type of "servername" to struct hostent * and in the following lines wherever "servername" was used, I changed it to "servername->h_name" but still no success. What do I do now? – user2725511 Aug 28 '13 at 15:25
  • 2
    @user2725511: "*but still no success*" so what's the current error. Where are you? What is not working? – alk Aug 28 '13 at 16:52
  • Its the same error as before : "Connection refused". I dont know what is wrong with it. – user2725511 Aug 28 '13 at 17:44
  • @user2725511 Ok, please add some debug to your code (test the return value of the system calls and print the error message with `perror()`), update your question with this new code and we'll see. – nouney Aug 28 '13 at 19:37
  • Not to be that guy, but after reading this thread I feel it may be necessary to check...Are you running the server prior to running the client? Running the client first would result in consistent "Connection Refused" errors. – user3285763 Nov 11 '14 at 15:30
4

check the port. server port is 7400 but the client try to connect with port number 11378 so only connection has been refused.

Jeyamaran
  • 783
  • 1
  • 9
  • 16
1

Make sure to include errno.h in your program.
Add line printf("%s", strerr(errno)); in your program after any failure. Any system call failure sets correct errno making it simple to diagnose the problem.

NeilBlue
  • 173
  • 2
  • 9
0

Well, you have to be sure that your server is running and listening on proper port (7400). Just check with: "netstat -tanp" if there is something listening on port 7400 and then try to connect.

user2699113
  • 4,262
  • 3
  • 25
  • 43
  • 1
    Although this step is one of the common debugging steps done when connection problem are observed, in this case the problem is on the client side, as per the OP's sources. – alk Aug 28 '13 at 14:05
  • @alk More to the point, the server *isn't* running on port 11378. Whether this is a client or server error is debatable. – user207421 Feb 05 '14 at 09:15