Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:12:58+00:00 2026-06-07T19:12:58+00:00

I am working through examples in UNIX Network Programming and I’ve adapted daytimeclientserv.c into

  • 0

I am working through examples in UNIX Network Programming and I’ve adapted “daytimeclientserv.c” into this code here. The server sends the date/time string to the client as expected except for the very first request it receives upon starting up. When I first run the server program (on another computer in the LAN) it creates the listening socket, binds it and then waits for connections. Upon receiving the first request it prints the date/time string to its own stdout (terminal) instead of to the socket and the client program hangs forever waiting. However, all subsequent requests are sent to the clients correctly. Using gdb, I noticed that connfd is always set to zero. It is set to zero on the first request and also on all future ones.

I also have a few other questions related to this:

  • if the server listens on one socket (listenfd) and then reconnects on another (connfd) with connect(), how does the client deal with the change of socket? It was my understanding that a socket is uniquely identified by four parts: servIPaddr, servPort, clientIPaddr, clientPort

  • how can i run the server (on linux) without being root

  • how can i cleanly close the listening socket, so that i can use it again. I get a bind error if I quit the server program with SIGINT (Ctrl-C). So far I’ve been using gdb, and using a “call close(listenfd)” to manually call the function. But is there a way to do this if I am not using gdb (ie. debugging the client application only).

Any help is greatly appreciated.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#define BUFFER 80
int main(int argc, char **argv) {
  int listenfd, connfd;
  char buf[BUFFER];
  struct sockaddr_in servaddr;
  time_t ticks;
  struct sockaddr *ptr;
  char *ret;

  if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket error");
    return 1;
  }

  memset(&servaddr, 0, sizeof(servaddr));
  memset(buf, 0, BUFFER);
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  ptr = (struct sockaddr*) &servaddr;
  if ( bind(listenfd, ptr ,sizeof(servaddr)) < 0) {
    perror("bind error");
    return 2;
  }

  if ( listen(listenfd, 128) < 0) {
    perror("listen error");
    return 3;
  }

  ptr = NULL;
  while ( 1 ) {
    if ( connfd = accept(listenfd, ptr, NULL) < 0) {
      perror("accept error");
      return 4;
    } else {
      ticks = time(NULL);
      ret = ctime(&ticks);
      sprintf(buf, "%.24s\n", ret);
      if ( write(connfd, buf, strlen(buf)) < 0) {
        perror("write error");
        close(connfd);
    }
  }

  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:13:00+00:00Added an answer on June 7, 2026 at 7:13 pm

    Here was my hunch: On a terminal (tty), stdout and stdin are the same physical device. Therefore writing to filedescriptor 0 (stdin) might actually work and result in terminal output.

    You need parentheses around this

       if ( connfd = accept(listenfd, ptr, NULL) < 0) {
    

    Like so

      if ( (connfd = accept(listenfd, ptr, NULL)) < 0) {
    

    Or connfd will be assigned ‘0’

    Update Just tested this, and this is indeed the culprit. Next time, compile with gcc -Wall and the compiler would have told you this (and several other issues of good form/style). That way, you won’t have to rely on having the hunch to find the error.

    Fixed version:

    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <time.h>
    
    #define BUFFER 80
    int main(int argc, char **argv) {
        int listenfd, connfd;
        char buf[BUFFER];
        struct sockaddr_in servaddr;
        time_t ticks;
        struct sockaddr *ptr;
        char *ret;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if ( listenfd < 0 ) {
            perror("socket error");
            return 1;
        }
    
        memset(&servaddr, 0, sizeof(servaddr));
        memset(buf, 0, BUFFER);
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(13);
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        ptr = (struct sockaddr*) &servaddr;
        if ( bind(listenfd, ptr ,sizeof(servaddr) ) < 0) {
            perror("bind error");
            return 2;
        }
    
        if ( listen(listenfd, 128) < 0 ) {
            perror("listen error");
            return 3;
        }
    
        ptr = NULL;
        while ( 1 ) {
            connfd = accept(listenfd, ptr, NULL);
            if ( connfd < 0 ) {
                perror("accept error");
                return 4;
            } else {
                ticks = time(NULL);
                ret = ctime(&ticks);
                sprintf(buf, "%.24s\n", ret);
                if ( write(connfd, buf, strlen(buf)) < 0) {
                    perror("write error");
                    close(connfd);
                }
            }
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working my way through some code examples and I stumbled upon this: endings
I'm working my way through Fowler's Analysis Patterns and programming examples for myself in
I am just working through some examples from my text book, writing the code
Working through more book examples- this one is a partial poker program- This segment
I'm working through some javascript examples, and I just did this one: <!DOCTYPE html
I'm working through a tutorial which includes this code: for position, target in population_gen(population):
I am working through some examples in a WCF book. There is a Host
I'm working through some MSDN examples, and some books on ADO.Net. What they all
I'm working through the Clojure examples in Stuart Halloway's Programmming Clojure and I've hit
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.