I have a client /server file operation program.It works properly when i run the client and server program in the same system.but when i try to run the client in one system and server in another system i am getting an error in the cleint machine as "ERROR:Connection refused". Plz help me in this

server coding:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include<time.h>
#include<sys/time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
char pass[100];
char pass1[100];

int main()
{
    int sockfd, new_fd, numbytes;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr;
    socklen_t sin_size;
    socklen_t *size;
    char opr[2];
    FILE *f, *t;
    time_t ti;
    struct sockaddr *names;
    struct timeval tv;
    struct timezone tz;
    char *times;
    int yes = 1, len = 0;
    struct sigaction sa;
    char s[INET6_ADDRSTRLEN], s1[INET6_ADDRSTRLEN];
    char name[20];
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((rv = getaddrinfo(NULL, "2001", &hints, &servinfo)) == -1) {
	fprintf(stderr, "getaddrinfo:%s", gai_strerror(rv));
	return 1;
    }
    printf("\n \n getaddrinfo:%d", rv);
    printf("\n------------------------------");
    for (p = servinfo; p != NULL; p = p->ai_next) {

	if ((sockfd =
	     socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
	    perror("server:socket");
	    continue;
	}
	printf("\n server socket launched...");

	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))
	    == -1) {
	    perror("setsockopt");
	    return 0;
	}
	printf("\n server setsocket option ...");

	if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {

	    close(sockfd);
	    perror("server:bind");
	    continue;
	}
	printf("\n socket binded....");
	break;
    }


    if (p == NULL) {
	fprintf(stderr, "server:failed");
	return 2;
    }
    if (listen(sockfd, 10) == -1) {
	perror("listen");
	return 0;
    }
//sa.sa_handler=sigchld_handler;
//sa.sa_flags=SA_RESTART;
/*if(sigaction(SIGCHLD,&sa,NULL)==-1)
{
perror("sigaction");
exit(0);
}*/
    printf("\n\nlisten to socket :%d", sockfd);

    sin_size = sizeof their_addr;
    new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
    printf("\n sockfd=%d new_fd=%d", sockfd, new_fd);
    if (new_fd == -1) {
	perror("accept");
    }
//inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s,sizeof s);
//printf("\n\nserver:connected %s",s);
    int count = 0;
    if ((send(new_fd, "start", 5, 0)) == -1)
	perror("ww");
    printf("\n started operation");
    printf("\n The operation is ");
    if ((recv(new_fd, pass, sizeof pass, 0)) == -1)
	perror("r");
    printf("%s", pass);
    if ((strcmp(pass, "1")) == 0) {
	int i;
	printf("\n hai");
	f = fopen("file.txt", "a");
	if ((recv(new_fd, pass1, sizeof pass1, 0)) == -1)
	    perror("err");
	if ((i = gettimeofday(&tv, &tz)) == -1)
	    perror("y");
	ti = tv.tv_sec;
	times = ctime(&ti);
	fprintf(f, "%s %s", pass1, times);

	fclose(f);
    } else {
	char op[2];
	char i;
	printf("\n hello");
	f = fopen("file.txt", "r");

	while (fgets(pass, 100, f) != NULL)
	    count++;
	op[0] = count + 48;
	op[1] = '\0';
	printf("no of lines:%s", op);
	if ((send(new_fd, op, sizeof op, 0)) == -1)
	    perror("re");
	fseek(f, 0, SEEK_SET);
	count = 0;
	while (fgets(pass, 100, f) != NULL) {
	    if ((send(new_fd, pass, 100, 0)) == -1)
		perror("l");
	}



	fclose(f);
    }
    close(new_fd);

    close(new_fd);
    return 0;
}

client coding

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

void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
	return &(((struct sockaddr_in *) sa)->sin_addr);
    }
    return &(((struct sockaddr_in6 *) sa)->sin6_addr);
}

char buff[100];
char buf[100];

int main(int argc, char *argv[])
{
    int sockfd, new_fd, numbytes;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr;
    int yes = 1, len = 16;
    char names[20];
    char opr[2];
    char s[INET6_ADDRSTRLEN];
    int rv;
    struct sockaddr *name;
    socklen_t size;
    if (argc != 2) {
	fprintf(stderr, "usage:client hostname");
	exit(1);

    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(NULL, "2001", &hints, &servinfo)) == -1) {
	fprintf(stderr, "getaddrinfo:%s", gai_strerror(rv));
	return 1;
    }

    for (p = servinfo; p != NULL; p = p->ai_next) {

	if ((sockfd =
	     socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
	    perror("\n client:socket");
	    continue;
	}
	printf(" sockeyt %d", sockfd);

	if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
	    close(sockfd);
	    perror("\nclient:connect");
	    continue;
	}

	break;
    }


    if (p == NULL) {
	fprintf(stderr, "client:failed");
	return 2;
    }

    if ((rv = getpeername(sockfd, name, &size)) == -1)
	perror("r");
    strcpy(names, argv[1]);

    inet_ntop(p->ai_family, get_in_addr((struct sockaddr *) p->ai_addr), s,
	      sizeof s);
    int count = 2;
    if (recv(sockfd, buf, sizeof buf, 0) == -1)
	perror("ee");
    printf("%s", buf);
    printf
	("\n Enter the operation to be performed:\n1) To store\n 2)To reterieve:\n ");
    scanf("%c", &opr[0]);
    opr[1] = '\0';
    len = 2;
    if ((send(sockfd, opr, len, 0)) == -1)
	perror("err");
    if (opr[0] == '1') {
	printf("\n Enter the text:");
	scanf("%s", buff);

	len = strlen(buff);
	buff[len] = '\0';
	if ((send(sockfd, buff, len + 1, 0)) == -1)
	    perror("e");

    } else {
	int count = 0;
	if ((recv(sockfd, buf, sizeof buf, 0)) == -1)
	    perror("p");
	printf("\n The no of lines in file is %s", buf);
	count = atoi(buf);
	while (count != 0) {
	    if ((recv(sockfd, buff, sizeof buff, 0)) == -1)
		perror("k");
	    printf("\n%s", buff);
	    count--;
	}

    }

    printf("over");

    return 0;
}

Hi,

I try to write a small socket program in my Linux machine.
I go on the Beej's guide but I cant compile the sample because, i have not the function:
get_in_addr(..........) in my Linux.
What's the problem you think?

This article has been dead for over six months. Start a new discussion instead.