📄 0,1410,26276,00.html
字号:
<HTML>
<HEAD>
<TITLE>Tutorial for creating a threaded socket server and client application</TITLE>
</HEAD>
<BODY>
<A NAME="top"></A>
<table>
<TR>
<td>
<P>
<SPAN CLASS="title3">Tutorial for creating a threaded socket server and client application</SPAN>
<BR>
<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>Deriving from TServerClientThread to create a threaded socket server application</BLOCKQUOTE><P>
</table>
<B>Tutorial: Creating a threaded socket server
and client application</B>
<br>
<p>This tutorial provides source code and explanations of how to create
a threaded
<br>socket server (and a complimentary client application). The server
creates threads
<br>upon the Clients request, and will wait a specified duration during
the thread execution
<br>for the Client to respond. Threads for server connections are
descendants of
<br>TServerClientThread and because of this, you can抰 use the New Thread
object dialog.
<br>Instead, you must declare your thread manually as by descending from
TServerClientThread.
<br>
<p>//---------------------------------------------------------------------------
<br>// *****************
ServerMain.cpp *****************
<br>//---------------------------------------------------------------------------
<br>// Requires: TServerSocket, TMemo
<br>#include <vcl.h>
<br>#pragma hdrstop
<br>#include "ServerMain.h"
<br>//---------------------------------------------------------------------------
<br>#pragma package(smart_init)
<br>#pragma resource "*.dfm"
<br>TForm1 *Form1;
<br>//---------------------------------------------------------------------------
<br>// Wait 60 seconds for client
<br>const int CLIENTWAITTIME = 60000;
<br>// Size of buffer for reading/writing text across socket connection
<br>const int BUFFERSIZE = 32;
<br>//---------------------------------------------------------------------------
<p>//---------------------------------------------------------------------------
<br>__fastcall TForm1::TForm1(TComponent* Owner)
<br> : TForm(Owner)
<br>{
<br>}
<br>//---------------------------------------------------------------------------
<p>//---------------------------------------------------------------------------
<br>// Instead of using a client socket component that you place in your
application
<br>// from the Component palette, the server client thread must use the
TServerClientWinSocket
<br>// object that is created when the listening server socket accepts
a client connection.
<br>// This is available as the public ClientSocket property. In addition,
you can use the
<br>// protected HandleException method rather than writing your own thread-safe
exception
<br>// handling.
<br>void __fastcall TMyServerThread::ClientExecute(void)
<br>{
<br> // make sure connection is active
<br> while (!Terminated && ClientSocket->Connected)
<br> {
<br> try
<br> {
<br> // Now, use TWinSocketStream
to read or write information
<br> // over a blocking
socket connection
<br> TWinSocketStream *pStream
= new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
<p> try
<br> {
<br>
char buffer[BUFFERSIZE];
<br>
memset( buffer, 0, sizeof(buffer) );
<p> //
give the client 60 seconds to start writing
<br>
if (pStream->WaitForData(CLIENTWAITTIME))
<br>
{
<br>
if (pStream->Read(buffer, sizeof(buffer)) == 0)
<br>
// (if can't read in 60 seconds) than close the connection
<br>
ClientSocket->Close();
<br>
else
<br>
{
<br>
// Client to Server test text
<br>
Form1->Memo1->Lines->Add(AnsiString("(Client) ") +AnsiString(buffer) );
<p>
// Back again to Client
<br>
pStream->Write( buffer, sizeof(buffer));
<br>
}
<p>
// ...
<br>
// Process requests here.
<br>
// ...
<p> }
<br>
else
<br>
ClientSocket->Close();
<br> }
<br> __finally
<br> {
<br>
delete pStream;
<br> }
<br> }
<br> catch (...)
<br> {
<br> HandleException();
<br> }
<br> }
<br>}
<br>//---------------------------------------------------------------------------
<p>//---------------------------------------------------------------------------
<br>void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
<br> TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
<br>{
<br> SocketThread = new TMyServerThread(false, ClientSocket);
<br>}
<br>//---------------------------------------------------------------------------
<br>// *******************************************************
<br>//---------------------------------------------------------------------------
<br>
<br>
<p>//---------------------------------------------------------------------------
<br>// ******************
ServerMain.h ******************
<br>//---------------------------------------------------------------------------
<br>//---------------------------------------------------------------------------
<br>#ifndef ServerMainH
<br>#define ServerMainH
<br>//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -