Problem with socket connection


 
Thread Tools Search this Thread
Top Forums Programming Problem with socket connection
# 1  
Old 04-08-2009
Error 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;
}


# 2  
Old 04-09-2009
the server's IP address should be in the hosts file .
check it in /etc/hosts or /usr/hosts .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Tcp Socket (Connection refused) to my server box

I installed a fresh copy of Solaris 7 and present up my ip and domain for my web services but when I try to connect to it I get the following error; TCPActiveOpen: connect failed tcp/192.168.1.148/7900: 146 (Connection refused). the port is open in my router but I don't no were to add it in... (5 Replies)
Discussion started by: Wpgn
5 Replies

2. UNIX for Advanced & Expert Users

Fatal: Read from socket failed: Connection reset by peer [preauth]

Hello, I have recently updated my AIX machine from version 6.1.7.5 to 6.1.9.1 and i noticed that the errpt of the server is full of ssh messages like the one below: sshdprocess_id>]: fatal: Read from socket failed: Connection reset by peer Does anyone knows if this a known bug of the ssh... (15 Replies)
Discussion started by: omonoiatis9
15 Replies

3. Programming

socket programming using UDP connection

I want to send packets through single socket() but using two different port numbers in UDP. Anybody give some idea on this. Thanks in advance.:) (2 Replies)
Discussion started by: naresh046
2 Replies

4. Programming

doing a socket connection using ssh service

Trying to establish a socket connection using ssh service - but want to use different login name for ssh than what currently logged in as - if under shell know to use ssh -l <login-name> host - however - not sure how to tell o.s. to use different login name when use connect command (2 Replies)
Discussion started by: clcoh11
2 Replies

5. Solaris

Solaris 10 ftp connection problem (connection refused, connection timed out)

Hi everyone, I am hoping anyone of you could help me in this weird problem we have in 1 of our Solaris 10 servers. Lately, we have been having some ftp problems in this server. Though it can ping any server within the network, it seems that it can only ftp to a select few. For most servers, the... (4 Replies)
Discussion started by: labdakos
4 Replies

6. UNIX for Dummies Questions & Answers

fatal: Read from socket failed: Connection reset by peer

I get this error when I log in through console: "fatal: Read from socket failed: Connection reset by peer". Can you tell me what this is and why it happens, and how to stop it? Thank you. (1 Reply)
Discussion started by: iamnew2solaris
1 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Programming

Socket Programming - Port Scanner. I Get Connection Timed Out, Why?

When i put the target IP as 127.0.1.1, the program is working fine, can catch blocked & open ports. But when i try to scan remotely, i get connection timed out! Can you tell me why? :( Here is my code - Look at between where i put astriks - at the bottom: #include<iostream>... (3 Replies)
Discussion started by: f.ben.isaac
3 Replies

9. Programming

Cloning a socket connection, using other port numbers

Hello everybody, I've coded a multi-client server based on internet sockets using the scheme listen on port X-accept-fork, exactly like beej's guide At some point I would like to establish a secondary connection between a client and the server-child serving him. I was considering the... (4 Replies)
Discussion started by: jonas.gabriel
4 Replies

10. Programming

Error: No Route to host...urgent {socket() connection}

hello, I am doing Socket programming.. when I am establishing a socket connection using TCP protocol ...I am getting Error :: No route to host. at the client side during connect() call...........that it is returning -1. So I thing problem lies here......but what to do now... So for just... (6 Replies)
Discussion started by: arunchaudhary19
6 Replies
Login or Register to Ask a Question