Hi,
I read the following code which open a server socket for client
request.
However, i would like to know how can I change it so that i just
listen for client requestfor 10 seconds, after that, it bows out?
Code:
// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
std::cout << "cannot create listen socket";
return;
}
// Bind listen socket to listen port. First set various fields in
// the serverAddress structure, then call bind().
// htonl() and htons() convert long integers and short integers
// (respectively) from host byte order (on x86 this is Least
// Significant Byte first) to network byte order (Most Significant
// Byte first).
serverAddress.s in_family = AF_INET;
serverAddress.s in_addr.s_addr = htonl(INADDR_AN Y);
serverAddress.s in_port = htons(listenPor t);
if (bind(listenSoc ket,
(struct sockaddr *) &serverAddre ss,
sizeof(serverAd dress)) < 0) {
std::cout << "cannot bind socket";
return;
}
// Wait for connections from clients.
// This is a non-blocking call; i.e., it registers this program with
// the system as expecting connections on this socket, and then
// this thread of execution continues on.
listen(listenSo cket, 10);
int count = 0;
while (1) {
std::cout << "Waiting for TCP connection on port " << listenPort
<< " ...\n";
// Accept a connection with a client that is requesting one. The
// accept() call is a blocking call; i.e., this thread of
// execution stops until a connection comes in.
// connectSocket is a new socket that the system provides,
// separate from listenSocket. We *could* accept more
// connections on listenSocket, before connectSocket is closed,
// but this program doesn't do that.
clientAddressLe ngth = sizeof(clientAd dress);
connectSocket = accept(listenSo cket,
(struct sockaddr *) &clientAddre ss,
&clientAddressL ength);
if (connectSocket < 0) {
std::cout << "cannot accept connection ";
return;
}
Thank you. 7 3177
"si************ ***@gmail.com" <si************ ***@gmail.comwr ites:
I read the following code which open a server socket for client
request.
However, i would like to know how can I change it so that i just
listen for client requestfor 10 seconds, after that, it bows out?
Code:
// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
std::cout << "cannot create listen socket";
return;
}
[snip]
That's C++. Why did you post to comp.lang.c?
But before you post to comp.lang.c++, you need to be aware that
sockets are not defined by either the C language or the C++ language.
Try a newsgroup that deals with your operating system, most likely
comp.unix.progr ammer.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Jun 18, 9:52 am, "silverburgh.me ...@gmail.com"
<silverburgh.me ...@gmail.comwr ote:
Hi,
I read the following code which open a server socket for client
request.
However, i would like to know how can I change it so that i just
listen for client requestfor 10 seconds, after that, it bows out?
Code:
// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
std::cout << "cannot create listen socket";
return;
}
// Bind listen socket to listen port. First set various fields in
// the serverAddress structure, then call bind().
// htonl() and htons() convert long integers and short integers
// (respectively) from host byte order (on x86 this is Least
// Significant Byte first) to network byte order (Most Significant
// Byte first).
serverAddress.s in_family = AF_INET;
serverAddress.s in_addr.s_addr = htonl(INADDR_AN Y);
serverAddress.s in_port = htons(listenPor t);
if (bind(listenSoc ket,
(struct sockaddr *) &serverAddre ss,
sizeof(serverAd dress)) < 0) {
std::cout << "cannot bind socket";
return;
}
// Wait for connections from clients.
// This is a non-blocking call; i.e., it registers this program with
// the system as expecting connections on this socket, and then
// this thread of execution continues on.
listen(listenSo cket, 10);
int count = 0;
while (1) {
std::cout << "Waiting for TCP connection on port " << listenPort
<< " ...\n";
// Accept a connection with a client that is requesting one. The
// accept() call is a blocking call; i.e., this thread of
// execution stops until a connection comes in.
// connectSocket is a new socket that the system provides,
// separate from listenSocket. We *could* accept more
// connections on listenSocket, before connectSocket is closed,
// but this program doesn't do that.
clientAddressLe ngth = sizeof(clientAd dress);
connectSocket = accept(listenSo cket,
(struct sockaddr *) &clientAddre ss,
&clientAddressL ength);
if (connectSocket < 0) {
std::cout << "cannot accept connection ";
return;
}
Thank you.
Hi
Though this is not a part of C programming, still I am going since i
have worked some stuff like this.
For the functions u are using u need to use Unix APIs like socket.h
and fcntl.h
As the comments in this code specify that the accept() is a blocking
mode function, so u need to call in in unblockking mode(Sorry if the
terminology I am using is not very precise).
For that take the flags in some variable stat and flip the flags to
non blocking mode
stat = fcntl(listenSoc ket, F_GETFL, NULL);
stat |= O_NONBLOCK;
fcntl(listenSoc ket, F_SETFL, stat);
Start a loop that works for 10seconds.
call the function accept(). If there is nothing which accept() can
work upon , it goes to the next statement. So, u can continue with
this for 10 seconds, apply the condition with accept() that if
something is returned by accept(), break the loop.
After the loop completes donot forget to flip back the flags to
blocking mode.
stat = fcntl(listenSoc ket, F_GETFL, NULL);
stat = (~O_NONBLOCK);
fcntl(listenSoc ket, F_SETFL, stat);
This worked for me!!!
Thanks
Aditya
Aditya said:
<snip>
For the functions u are using u need to use Unix APIs like socket.h
and fcntl.h
As the comments in this code specify that the accept() is a blocking
mode function, so u need to call in in unblockking mode
Not necessarily true, and in fact rather misleading.
The OP would do better to take this up in comp.unix.progr ammer where he
will get expert advice.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On Jun 19, 1:07 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Aditya said:
<snip>
For the functions u are using u need to use Unix APIs like socket.h
and fcntl.h
As the comments in this code specify that the accept() is a blocking
mode function, so u need to call in in unblockking mode
Not necessarily true, and in fact rather misleading.
The OP would do better to take this up in comp.unix.progr ammer where he
will get expert advice.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Hi Richard
Sorry if was wrong... I am just a newbie. But please do tell me if I
am misleading. What is the place I am wrong?
Thanks
Aditya
Aditya wrote:
On Jun 19, 1:07 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Aditya said:
<snip>
>>For the functions u are using u need to use Unix APIs like socket.h and fcntl.h As the comments in this code specify that the accept() is a blocking mode function, so u need to call in in unblockking mode
Not necessarily true, and in fact rather misleading.
The OP would do better to take this up in comp.unix.progr ammer where he will get expert advice.
Hi Richard
Sorry if was wrong... I am just a newbie. But please do tell me if I
am misleading. What is the place I am wrong?
The advice and the reason (what do you do when accept returns? How to
you manage the timeout?) are both off topic! The best advice is to move
the question to comp.unix.progr ammer.
One more thing, please please don't use silly abbreviations like "u",
they make you post hard to read.
--
Ian Collins.
Aditya said:
On Jun 19, 1:07 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Aditya said:
<snip>
For the functions u are using u need to use Unix APIs like socket.h
and fcntl.h
As the comments in this code specify that the accept() is a
blocking mode function, so u need to call in in unblockking mode
Not necessarily true, and in fact rather misleading.
The OP would do better to take this up in comp.unix.progr ammer where he will get expert advice.
Sorry if was wrong... I am just a newbie. But please do tell me if I
am misleading. What is the place I am wrong?
One of the mistakes you made was in trying to offer Unix programming
advice in a newsgroup where it is not topical and therefore where any
mistakes you make risk not being picked up.
To learn about the /other/ mistake you made, ask in
comp.unix.progr ammer, since it's not topical here in comp.lang.c.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
Aditya wrote:
>
.... snip ...
>
Though this is not a part of C programming, still I am going since
i have worked some stuff like this. For the functions u are using
u need to use Unix APIs like socket.h and fcntl.h
u hasn't been on this newsgroup for some time. His name is
singular, so you should use the verb 'is', not are.
I.e. don't use silly geekspeak on Usenet. It is NOT cool.
--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Daniel |
last post by:
TcpClient close() method socket leak
when i use TcpClient to open a connection, send data and close the TcpClient
with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket
on the client machine to close per my network app the computer fills up w/
thousands of these
:0 TCP foobox:8888 localhost:2188 TIME_WAIT
:0 TCP foobox:8888 localhost:2189 TIME_WAIT
:0 TCP foobox:8888 localhost:2190 TIME_WAIT
|
by: mBird |
last post by:
I wrote a service that listens for broadcast messages from my firewall (UDP
snmp trap messages) and parses and puts the data in an database.
I'd also like to write an app that is a simple form that can listen in when
it runs (so I can see messages in a form as they occur.)
So I need the ability to listen to a UDP port with two apps at the same time
(the service and my app). But when I do that I get an error:...
|
by: Chris Tanger |
last post by:
Context:
C#
System.Net.Sockets
Socket created with constructor prarmeters Internetwork, Stream and TCP
everything else is left at the default parameters and options except linger
may be changed as I find appropriate.
I am using the socket asynchronously by calling the BeingSend and
BeginReceive calls. I would like to be able to call shutdown and close
asynchronously if possible.
|
by: Ricardo Quintanilla |
last post by:
i had a problem whom i do not know how to explain.
i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and
receive data to an AS400 socket. Two months ago it started to work slowly,
about 4 seconds between send and receive. In our production environment with
hundreds of transactions it was truly costly.
a while ago i changed de TcpClient object. Now i am using a Socket
(System.Net.Sockets.Socket) object and it...
|
by: Sharon |
last post by:
Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read, only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right...
| |
by: Yofnik |
last post by:
I am writing an application that simulates a TCP device that only
accepts one connection at a time. For my application, I would like
connection requests to fail if one already exists. So far I have the
following code:
IPAddress ip = Dns.Resolve("localhost").AddressList;
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(ip, 2323);
s.Bind(ep);
|
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= |
last post by:
Hello,
First of all I wish you a good day.
My help request is about .NET asynchrounus socket communication. I have
developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I
have several problems re-establishin connection between peers. Below are my
problem cases after closing of the first successfull communication;
#1) I re-start the Server to accept connection requests. While it is
waiting, I run the Client. It is...
|
by: felix.citycs |
last post by:
Why I cannot do with this code and exception is thrown with "the
requested address is not valid in its context"
try
{
IPAddress inputDNS_IP = Dns.Resolve(inputDNS_IP).AddressList;
// start the data port and listen for connection from input host
this.dataListener = new TcpListener(inputDNS_IP, dataPort);
|
by: =?Utf-8?B?Vmlua2k=?= |
last post by:
Hello everyone,
I have this code for TCPListenPort. The code works fine, but my manager is
asking me to establish multiple connections to the same port. How can i
acheive that
below is my code
Int32 port = Int32.Parse(ConfigurationManager.AppSettings);
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |