S
Stuart
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(void *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 = CreateTCPServerSocket(echoServPort);
clntSock = AcceptTCPConnection(servSock);
/* Create separate memory for client argument */
if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct
ThreadArgs)))
== NULL)
DieWithError("malloc() failed");
threadArgs -> clntSock = clntSock;
/* Create client thread */
if (pthread_create(&threadID, NULL, ThreadMain, (void *)
threadArgs) != 0)
DieWithError("pthread_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("Finnished... \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
"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(void *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 = CreateTCPServerSocket(echoServPort);
clntSock = AcceptTCPConnection(servSock);
/* Create separate memory for client argument */
if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct
ThreadArgs)))
== NULL)
DieWithError("malloc() failed");
threadArgs -> clntSock = clntSock;
/* Create client thread */
if (pthread_create(&threadID, NULL, ThreadMain, (void *)
threadArgs) != 0)
DieWithError("pthread_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("Finnished... \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