473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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(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
3 2747
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sticks | last post by:
hello i want to be able to send a packet to a http server and receive the response, but i'm not quite sure how to do it. i thought of using sockets, and although i have worked out how to send...
2
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose only job is to read. Any data read (from recv call)...
6
by: Philippe C. Martin | last post by:
Hi, I am following a few tutorial and this howto: ************************************************************* ............ What happens in the web server is a bit more complex. First, the...
0
by: Per Bergland | last post by:
From MSDN dox: WinSock select(): For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was...
0
by: Richard L Rosenheim | last post by:
I'm playing around with setting up a multicast socket. I can create a socket for sending, and I can one that receives. Is it possible to create one socket that can do both? The code I've been...
4
by: Joe Kinsella | last post by:
The following code behaves differently from what I would expect: socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); socket.Blocking = false;...
2
by: Silby | last post by:
I've been messing around with sockets for a bit and i got most of it down (i think). The server side is an asynchronous tcp socket listener with sits and waits for data. The client side uses...
4
by: carl_bevil | last post by:
I would like to use a single port to connect to a server. I would like to be able to disconnect a socket using this port and then be able to connect again (on the same port) immediately. I know...
1
by: Steve Mol | last post by:
I have a program that works perfectly, as long as only one socket is in use at a time. If I open a second socket, the program works great - for a random amount of time. Then, suddenly, my...
2
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.