client / server

This is a discussion on client / server within the C Programming forums, part of the General Programming Boards category; I am new to network programming. So far I am just analyzing programs written by others. I have just one ...

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    client / server

    I am new to network programming. So far I am just analyzing programs written by others. I have just one computer so I have to use address 127.0.0.1 when running these programs.

    When I attempt to run this client (./client 127.0.0.1) I get a 'connection refused' error.

    Any suggestions as to why this might occur. Thanks in advance !

    Code:
    #include "myheader.h"
    
    #define SA struct sockaddr
    
    int
    main(int argc, char **argv)
    {
     int     sockfd, n;
     char    recvline[MAXLINE + 1];
     struct sockaddr_in servaddr;
    
     if (argc != 2)
      printf ("Usage: ./client <server address>\n"), exit (1);
    
     if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      perror ("Socket error"), exit (1);
    
     bzero(&servaddr, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port   = htons(13); /* daytime server */
    
     if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
      printf ("inet_pton error for %s\n", argv[1]), exit (1);
    
     if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
      perror ("connect error"), exit (1);
    
     while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
      recvline[n] = 0; /* null terminate */
      if (fputs(recvline, stdout) == EOF)
       perror ("fputs error"), exit (1);
     }
    
     if (n < 0)
      printf ("read error\n"), exit (1);
    
     exit(0);
    }
    Here is the code for the server:

    Code:
    #include "myheader.h"
    
    #define SA struct sockaddr
    
    int
    main(int argc, char **argv)
    {
     int     listenfd, connfd;
     struct sockaddr_in servaddr;
     char    buff[MAXLINE];
     time_t    ticks;
    
     listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
     bzero(&servaddr, sizeof(servaddr));
     servaddr.sin_family      = AF_INET;
     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
     servaddr.sin_port        = htons(13); /* daytime server */
    
     bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
    
     listen(listenfd, 5);
    
     for ( ; ; ) {
      connfd = accept(listenfd, (SA *) NULL, NULL);
    
            ticks = time(NULL);
            snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
            write(connfd, buff, strlen(buff));
    
      close(connfd);
     }
    }
    PS. myheader.h includes all network include files needed.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I tried you code, both client and server..... I had to change one function (I didn't appear to have inet_pton(), so I used inet_aton() instead). And it all worked OK.

    I noticed that in your client you do all the correct error checking, but you don't do any in your server. Maybe doing so might help you find the problem.

    When you run your server, can you see it listening? (via netstat -an), can you telnet to it?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's possible that your use of port 13 is not being allowed. Ports less than 1024 are usually considered "reserved" ports.

    Try changing to a higher port number.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    Thumbs up Thx

    Thank you Hammer and quzah.

    I was using a wrong port. Port 13 is reserved to Daytime service. I changed to a higher port number (2501). It worked fine.

    I also did some error checking, as you suggested, Hammer. That's always a good habit

    PS. By the way, Hammer, how come you are allowed to use port 13? (just curious)
    Last edited by PutoAmo; 05-24-2002 at 06:08 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Thx

    Originally posted by PutoAmo
    PS. By the way, Hammer, how come you are allowed to use port 13? (just curious)
    'cos I didn't run it on Unix.... it was run under cygwin on Win98.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  3. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 10:33 AM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21