473,856 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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]);

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
== 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");
printf("Finnish ed... \n");
close(servSock) ;

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

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?


Jul 5 '07 #1
3 2773
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.

Jul 5 '07 #2
In article <11************ **********@q69g 2000hsb.googleg roups.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.securityfoc us.com/columnists/423>
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

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 data to the http server, i have no idea how to receive the responses. any help wuld be greatly appreciated. this is what i've got so far. i know that the server is receiving the
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) is just passed to (some) Queue. This thread is "owned" by a second thread waiting on a Queue for write requests. The thread just pops these from the Queue, and calls the send method from the socket. This thread also takes care of closing the...
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 web server creates a "server socket".
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 closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. ..NET Socket.Select(): Readability can also indicate whether the remote Socket has shut down
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 basing my work on is the code in "IP Multicasting in C#" by Gary Brewer, http://www.codeproject.com/csharp/multicast.asp). The following is the code snippet that I have. I'm able to receive packets, but sending is hanging up.
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; socket.Connect(ipe); isConnected = socket.Poll(30*1000000, SelectMode.SelectWrite); I would have expected that once I set the socket to non-blocking, the connect would return immediately and the poll would wait up to 30 seconds
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 TcpClient to connect. Connecting, sending and receiving data, error handling, ... all works fine. The problem is when closing the connection. Now as i understand it, closing the socket works as follows: client -> tcp finish command
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 there is a TIME_WAIT value, but I thought I could get around it by using the ResuseAddress option. Here is what the code looks like (C#): Socket connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
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 EndReceive events start firing rapidly on one socket or the other, sending xFF bytes constantly (no real data). Even if I close the socket, the EndReceive event fires repeatedly. How do I get this socket to stop these errant events?
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 server message and send another message. 4) finally, the server receive the message and close the connection.
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.