📄 multi-threaded client-server socket class - the code project - internet & network.mht
字号:
src=3D"http://www.codeproject.com/script/images/t.gif" =
width=3D160> </TD>
<TD width=3D"100%">
<TABLE class=3DSearchHeaderBar cellSpacing=3D0 =
width=3D"100%">
<TBODY>
<TR>
<TD vAlign=3Dcenter noWrap align=3Dright width=3D"60%">
<FORM style=3D"MARGIN: 0px" name=3DSearch =
action=3D/info/search.asp=20
method=3Dpost><B>Search </B><INPUT =
class=3DsmallText=20
style=3D"WIDTH: 200px" name=3Dtarget> <SELECT=20
style=3D"FONT-WEIGHT: bold; FONT-SIZE: 8pt" name=3Dst> =
<OPTION=20
value=3Dkw selected>Articles</OPTION> <OPTION=20
value=3Dau>Authors</OPTION></SELECT> <INPUT =
style=3D"FONT-WEIGHT: bold; FONT-SIZE: 8pt" type=3Dsubmit value=3D" Go! =
">=20
</FORM></TD>
<TD class=3DtinyText noWrap><A=20
=
href=3D"http://www.codeproject.com/info/search.asp">Advanced=20
Search</A><BR><A=20
=
href=3D"http://www.codeproject.com/script/articles/sections.asp">Sitemap<=
/A>=20
</TD></TR></TBODY></TABLE>
<TABLE width=3D"100%">
<TBODY>
<TR vAlign=3Dtop>
<TD class=3DSmallText noWrap>
<TABLE>
<TBODY>
<TR>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/images/print.gif"=20
align=3DabsMiddle> <A=20
=
href=3D"http://www.codeproject.com/internet/serversocket.asp?print=3Dtrue=
"=20
target=3D_print>Print</A></TD>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/script/images/sitebuild_icon.gif"=20
align=3DabsMiddle><A=20
=
href=3D"http://www.codeproject.com/script/submit/ReportProblem.asp?GUID=3D=
ServerSocket%2Finternet6%2F23%2F2002">Broken=20
Article?</A></TD>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/images/link.gif"=20
align=3DabsMiddle><A=20
=
href=3D"http://www.codeproject.com/script/profile/add_bookmark.asp?t=3D0&=
amp;ct=3D%2Finternet%2Fserversocket%2Easp&guid=3DServerSocket%2Finter=
net6%2F23%2F2002">Bookmark</A></TD>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/images/mail_small.gif"=20
align=3DabsMiddle> <A=20
=
href=3D"http://www.codeproject.com/internet/serversocket.asp#__comments">=
Discuss</A></TD>
<TD class=3DsmallText noWrap><IMG=20
=
src=3D"http://www.codeproject.com/images/mail.gif"=20
align=3DabsMiddle> <A=20
=
href=3D"http://www.codeproject.com/script/recommend/form.asp?guid=3DServe=
rSocket%2Finternet6%2F23%2F2002">Send=20
to a friend</A></TD></TR></TBODY></TABLE></TD>
<TD noWrap align=3Dright><A name=3D__top></A>
<TABLE>
<TBODY>
<TR>
<TD class=3DsmallText align=3Dright>127 votes for =
this=20
article.</TD>
<TD>
<TABLE cellSpacing=3D0 cellPadding=3D0 =
border=3D2>
<TBODY>
<TR>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D7 border=3D0><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/white.gif"=20
width=3D13 =
border=3D0></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=3DsmallText align=3Dright colSpan=3D2><A =
title=3D"Calculated as rating x Log10(# votes)"=20
=
href=3D"http://www.codeproject.com/script/articles/top_articles.asp?st=3D=
2">Popularity:=20
9.18</A>. Rating: <B>4.36</B> out of=20
=
5.</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=3DArticlePane><SPAN id=3DintelliTXT>
<DIV id=3Dcontentdiv><!-- Article Starts -->
<UL class=3Ddownload>
<LI><A=20
=
href=3D"http://www.codeproject.com/internet/ServerSocket/ServerSocket_dem=
o.zip">Download=20
ServerSocket demo project - 47.3 Kb</A> </LI></UL>
<H2>Screenshots</H2>
<P><B>Note:</B> The demo can be started in Client or Server =
mode,=20
execute with "/C" (or "/CLIENT") or "/S" (or "/SERVER", this =
is the=20
default).</P>
<P><IMG height=3D284 alt=3D"Server Socket App - Screenshot"=20
=
src=3D"http://www.codeproject.com/internet/ServerSocket/serversocket.gif"=
=20
width=3D350></P>
<P><IMG height=3D284 alt=3D"Client Socket App - Screenshot"=20
=
src=3D"http://www.codeproject.com/internet/ServerSocket/clientsocket.gif"=
=20
width=3D350></P>
<H2>Introduction</H2>
<P>This article is about a client/server multi-threaded =
socket=20
class. The thread is optional since the developer/designer =
is still=20
responsible to decide if he/she needs it. There are other =
Socket=20
classes here and other places over the Internet but none of =
them can=20
provide feedback (event detection) to your application like =
this one=20
does. It provides you with the following events detection:=20
connection established, connection dropped, connection =
failed and=20
data reception (including 0 byte packet).</P>
<H2>Description</H2>
<P>This article presents a new socket class which supports =
both TCP=20
and UDP communication. But it provides some advantages =
compared to=20
other classes that you may find here or on some other Socket =
Programming articles. First of all, this class doesn't have =
any=20
limitation like the need to provide a window handle to be =
used. This=20
limitation is bad if all you want is a simple console =
application.=20
So this library doesn't have such a limitation. It also =
provides=20
threading support automatically for you, which handles the =
socket=20
connection and disconnection to a peer. It also features =
some=20
options not yet found in any socket classes that I have seen =
so far.=20
It supports both client and server sockets. A server socket =
can be=20
referred as to a socket that can accept many connections. =
And a=20
client socket is a socket that is connected to server =
socket. You=20
may still use this class to communicate between two =
applications=20
without establishing a connection. In the latter case, you =
will want=20
to create two UDP server sockets (one for each application). =
This=20
class also helps reduce coding need to create chat-like =
applications=20
and IPC (Inter-Process Communication) between two or more=20
applications (processes). Reliable communication between two =
peers=20
is also supported with TCP/IP with error handling. You may =
want to=20
use the smart addressing operation to control the =
destination of the=20
data being transmitted (UDP only). TCP operation of this =
class deals=20
only with communication between two peers.</P>
<P>Now for those not familiar with IP Socket, the following =
section=20
will give some details on how it works. This is also the =
goal with=20
this article: explain the basic functionality behind socket=20
objects.</P>
<H3>TCP/IP stack</H3>
<P>The TCP/IP stack is shorter than the OSI one:</P>
<P><IMG height=3D328=20
=
src=3D"http://www.codeproject.com/internet/ServerSocket/tcp_stack.gif"=20
width=3D437></P>
<P>TCP is a connection-oriented protocol, UDP (User Datagram =
Protocol) is a connectionless protocol.</P>
<H3>IP datagrams</H3>
<P>The IP layer provides a connectionless and unreliable =
delivery=20
system. It considers each datagram independently of the =
others. Any=20
association between datagrams must be supplied by the higher =
layers.=20
The IP layer supplies a checksum that includes its own =
header. The=20
header includes the source and destination addresses. The IP =
layer=20
handles routing through an Internet. It is also responsible =
for=20
breaking up large datagrams into smaller ones for =
transmission and=20
reassembling them at the other end.</P>
<H3>UDP</H3>
<P>UDP is also connectionless and unreliable. What it adds =
to IP is=20
a checksum for the contents of the datagram and port =
numbers. These=20
are used to give a client/server model - see later.</P>
<H3>TCP</H3>
<P>TCP supplies logic to give a reliable connection-oriented =
protocol above IP. It provides a virtual circuit that two =
processes=20
can use to communicate.</P>
<H3>Internet addresses</H3>
<P>In order to use a service, you must be able to find it. =
The=20
Internet uses an address scheme for machines so that they =
can be=20
located. The address is a 32 bit integer which gives the IP =
address.=20
This encodes a network ID and more addressing. The network =
ID falls=20
into various classes according to the size of the network=20
address.</P>
<H3>Network address</H3>
<P>Class A uses 8 bits for the network address with 24 bits =
left=20
over for other addressing. Class B uses 16 bit network =
addressing.=20
Class C uses 24 bit network addressing and class D uses all =
32.</P>
<H3>Subnet address</H3>
<P>Internally, the Unix network is divided into subnetworks. =
Building 11 is currently on one subnetwork and uses 10-bit=20
addressing, allowing 1024 different hosts.</P>
<H3>Host address</H3>
<P>8 bits are finally used for host addresses within our =
subnet.=20
This places a limit of 256 machines that can be on the =
subnet.</P>
<H3>Total address</H3>
<P><IMG height=3D145=20
=
src=3D"http://www.codeproject.com/internet/ServerSocket/inet_addr.gif"=20
width=3D330></P>
<P>The 32 bit address is usually written as 4 integers =
separated by=20
dots.</P>
<H3>Port addresses</H3>
<P>A service exists on a host, and is identified by its =
port. This=20
is a 16 bit number. To send a message to a server, you send =
it to=20
the port for that service of the host that it is running on. =
This is=20
not location transparency! Certain of these ports are "well=20
known".</P>
<P>For example,</P>
<TABLE cols=3D3 width=3D400 border=3D0>
<TBODY>
<TR>
<TD width=3D"40%">tcpmux</TD>
<TD width=3D"20%">1</TD>
<TD width=3D"40%">TCP</TD></TR>
<TR>
<TD width=3D"40%">echo</TD>
<TD width=3D"20%">7</TD>
<TD width=3D"40%">UDP</TD></TR>
<TR>
<TD width=3D"40%">echo</TD>
<TD width=3D"20%">7</TD>
<TD width=3D"40%">TCP</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -