Winsock Study Notes 3: select model

Source: Internet
Author: User

Winsock Study Notes 3: select model

 

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, WinSock, extctrls;

Type
Tworkthread =   Class (Tthread)
Private
Fclientsocket: tsocket;
Fmemo: tmemo;
Fbuff: array [ 0 .. 10 ] Of char;
Procedure showrecv;
Protected
Procedure execute; Override ;
Public
constructor create (clientsocket: tsocket; memo: tmemo);
end;

Tform1= Class(Tform)
Memo1: tmemo;
Button1: tbutton;
Timer1: ttimer;
Procedure button1click (Sender: tobject );
Procedure timer1timer (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;
Serversocket: tsocket;
Clientsocket: tsocket;

Implementation

{$ R*. DFM}

Procedure tform1.button1click (Sender: tobject );
VaR
Wsdata: wsadata;

Localaddr: tsockaddrin;
Socketmode: integer;
Begin
//Initialize Winsock
Wsastartup ($202, Wsdata );
//Create socket
Serversocket:=Socket (af_inet, sock_stream, ipproto_tcp );

// Set localaddr Parameters
Localaddr. sin_family: = Af_inet; // IPv4
Localaddr. sin_addr.s_addr: = Inet_addr ( ' 127.0.0.1 ' ); // Point string format IP address to Internet format
Localaddr. sin_port: = Htons ( 1077 ); // Host to net short, host byte order to network byte order
// Bind the local IP address and port. Set the localaddr parameters before binding.
BIND (serversocket, localaddr, sizeof (localaddr ));

//Set Winsock I/O mode
Socketmode:= 1;
Ioctlsocket (serversocket, fionbio, socketmode );

//Start listening. A maximum of five connections can be monitored at the same time.
Listen (serversocket,5);
Timer1.enabled:=True;
End;

{Tworkthread}

Constructor tworkthread. Create (clientsocket: tsocket; memo: tmemo );
Begin
Inherited create (false );

Fclientsocket:=Clientsocket;
Fmemo:=Memo;
End;

Procedure tworkthread. Execute;
VaR
Fdset: tfdset;
Begin
Inherited;

Freeonterminate:=True;

While Not terminated Do
Begin
Fd_zero (fdset ); // Initialize fdset
Fd_set (fclientsocket, fdset ); // Add fclientsocket to fdset
If Select ( 0 , @ Fdset, nil) >   0 Then // Test whether a readable connection exists in fdset.
Begin
If Recv (fclientsocket, fbuff, sizeof (fbuff ), 0 ) >   0 Then // If a message is received, it is displayed.
Synchronize (showrecv)
Else
Break;
End;
End;
End;

Procedure tworkthread. showrecv;
Begin
Fmemo. lines. Add (fbuff );
End;

Procedure tform1.timer1timer (Sender: tobject );
Begin
Clientsocket:=Accept (serversocket, nil, nil );
IfClientsocket<>Invalid_socket then
Tworkthread. Create (clientsocket, memo1 );
End;

End.

 

 

1. To use the select model, define an fdset structure, add the client socket to the structure, and use the select function to test the read/write status of the socket. Fdset structure:

Typedef struct fd_set {
U_int fd_count;
Socket fd_array [fd_setsize];
} Fd_set;

2. The fdset structure has four predefined macros:

Fd_clr (S, * Set)
Removes the descriptor s from set.
Fd_isset (S, * Set)
Nonzero if S is a member of the set. Otherwise, zero.
Fd_set (S, * Set)
Adds descriptor s to set.
Fd_zero (* Set)
Initializes the set to the null set.

3. The aboveCodeFor convenience, a timer is used to accept, and then a thread is created for each connected socket. In the thread, select is used to test whether the code is readable. In order to make the code simple, there is no code to handle exceptions.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dropme/archive/2009/09/09/4534748.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.