LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2009, 07:56 AM   #1
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Rep: Reputation: 0
Exclamation Problem with socket connection


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:
Code:
#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
Code:
#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;
}

 
Old 04-08-2009, 08:08 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
On your server side, you are not specifying an IP address (which later is used to bind your port)... at least that is what I gather when I examined your getaddrinfo(). Thus I bet your are binding to 127.0.0.1.

Hence your client, which I presume is being provided an IP address (e.g. 192.168.1.100) and port utilized by the server, is getting a "connection refused" simply because there is no server bound to that address.
 
Old 04-08-2009, 02:35 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by vigneshinbox View Post
It works properly when i run the client and server program in the same system.
You need to tell the server to use the INADDR_ANY address so that it listens for connections on all available network interfaces.

As dwhitney67 surmised, the first entry in your list is "localhost" or "127.0.0.1". If you only listen on that interface, you can only accept connections from processes running on the same machine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Connection Failed: 111 Connection Refused degraffenried13 Linux - General 3 05-31-2009 01:17 AM
connection between java socket and unix socket sathishkumar Programming 1 01-10-2006 03:21 PM
Socket connection refused problem James_dean Linux - Networking 1 09-23-2005 04:51 PM
MySQL connection LINUX domain Socket problem herambshembekar Programming 2 10-18-2002 12:01 PM
socket connection raven Programming 1 02-04-2002 11:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration