 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
winexec Guest
|
Posted: Mon Sep 15, 2003 4:07 pm Post subject: sockets |
|
|
Hi all.
I'm trying to understand how the winsock is working and I encounter a problem: the socket is listening OK but afterwards cannot be closed! I dont't know what could be wrong.
Here is a sample:
procedure Listen_Close;
var
wsadata:twsadata;sock:integer;
addr:tsockaddrin;
begin
wsastartup(MAKEWORD(1,1),wsadata);
sock:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
addr.sin_family:=AF_INET;
addr.sin_addr.s_addr:=htonl(INADDR_ANY);
addr.sin_port:=htons(12345);//a port
bind(Sock,addr,sizeof(addr))
WSAAsyncSelect(Sock,aHandle,aMsg,FD_ACCEPT or FD_READ or FD_CLOSE);
listen(Sock,2);//not sure about that 2
//Now I want to close the socket
shutdown(sock,2);
closesocket(sock);
wsacleanup;
end;
Thx for help.
|
|
| Back to top |
|
 |
Francois PIETTE Guest
|
Posted: Mon Sep 15, 2003 4:11 pm Post subject: Re: sockets |
|
|
If you use async sockets, then you must wait for the events to occur !
A listening socket doesn't have to be shutdown, just call closesocket.
btw: You can download ICS (http://www.overbyte.be) and see wsocket.pas
source file. It make use of asynchronous socket. You could learna lot
studying ICS source code.
--
Contribute to the SSL Effort. Visit
http://overbyte.delphicenter.com/eng/ssl.html
--
[email]francois.piette (AT) overbyte (DOT) be[/email]
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
"winexec" <winexec (AT) email (DOT) com> a écrit dans le message de
news:3f65e3c4$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
Hi all.
I'm trying to understand how the winsock is working and I encounter a
problem: the socket is listening OK but afterwards cannot be closed! I |
dont't know what could be wrong.
| Quote: |
Here is a sample:
procedure Listen_Close;
var
wsadata:twsadata;sock:integer;
addr:tsockaddrin;
begin
wsastartup(MAKEWORD(1,1),wsadata);
sock:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
addr.sin_family:=AF_INET;
addr.sin_addr.s_addr:=htonl(INADDR_ANY);
addr.sin_port:=htons(12345);//a port
bind(Sock,addr,sizeof(addr))
WSAAsyncSelect(Sock,aHandle,aMsg,FD_ACCEPT or FD_READ or FD_CLOSE);
listen(Sock,2);//not sure about that 2
//Now I want to close the socket
shutdown(sock,2);
closesocket(sock);
wsacleanup;
end;
Thx for help.
|
|
|
| Back to top |
|
 |
Andy M. Guest
|
Posted: Mon Sep 15, 2003 8:45 pm Post subject: Re: sockets |
|
|
| Quote: | I'm trying to understand how the winsock is working and I encounter a problem: the socket is listening OK but afterwards cannot be closed! I dont't know what could be wrong.
|
How do you know?
|
|
| Back to top |
|
 |
winexec Guest
|
Posted: Tue Sep 16, 2003 6:00 am Post subject: Re: sockets |
|
|
Thx, I downloaded ICS and I'll take a look later.
I also made more research. I understand now that TCP is a bidirectional protocol and for closing a socket must be send a Socket.Close command from the Client. In my case it seems that the socket is not closing properly. What I did was that: I made the server listen, I connected to the server and all was OK, I closed the connection from the client, the server received the message (FD_CLOSE) and started the closing and both Shutdown and CloseSocket functions return -1. So there is a bug! Can you explain me how the socket can be close normally? Thx.
"Francois PIETTE" <francois.piette (AT) overbyte (DOT) be> wrote:
| Quote: | If you use async sockets, then you must wait for the events to occur !
A listening socket doesn't have to be shutdown, just call closesocket.
btw: You can download ICS (http://www.overbyte.be) and see wsocket.pas
source file. It make use of asynchronous socket. You could learna lot
studying ICS source code.
|
|
|
| Back to top |
|
 |
Francois Piette Guest
|
Posted: Tue Sep 16, 2003 7:13 am Post subject: Re: sockets |
|
|
| Quote: | Can you explain me how the socket can be close normally?
|
An async client socket should be closed by calling Shutdown. This send the
FIN message to the server which will then close his end, sending an ACK-FIN
message to the client. At client side you receive FD_CLOSE notification, you
then call closesocket to free the socket handle.
You should also read the winsock doc (setsockopt function) and understand
the "linger" mechanism (SO_LINGER, SO_DONTLINGER) which modifies the way the
close operation is handled by the system.
--
Contribute to the SSL Effort. Visit
http://overbyte.delphicenter.com/eng/ssl.html
--
[email]francois.piette (AT) overbyte (DOT) be[/email]
Author of ICS (Internet Component Suite, freeware)
Author of MidWare (Multi-tier framework, freeware)
http://www.overbyte.be
"winexec" <winexec (AT) email (DOT) com> a écrit dans le message de
news:3f66a710$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
Thx, I downloaded ICS and I'll take a look later.
I also made more research. I understand now that TCP is a bidirectional
protocol and for closing a socket must be send a Socket.Close command from |
the Client. In my case it seems that the socket is not closing properly.
What I did was that: I made the server listen, I connected to the server and
all was OK, I closed the connection from the client, the server received the
message (FD_CLOSE) and started the closing and both Shutdown and CloseSocket
functions return -1. So there is a bug! Can you explain me how the socket
can be close normally? Thx.
| Quote: |
"Francois PIETTE" <francois.piette (AT) overbyte (DOT) be> wrote:
If you use async sockets, then you must wait for the events to occur !
A listening socket doesn't have to be shutdown, just call closesocket.
btw: You can download ICS (http://www.overbyte.be) and see wsocket.pas
source file. It make use of asynchronous socket. You could learna lot
studying ICS source code.
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|