Socket Question

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
 
D

Default User

Stuart said:
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.programmer would be
a good starting place.




Brian
 
W

Walter Roberson

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.
 
C

CBFalconer

Stuart said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top