Socket Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart

    #1

    Socket Question

    I am in the process of teaching myself socket programming. I am
    "playing around" with
    some simple echo server-client programs for m the book TCP/IP Sockets
    in C.

    The Server program is:

    #include "TCPEchoServer. h" /* TCP echo server includes */
    #include <pthread.h /* for POSIX threads */

    void *ThreadMain(voi d *arg); /* Main program of a thread */

    /* Structure of arguments to pass to client thread */
    struct ThreadArgs
    {
    int clntSock; /* Socket descriptor for client
    */
    };

    int main(int argc, char *argv[])
    {
    int servSock; /* Socket descriptor for server
    */
    int clntSock; /* Socket descriptor for client
    */
    int i;
    unsigned short echoServPort; /* Server port */
    pthread_t threadID; /* Thread ID from
    pthread_create( ) */
    struct ThreadArgs *threadArgs; /* Pointer to argument structure
    for thread */

    if (argc != 2) /* Test for correct number of arguments */
    {
    fprintf(stderr, "Usage: %s <SERVER PORT>\n", argv[0]);
    exit(1);
    }

    echoServPort = atoi(argv[1]); /* First arg: local port */

    servSock = CreateTCPServer Socket(echoServ Port);
    clntSock = AcceptTCPConnec tion(servSock);

    /* Create separate memory for client argument */
    if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(s truct
    ThreadArgs)))
    == NULL)
    DieWithError("m alloc() failed");
    threadArgs -clntSock = clntSock;

    /* Create client thread */
    if (pthread_create (&threadID, NULL, ThreadMain, (void *)
    threadArgs) != 0)
    DieWithError("p thread_create() failed");
    printf("with thread %ld\n", (long int) threadID);

    for (i=0;i<20;i++) /* run forever */
    {
    printf("Hmmmmm HMmmmmmm Hmmmm... \n");
    sleep(3);
    }
    printf("Finnish ed... \n");
    close(servSock) ;
    /* NOT REACHED */
    }

    In the original program, the for loop was a forever loop. But I
    modified it
    to only run for a minute or so after which the loop finishes and the
    program
    exits.

    So I start the server and connect to it using telnet. The client
    handling part of the code
    checks to see if the string "EXIT" appears and if so the client
    handler (in the case a thread)
    exits and closes the client socket. All this done before the minute is
    up. When the minute is up
    the Server quits.

    However, I find that I cannot immediatley restart the Server on the
    same port; I get the error that
    the port is already in use. Using netstat, I find that the port is in
    the TIME_WAIT state.

    Is there a way to make sure that when the program exits that the
    socket is not placed in
    the TIME_WAIT state?

    Stuart

  • Default User

    #2
    Re: Socket Question

    Stuart wrote:
    I am in the process of teaching myself socket programming. I am
    "playing around" with
    some simple echo server-client programs for m the book TCP/IP Sockets
    in C.

    Sockets and threads are highly platform-specific. You'll need to find a
    newsgroup dealing with yours. Looks like comp.unix.progr ammer would be
    a good starting place.




    Brian

    Comment

    • Walter Roberson

      #3
      Re: Socket Question

      In article <1183669085.720 017.251490@q69g 2000hsb.googleg roups.com>,
      Stuart <bigdakine@aol. comwrote:
      >I am in the process of teaching myself socket programming.
      Socket programming is not part of the C standard. POSIX threads
      are also not part of the C standard. In this newsgroup we do not
      have the proper expertise to assist you with your problem.
      Try a unix programming newsgroup.

      >However, I find that I cannot immediatley restart the Server on the
      >same port; I get the error that
      >the port is already in use. Using netstat, I find that the port is in
      >the TIME_WAIT state.
      >Is there a way to make sure that when the program exits that the
      >socket is not placed in
      >the TIME_WAIT state?
      Definitely not a C question.

      As best I can tell, you've grasped the nettle from the wrong
      end. Instead of asking whether you can force the socket not to be
      left in TIME_WAIT, you should be asking whether there is a way
      to proceed even though previous socket is in TIME_WAIT. And
      any good unix programming newsgroup should be able to help you
      with that question.
      --
      If you lie to the compiler, it will get its revenge. -- Henry Spencer

      Comment

      • CBFalconer

        #4
        Re: Socket Question

        Stuart wrote:
        >
        I am in the process of teaching myself socket programming. I am
        "playing around" with some simple echo server-client programs for
        the book TCP/IP Sockets in C.
        Better ask in a newsgroup where 'socket programming' is on-topic.
        This isn't it.

        --
        <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
        <http://www.securityfoc us.com/columnists/423>
        <http://www.aaxnet.com/editor/edit043.html>
        cbfalconer at maineline dot net



        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        Working...