Home > Archive > Unix Programming > May 2006 > persistent connection
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
persistent connection
|
|
| aston 2006-05-08, 10:05 pm |
| hello everybody
I have a multithreaded server, where each thread creates new socket...
My problem is how to make this socket open all time.... Only setsokopt
is needed or anyother things i have to consider
with regards
aston
| |
| Barry Margolin 2006-05-09, 4:27 am |
| In article <1147143519.290291.306480@e56g2000cwe.googlegroups.com>,
"aston" <rajesh.rc@gmail.com> wrote:
> hello everybody
>
> I have a multithreaded server, where each thread creates new socket...
> My problem is how to make this socket open all time.... Only setsokopt
> is needed or anyother things i have to consider
I don't understand the question. Just don't close the socket.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| >>I don't understand the question. Just don't close the socket.
I have a situation like this,
how to make the connection alive evenif no request is send by the
client.In other word how to make a connection persistent. If i don't
close the socket, the client connection status will be CLOSE_WAIT, if
i am not wrong. It is possible to send the data in the close_wait state
from the client .
The situation is:
Create Threads(min alive threads)
Listen on Listen Port
For each connection on the ListenFD,
Accept and get the FD
Pass this FD to one of the idle thread.
if there are no more idle thread ,create a new instance until
maxthreads limit
is reached
Thread Operation:
On startup:
Open a outoing connection to Destination IP and Destination Port =
FDOUT
set keepalive on the TCP socket
On arrival of new FD (CFD):
Mark this thread as busy
select on CFD and FDOUT for read events
CFD read active:
Read from CFD
write to FDOUT
FDOUT read active :
Read from FDOUT
write to CFD
Close from CFD:
Close CFD
Mark this thread as idle
Close from FDOUT:
I should not get this
in case you do get it kill this thread / or open a new socket
to the port
I don't want the FDOUT to be close but how to make the fdout connected
to the server. How to solve this problem.
with regards
aston
| |
|
| >>I don't understand the question. Just don't close the socket.
I have a situation like this,
how to make the connection alive evenif no request is send by the
client.In other word how to make a connection persistent. If i don't
close the socket, the client connection status will be CLOSE_WAIT, if
i am not wrong. It is possible to send the data in the close_wait state
from the client .
The situation is:
Create Threads(min alive threads)
Listen on Listen Port
For each connection on the ListenFD,
Accept and get the FD
Pass this FD to one of the idle thread.
if there are no more idle thread ,create a new instance until
maxthreads limit
is reached
Thread Operation:
On startup:
Open a outoing connection to Destination IP and Destination Port =
FDOUT
set keepalive on the TCP socket
On arrival of new FD (CFD):
Mark this thread as busy
select on CFD and FDOUT for read events
CFD read active:
Read from CFD
write to FDOUT
FDOUT read active :
Read from FDOUT
write to CFD
Close from CFD:
Close CFD
Mark this thread as idle
Close from FDOUT:
I should not get this
in case you do get it kill this thread / or open a new socket
to the port
I don't want the FDOUT to be close. It should be connected to the
server. How to solve this problem. i have use setoptsock with
SO_KEEPALIVE. I don't know how to make the connection alive
with regards
aston
| |
| Nils O. Selåsdal 2006-05-09, 4:27 am |
| aston wrote:
>
> I have a situation like this,
>
> how to make the connection alive evenif no request is send by the
> client.In other word how to make a connection persistent. If i don't
> close the socket, the client connection status will be CLOSE_WAIT, if
> i am not wrong. It is possible to send the data in the close_wait state
> from the client .
No, you are wrong. The connection persist until you or the client closes
the connection. A TCP connection can also be half closed - so both
ends need to close it, actually.
(Network trouble not-withstanding - e.g. a NAT gateway might kill an
idle tcp connection. You might benefit from having a little keepalive/
ping-pong mechanism)
| |
|
| >>No, you are wrong. The connection persist until you or the client closes
> the connection. A TCP connection can also be half closed - so both
>ends need to close it, actually.
> (Network trouble not-withstanding - e.g. a NAT gateway might kill an
> idle tcp connection. You might benefit from having a little keepalive/
> ping-pong mechanism)
then, how the http 1.1 makes connection persistent.
I think there must be the way.
with regards
aston
| |
| Ian Collins 2006-05-09, 4:27 am |
| aston wrote:
>
>
>
> then, how the http 1.1 makes connection persistent.
> I think there must be the way.
>
That's different, HTTP is a protocol that runs over TCP. In HTTP <1.1
each request established a connection for each request, which was closed
after the response.
With HTTP 1.1, the client can request the server to keep the connection
open to reduce latency and network traffic.
--
Ian Collins.
| |
| Pascal Bourguignon 2006-05-09, 4:27 am |
| "aston" <rajesh.rc@gmail.com> writes:
>
>
> then, how the http 1.1 makes connection persistent.
> I think there must be the way.
It doesn't depend on you.
If you do close the connection, then it gets closed.
If you don't close the connection, then whatever may happen.
In the best case, it stays open.
If anything happens (a meteorite falls on the remote computer, a rat
eats the cable, a NAT gateway kills your idle connection, etc), then
it gets closed all the same.
TCP is a layer 3 protocol: network.
If you want to keep a persistent session, then you must implement your
own layer 4 protocol: session.
That means that you must reconnect yourself when the connection gets
closed.
This is what your web browser does. With http 1.1 it asks the remote
server to keep the connection open. But it you last too much to click
on the next link, the connection may have been closed, and the web
browser needs to reconnect with the server, which it does
automatically. So it feels like you're always connected, even when
there are disconnections at the layer 3 (network) or layer 2 (link) or
layer 1 (physical). That's where the cookies are used to keep session
state for the web sites.
--
__Pascal Bourguignon__ http://www.informatimago.com/
PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
| |
|
| thanx for the suggestion above.
I have some wired problem. Server close the connection unpredictably
and i am not getting all the data from the server. Which makes the
browser wild(display problem).It o some time shows the html code, says
connection time out or displays the browser right after long period of
time.
I have use recv and send function in the thread main function
like.....
code snippet of thread main function:
sock is socket descriptor passed by main thread which accept the
connection
sockfd is the thread socket descriptot.
recv(sock, buf, sizeof(buf),0); // reading the http request from the
client
send(sockfd, buf, sizeof(buf),0); // writing the http request to the
server
/** Reading the response of http request from the server (like html
,gif ) */
while ((len = Readline(sockfd, buf1,MAXLINE)) > 0) {
if(len < 0)
{
if(errno == ENOTCONN)
{
if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<
0)
err_sys("connection error:");
continue;
}
}
if (writen(sock, buf1, len) != len) // writing to the client (web
-browser)
printf("\nWRITTEN ERROR");
I know there is problem but i don't find it.
Any idea. I will post all the code if required.
with regard
aston
| |
| Nils O. Selåsdal 2006-05-09, 7:04 pm |
| aston wrote:
> thanx for the suggestion above.
>
> I have some wired problem. Server close the connection unpredictably
> and i am not getting all the data from the server. Which makes the
> browser wild(display problem).It o some time shows the html code, says
> connection time out or displays the browser right after long period of
> time.
It might be you are violating the http protocol.
While the http protocol looks nice at the first glance, there is alot of
dusty and nitpicking corners.
Be sure you have read AND understood the http rfcs properly, in addition
to the underlying protocols and apis.
This is a rather big task, so you're encouraged to use an existing
and tested http library. libcurl comes to mind.
| |
|
| Thanx for the suggestion.
It is possible to keep connection alive between the two servers.I am
facing the problems ,that one server close the connection and which
ultimately make half close connection.
I want to know the implementation ideas. Through these servers we are
connected to the web-browser and the internet.
with regards
aston
| |
| Barry Margolin 2006-05-12, 10:00 pm |
| In article <1147411709.442327.136460@i39g2000cwa.googlegroups.com>,
"aston" <rajesh.rc@gmail.com> wrote:
> Thanx for the suggestion.
> It is possible to keep connection alive between the two servers.I am
> facing the problems ,that one server close the connection and which
> ultimately make half close connection.
>
> I want to know the implementation ideas. Through these servers we are
> connected to the web-browser and the internet.
If one server closes the connection, there's nothing the other server
can do to stop it. The only way to keep a connection persistent is for
neither side to close it.
Your question is like asking how to keep a phone call going. There's no
way for you to stop the other guy from hanging up the phone.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| I want to implement two small demon servers, which makes the persistent
connection between them. Each demon servers only sent the request and
get the reply. Each servers are located between two ends. Web-browser
request and reply comes throught these servers. Theses servers makes
the connection persistent so that the other client requests can use the
previous connected sockets.
I implemented one server but don't have the idea how to implement other
server with the characterstics of persistent connection to the
implemented server. The implemented servers opens the X number of
ESTABLISHED connection and i don't want the other server close this
connections.
Any idea ??
If you don't understand my idea..........I will explain...
with regards
aston vv
| |
| Pascal Bourguignon 2006-05-16, 4:04 am |
| "aston" <rajesh.rc@gmail.com> writes:
> I want to implement two small demon servers, which makes the persistent
> connection between them. Each demon servers only sent the request and
> get the reply. Each servers are located between two ends. Web-browser
> request and reply comes throught these servers. Theses servers makes
> the connection persistent so that the other client requests can use the
> previous connected sockets.
>
> I implemented one server but don't have the idea how to implement other
> server with the characterstics of persistent connection to the
> implemented server. The implemented servers opens the X number of
> ESTABLISHED connection and i don't want the other server close this
> connections.
This is not possible. What if a rat eats thru the cable and cut the
network between the two servers? The connection will be closed for a
couple of hours.
The only thing you can do, is to try to reconnect when the connection
is closed.
--
__Pascal Bourguignon__ http://www.informatimago.com/
READ THIS BEFORE OPENING PACKAGE: According to certain suggested
versions of the Grand Unified Theory, the primary particles
constituting this product may decay to nothingness within the next
four hundred million years.
| |
| Barry Margolin 2006-05-16, 4:04 am |
| In article <1147756377.073745.269140@u72g2000cwu.googlegroups.com>,
"aston" <rajesh.rc@gmail.com> wrote:
> I want to implement two small demon servers, which makes the persistent
> connection between them. Each demon servers only sent the request and
> get the reply. Each servers are located between two ends. Web-browser
> request and reply comes throught these servers. Theses servers makes
> the connection persistent so that the other client requests can use the
> previous connected sockets.
>
> I implemented one server but don't have the idea how to implement other
> server with the characterstics of persistent connection to the
> implemented server. The implemented servers opens the X number of
> ESTABLISHED connection and i don't want the other server close this
> connections.
Since you're implementing both servers, you have full control over them.
If you don't want the other server to close the connection, then program
it so it doesn't close it.
But as Pascal pointed out, sometimes connections fail due to network
problems, server crashes, etc. You have to be able to deal with these
and re-establish the connection when necessary. If a failure happens
you'll get an error or signal (it's probably easiest to set SIGPIPE's
handler to SIG_IGN so that you just get an error).
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| hi i have seen so many answer by u so can u please answer this
its understanding about web server and http protocol
see when i first gets connected to the web server on port 80 the web
server opens a socket (with port 80 occupied) for communication to
the client now see when web server accept this incoming connection
his port gets bussy till it closes that connection . now in the mean
time if any other client B tries to connect on port 80 how server
accept this request since at a time a port can only connect to a
single socket.?
may be i am wrong in my understanding of protocol?
ok now this problem get worse when we use persistent connection since
now socket connection is not droped
so please reply so i can better understand the protocol workingh
thanksssss
| |
| Pascal Bourguignon 2006-05-18, 8:03 am |
| "shan" <shan.icwa@gmail.com> writes:
> hi i have seen so many answer by u so can u please answer this
> its understanding about web server and http protocol
> see when i first gets connected to the web server on port 80 the web
> server opens a socket (with port 80 occupied) for communication to
> the client now see when web server accept this incoming connection
> his port gets bussy till it closes that connection . now in the mean
> time if any other client B tries to connect on port 80 how server
> accept this request since at a time a port can only connect to a
> single socket.?
The question is what does it mean "at a time"?
It means, while the server is between a listen(2) and an accept(2) system call.
The server can accept any number of incoming connections as long as it
loops on this listen/accept:
#define MAX 1000
struct {
int socket;
struct sockaddr remote_address;
} connections[MAX];
n=0;
while(n<MAX){
if(0==listen(server_socket,10)){
connection[n].socket=accept(server_socket,
&(connection[n].remote_address)
sizeof(connection[n].remote_address));
if(connection[n].socket>=0){
n++;}}}
> may be i am wrong in my understanding of protocol?
A TCP connection is identified by 4 numbers:
- local ip address
- local port
- remote ip address
- remote port
Since there are about 4 billion remote ip addresses and 65535 remote
port, you could have about 262 trillion simulatneous connections on
the same local ip address and the same local port. (Well, in
practice, system resources, like number of file descriptors, would be
exhausted before).
> ok now this problem get worse when we use persistent connection since
> now socket connection is not droped
> so please reply so i can better understand the protocol workingh
> thanksssss
You should read more about unix programming, about unix network
programming, and about internet. Use http://www.google.com/, and see
for example:
http://www.kohala.com/start/unpv12e.html
http://www.hiraeth.com/alan/tutoria...rses/tcpip.html
--
__Pascal_Bourguignon__ _ Software patents are endangering
() ASCII ribbon against html email (o_ the computer industry all around
/\ 1962:DO20I=1.100 //\ the world http://lpf.ai.mit.edu/
2001:my($f)=`fortune`; V_/ http://petition.eurolinux.org/
| |
| Barry Margolin 2006-05-18, 8:03 am |
| In article <1147929655.039463.73050@y43g2000cwc.googlegroups.com>,
"shan" <shan.icwa@gmail.com> wrote:
> hi i have seen so many answer by u so can u please answer this
> its understanding about web server and http protocol
> see when i first gets connected to the web server on port 80 the web
> server opens a socket (with port 80 occupied) for communication to
> the client now see when web server accept this incoming connection
> his port gets bussy till it closes that connection . now in the mean
> time if any other client B tries to connect on port 80 how server
> accept this request since at a time a port can only connect to a
> single socket.?
Didn't you ask this same question a couple of days ago in another
thread? I answered you there, but I'll briefly repeat it.
The reason this works is because the remote address or port of client B
is different from client A. You can have as many connections as you
want to local port 80, they are distinguished by the remote address and
port.
> may be i am wrong in my understanding of protocol?
> ok now this problem get worse when we use persistent connection since
> now socket connection is not droped
There is no problem in the first place.
Every time a client connects, the server's call to accept() returns a
different socket. The server can have lots of these sockets open, and
use them to talk to different clients concurrently.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| hi thanks for response but the problem is::
i am talking about the http protocol(thats why i used persistent
connection) . When ever a client first tries to connect to a websever
the web server opens a socket . now my problem is does the
communication between client and server is done on this port (80
server) if yes then if any new connections is incoming then how server
will accept that since its port 80 is already occupied?
is this possible to open two different sockets on same port now i mean
u want to create socket like( in c# )
(server code ,not used loops but can use for server )
System.Net.Sockets.TcpListener tcpListener = new TcpListener(80);
tcpListener.Start();
socketForClient = tcpListener.AcceptSocket();
now if more then one clients wants to connect with this server ,then
how a server will provide them differnet sockets for communication .
i mean how this stuff workss
now what i think is , for each request\response in http ,client
creates a different socket and send his request and there on server
one new socket is created(by using accept) for response . but the
problem lies if server creates a new socket with port 80 then till this
socket ends no new sockets can be created since a port can only connect
to one socket at a time?
please do reply thanks
| |
|
| hi let me clear it
is this possible to create multiple sockets on the same port i mean
can i create two sockets on same ports??
if yes then it is possible but i am why i was taught that i
could create only one and in fact even my code is giving exception if
i try to connect more then one time (through telent) giving error like
"port already used "
so please clear it
thankssss
| |
|
| hi i got it we can create many socket on the same port so whatever i
studied was shattered i am lost i made such mistake
..some times u make such mistake u even dont know what made all these
things happen
wel thanks very much for answering otherwise i would be completely lost
have a nice day
byeeeeeeeeeeeee
| |
| Rick Jones 2006-05-18, 7:06 pm |
| shan <shan.icwa@gmail.com> wrote:
> hi i have seen so many answer by u so can u please answer this
> its understanding about web server and http protocol
> see when i first gets connected to the web server on port 80 the web
> server opens a socket (with port 80 occupied) for communication to
> the client now see when web server accept this incoming connection
> his port gets bussy till it closes that connection . now in the mean
> time if any other client B tries to connect on port 80 how server
> accept this request since at a time a port can only connect to a
> single socket.?
while one can only bind one _local_ port and _local_ IP to a socket, a
TCP connection is actually "named" by the four-tuple of local and
remote IP and port numbers. so, the connections from client A and
client B are separate TCP connections, and the accept() call has
associated them with sockets separate from the one the web server has
placed in the listen state.
rick jones
--
oxymoron n, commuter in a gas-guzzling luxury SUV with an American flag
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
| |
| Pascal Bourguignon 2006-05-18, 7:06 pm |
| "shan" <shan.icwa@gmail.com> writes:
> hi thanks for response but the problem is::
> i am talking about the http protocol(thats why i used persistent
> connection).
Take a big mass. Hit very hard the server, several times, savagely.
Does the connection persist? Still? Take a scisor. Cut the ASDL
cable. What about the connection now?
> When ever a client first tries to connect to a websever
> the web server opens a socket . now my problem is does the
> communication between client and server is done on this port (80
> server) if yes then if any new connections is incoming then how server
> will accept that since its port 80 is already occupied?
Ports are not occupied. Never.
Either there is a process listening on a port, or there is not.
Did your process call listen(2)?
> is this possible to open two different sockets on same port now i mean
> u want to create socket like( in c# )
> (server code ,not used loops but can use for server )
>
> System.Net.Sockets.TcpListener tcpListener = new TcpListener(80);
> tcpListener.Start();
> socketForClient = tcpListener.AcceptSocket();
>
> now if more then one clients wants to connect with this server ,then
> how a server will provide them differnet sockets for communication .
By calling several times tcpListener.AcceptSocket();
> i mean how this stuff workss
> now what i think is , for each request\response in http ,client
> creates a different socket and send his request and there on server
> one new socket is created(by using accept) for response .
Yes, that's how it works.
> but the
> problem lies if server creates a new socket with port 80 then till this
> socket ends no new sockets can be created since a port can only connect
> to one socket at a time?
No, as I explained, sockets created by accept(2) correspond to TCP
sessions, and these TCP sessions are identified by four numbers, not
only by the local port. You can create as many TCP sessions you want
on the same local address/port, as long as it's done with different
remote addresses/ports.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"You question the worthiness of my code? I should kill you where you
stand!"
| |
| Jim Cochrane 2006-05-18, 7:06 pm |
| On 2006-05-18, Pascal Bourguignon <pjb@informatimago.com> wrote:
> "shan" <shan.icwa@gmail.com> writes:
>
> The question is what does it mean "at a time"?
> It means, while the server is between a listen(2) and an accept(2) system call.
>
> The server can accept any number of incoming connections as long as it
> loops on this listen/accept:
>
> #define MAX 1000
> struct {
> int socket;
> struct sockaddr remote_address;
> } connections[MAX];
> n=0;
I'm not a NW expert, so pardon me if I'm wrong, but shouldn't this:
> while(n<MAX){
> if(0==listen(server_socket,10)){
> connection[n].socket=accept(server_socket,
> &(connection[n].remote_address)
> sizeof(connection[n].remote_address));
> if(connection[n].socket>=0){
> n++;}}}
be:
if(0==listen(server_socket,10)){
while(n<MAX){
connection[n].socket=accept(server_socket,
&(connection[n].remote_address)
sizeof(connection[n].remote_address));
if(connection[n].socket>=0){
n++;}}}
?
In other words, a 'listen' call does not need to be made before each
'accept' call.
--
*** Posted via a free Usenet account from http://www.teranews.com ***
| |
| Pascal Bourguignon 2006-05-18, 7:06 pm |
| Jim Cochrane <allergic-to-spam@no-spam-allowed.org> writes:
> On 2006-05-18, Pascal Bourguignon <pjb@informatimago.com> wrote:
>
> I'm not a NW expert, so pardon me if I'm wrong, but shouldn't this:
>
>
> be:
>
> if(0==listen(server_socket,10)){
> while(n<MAX){
> connection[n].socket=accept(server_socket,
> &(connection[n].remote_address)
> sizeof(connection[n].remote_address));
> if(connection[n].socket>=0){
> n++;}}}
> ?
>
> In other words, a 'listen' call does not need to be made before each
> 'accept' call.
Exact.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.
| |
| Barry Margolin 2006-05-18, 10:01 pm |
| In article <1147963758.931894.126370@j73g2000cwa.googlegroups.com>,
"shan" <shan.icwa@gmail.com> wrote:
> hi let me clear it
> is this possible to create multiple sockets on the same port i mean
> can i create two sockets on same ports??
How many times do I have to answer this question? YES you can have
multiple sockets on the same local port, they will have different remote
ports.
How do you think www.google.com is able to handle connections from
thousands of people at the same time?
> i try to connect more then one time (through telent) giving error like
> "port already used "
That happens if you try to BIND two sockets to the same port. But you
don't need to do that. You bind ONE socket, call listen(), and then
call accept() in a loop. Every time a connection comes in, accept()
will return a new socket.
Please get the book "Unix Network Programming, Volume 1" by W. Richard
Stevens. It explains all of this.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| yes ur right man
listen will create a queue which will pass the complete socket
connection to accept( i mean three way handshake one )
|
|
|
|
|