📄 0,1410,26223,00.html
字号:
<HTML>
<HEAD>
<TITLE>Winsock Fundementals</TITLE>
</HEAD>
<BODY>
<A NAME="top"></A>
<table>
<TR>
<td>
<SPAN CLASS="title3"><b>Winsock Fundementals</b></SPAN>
<BR>
<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>This article covers the basic requirements to create a socket connection and send data across it.</BLOCKQUOTE>
<Center><B><H2>What are the basics of writing a
WinSock application?</H2> </B></Center>
<P> The basic steps required to write a WinSock application are analagous to those required to write a program that manipulates the contents of a file. Once you see how WinSock programming parralells File/IO, writing WinSock applications should become fairly easy. Note that this is a general overview. For a client application you only need to call the <B>socket</B>, and <B>connect</B> functions to establish a socket connection. For a server on the other hand, you have to call <B>socket</B>, <B>bind</B>, <B>listen</B>, and <B>accept</B> to establish a connection. Once the connection is established, you can then call <B>send</B> and <B>recv</B> for either. Also of note is that the server must call <B>closesocket</B> twice, once to close the connection, and once to close the listening socket.
<P> The basic steps are as follows:
<P> 1. Open the Socket with the <B>socket()</B> function call.
<P> 2. Name the socket using the <B>bind()</B> function.
<P> 3. Associate that socket. (This is a little more complicated than calling a function, more about that later...)
<P> 4. Send and recieve the data between the sockets using the <B>send()</B> and <B>recv()</B> functions.
<P> 5. Close the socket using the <B>closesocket()</B> function.
<P> Now that I have given the basic steps, I will go into more detail about each one in turn.
<B>socket()</B>
<P> The socket function takes three arguments, the first is af, or the protocol suite; the second is type, and the third is protocol. The <B>af</B> parameter has three possible values, <B>PF_INET, AF_IPX</B>, and <B>AF_APPLETALK</B>. The <B>PF_INET</B> value is the one that you will want to use for TCP/IP communication. The <B>AF_IPX</B> would be used for communication to Novell machines, and the <B>AF_APPLETALK</B> is used for connecting to Macintosh machines.
<P> The second parameter is the <B>type</B> parameter. This argument determines (implicitly) the protocol within the address family that you will be using. Possible values are <B>SOCK_DGRAM, SOCK_STREAM</B>, or <B>SOCK_RAW</B>. If you are using the <B>SOCK_DGRAM</B>, it will always indicate that you are using a UDP connection; whereas, if you are using the <B>SOCK_STREAM</B> argument, then that would indicate that you are using a TCP/IP connection. If such cases where the protocol is determined by the type argument, the third parameter (protocol) is ignored. In those cases, you should just pass in 0 to be on the safe side.
<P> You may be wondering what the <B>SOCK_STREAM</B> argument is for. The winsock specification defines this value, however, implimentations aren't required to use it. Therefor its use is discouraged for compatibility reasons. Just in case you were wondering what it would be use for, the <B>SOCK_RAW</B> type would give you complete access to the stream, including things like TCP/IP headers.
<P> Once you have called the <B>socket()</B> function, it will return the socket descriptor to you. If the call failed for some reason then it will return <B>INVALID_SOCKET</B>. From here on out we will only concern ourselves with TCP/IP applications.
<P> After you have created the socket, you need to associate that with a name using the <B>bind()</B> function. The <B>bind()</B> function takes three arguments, the first of which is the socket descriptor that was returned from our previous call to the socket function. The second value is a <B>sockaddr_in</B> structure. The <B>sockaddr_in</B> structure has four fields. The first is the protocol family (in this case it is <B>PF_INET</B>), the second value is the port number that we want to access. The fourth field is the IP address of the machine that we want to connect to. The fourth field is unused, so don't worry about it. The third value that it takes is the length of the aformentioned <B>sockaddr_in</B> structure using the <B>sizeof()</B> function.
<P> Once you have named the socket structure you need to associate that socket with another socket on the remote machine. This is accomplished with a TCP/IP connection by calling the <B>listen()</B> function. The <B>listen()</B> function only takes two parameters, the first is the socket that was returned by the first function call, and the second is the backlog value. A complete description of the backlog parameter is beyond the scope of the document, but suffice it to say, setting this value to 3 should be fine for most applications.
<P> Now that we have established a connection between our socket and the remote machine, we can start sending data between them. To accomplish this, we use the <B>send()</B> function. For this function call you should pass in our socket, the buffer that we want to send (cast to a char *), the length of the data, and a 0. If you want to receive data from the remote socket you can do so with the <B>recv()</B> function. It takes the same parameters as the <B>send()</B> function.
<P> After you have send and recieved data to your hearts content, you have to close the socket before exiting the program. There are two functions that you have to call to close a TCP/IP connection. The first function that you need to call is shutdown. This function takes two parameters. The first is our trusty socket, and the second is the value 2. For now don't worry about the second value, its discussion is beyond the scope of this article. After you call shutdown you need to call <B>closesocket()</B>. The only argument that <B>closesocket()</B> takes is the socket that we have been using all along.
<P> I hope that after reading this, you get a good overview of the steps necessary to write a simple TCP/IP socket application. If you would like to learn more about socket programming, there are several good books out there, as well as articles at <a href=http://msdn.microsoft.com>http://msdn.microsoft.com</a> .
</table>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -