📄 subject_38672.htm
字号:
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-05-05 12:36:14
<br>内容:好的<BR>我再查查资料
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-05-05 12:41:01
<br>内容:顺便问一下<BR>一般用得最多的<BR>Socket I/O Models<BR>是什么?<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Neil Gan 回复日期:2003-05-05 12:41:04
<br>内容:Please send your code to my mailbox. I will leave now.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-05-05 12:42:23
<br>内容:嘿嘿,找到了<BR>The select model is the most widely available I/O model in Winsock.<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-05-05 12:45:27
<br>内容:源代码在这里<BR>是network programming for ms windows里面的源代码<BR>我只修改了recvfrom,变成WSARecvFrom<BR><BR><BR>// Module Name: Receiver.c<BR>//<BR>// Description:<BR>// This sample receives UDP datagrams by binding to the specified<BR>// interface and port number and then blocking on a recvfrom() <BR>// call<BR>//<BR>// Compile:<BR>// cl -o Receiver Receiver.c ws2_32.lib<BR>//<BR>// Command line options:<BR>// sender [-p:int] [-i:IP][-n:x] [-b:x]<BR>// -p:int Local port<BR>// -i:IP Local IP address to listen on<BR>// -n:x Number of times to send message<BR>// -b:x Size of buffer to send<BR>//<BR>#include <winsock2.h><BR>#include <stdio.h><BR>#include <stdlib.h><BR><BR>#define DEFAULT_PORT 5150<BR>#define DEFAULT_COUNT 25<BR>#define DEFAULT_BUFFER_LENGTH 4096<BR><BR>int iPort = DEFAULT_PORT; // Port to receive on<BR>DWORD dwCount = DEFAULT_COUNT, // Number of messages to read<BR> dwLength = DEFAULT_BUFFER_LENGTH; // Length of receiving buffer<BR>BOOL bInterface = FALSE; // Use an interface other than<BR> // default<BR>char szInterface[32]; // Interface to read datagrams from<BR><BR>//<BR>// Function: usage:<BR>//<BR>// Description:<BR>// Print usage information and exit<BR>//<BR>void usage()<BR>{<BR> printf("usage: sender [-p:int] [-i:IP][-n:x] [-b:x]\n\n");<BR> printf(" -p:int Local port\n");<BR> printf(" -i:IP Local IP address to listen on\n");<BR> printf(" -n:x Number of times to send message\n");<BR> printf(" -b:x Size of buffer to send\n\n");<BR> ExitProcess(1);<BR>}<BR><BR>//<BR>// Function: ValidateArgs<BR>//<BR>// Description:<BR>// Parse the command line arguments, and set some global flags to<BR>// indicate what actions to perform<BR>//<BR>void ValidateArgs(int argc, char **argv)<BR>{<BR> int i;<BR><BR> for(i = 1; i < argc; i++)<BR> {<BR> if ((argv[i][0] == '-') || (argv[i][0] == '/'))<BR> {<BR> switch (tolower(argv[i][1]))<BR> {<BR> case 'p': // Local port<BR> if (strlen(argv[i]) > 3)<BR> iPort = atoi(&argv[i][3]);<BR> break;<BR> case 'n': // Number of times to receive message<BR> if (strlen(argv[i]) > 3)<BR> dwCount = atol(&argv[i][3]);<BR> break;<BR> case 'b': // Buffer size<BR> if (strlen(argv[i]) > 3)<BR> dwLength = atol(&argv[i][3]);<BR> break;<BR> case 'i': // Interface to receive datagrams on<BR> if (strlen(argv[i]) > 3)<BR> {<BR> bInterface = TRUE;<BR> strcpy(szInterface, &argv[i][3]);<BR> }<BR> break;<BR> default:<BR> usage();<BR> break;<BR> }<BR> }<BR> }<BR>}<BR><BR>//<BR>// Function: main<BR>//<BR>// Description:<BR>// Main thread of execution. Initialize Winsock, parse the command<BR>// line arguments, create a socket, bind it to a local interface <BR>// and port, and then read datagrams.<BR>//<BR>char str[100];<BR>int main(int argc, char **argv)<BR>{<BR> WSADATA wsd;<BR> SOCKET s;<BR> char *recvbuf = NULL;<BR> int ret,<BR> i;<BR> DWORD dwSenderSize,count;<BR> SOCKADDR_IN sender,<BR> local;<BR> WSABUF mybuf[10]; <BR> mybuf[0].buf=str;<BR> mybuf[0].len=100;<BR> // Parse arguments and load Winsock<BR> //<BR> ValidateArgs(argc, argv);<BR><BR> if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)<BR> {<BR> printf("WSAStartup failed!\n");<BR> return 1;<BR> }<BR> // Create the socket and bind it to a local interface and port<BR> //<BR> s = socket(AF_INET, SOCK_DGRAM, 0);<BR> if (s == INVALID_SOCKET)<BR> {<BR> printf("socket() failed; %d\n", WSAGetLastError());<BR> return 1;<BR> }<BR> local.sin_family = AF_INET;<BR> local.sin_port = htons((short)iPort);<BR> if (bInterface)<BR> local.sin_addr.s_addr = inet_addr(szInterface);<BR> else<BR> local.sin_addr.s_addr = htonl(INADDR_ANY);<BR> if (bind(s, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)<BR> {<BR> printf("bind() failed: %d\n", WSAGetLastError());<BR> return 1;<BR> }<BR> // Allocate the receive buffer<BR> //<BR> recvbuf = GlobalAlloc(GMEM_FIXED, dwLength);<BR> if (!recvbuf)<BR> {<BR> printf("GlobalAlloc() failed: %d\n", GetLastError());<BR> return 1;<BR> }<BR> // Read the datagrams<BR> //<BR> for(i = 0; i < dwCount; i++)<BR> {<BR> dwSenderSize=1;<BR> dwSenderSize = sizeof(sender);<BR> ret = WSARecvFrom(s,mybuf, 1,&count,0,// mybuf, 1, &count, 0,<BR> (SOCKADDR *)&sender, &dwSenderSize,0,0);<BR><BR> /* ret = recvfrom(s, recvbuf, dwLength, 0, <BR> (SOCKADDR *)&sender, &dwSenderSize);<BR>*/<BR> if (ret == SOCKET_ERROR)<BR> {<BR> printf("recvfrom() failed; %d\n", WSAGetLastError());<BR> break;<BR> }<BR> else if (ret == 0)<BR> break;<BR> else<BR> {<BR> recvbuf[ret] = '\0';<BR> printf("[%s] sent me: '%s'\n", <BR> inet_ntoa(sender.sin_addr), recvbuf);<BR> }<BR> }<BR> closesocket(s);<BR><BR> GlobalFree(recvbuf);<BR> WSACleanup();<BR> return 0;<BR>}<BR><BR>2003-5-5 12:47:39
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -