Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket Programming in C(Client Server Example)

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.

like image 231
user2725511 Avatar asked Mar 23 '23 14:03

user2725511


1 Answers

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).

like image 190
nouney Avatar answered Mar 27 '23 12:03

nouney