By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,334 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,334 IT Pros & Developers. It's quick & easy.

Socket Question

P: n/a
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

Jul 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.programmer would be
a good starting place.


Brian
Jul 5 '07 #2

P: n/a
In article <11**********************@q69g2000hsb.googlegroups .com>,
Stuart <bi*******@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
Jul 5 '07 #3

P: n/a
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.securityfocus.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

Jul 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.