青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

大龍的博客

常用鏈接

統(tǒng)計

最新評論

MSG_PARTIAL 選項解釋(畫----的那部分)

Connectionless Communication
Connectionless communication behaves differently than connection-oriented communication, so the method for sending and receiving data is substantially different. First we'll discuss the receiver (or server, if you prefer) because the connectionless 
receiver requires little change when compared with the connection-oriented servers. After that we'll look at the sender.

In IP, connectionless communication is accomplished through UDP/IP. UDP doesn't guarantee reliable data transmission and is capable of sending data to multiple destinations and receiving it from multiple sources. For example, if a client sends data to 
a server, the data is transmitted immediately regardless of whether the server is ready to receive it. If the server receives data from the client, it doesn't acknowledge the receipt. Data is transmitted using datagrams, which are discrete message 
packets.

Receiver
The steps in the process of receiving data on a connectionless socket are simple. First, create the socket with either socket or WSASocket. Next, bind the socket to the interface on which you wish to receive data. This is done with the bind function 
(exactly like the session-oriented example). The difference with connectionless sockets is that you do not call listen or accept. Instead, you simply wait to receive the incoming data. Because there is no connection, the receiving socket can receive 
datagrams originating from any machine on the network. The simplest of the receive functions is recvfrom, which is defined as

int recvfrom(
    SOCKET s,
    char FAR* buf,
    int len,
    int flags,
    struct sockaddr FAR* from,
    int FAR* fromlen
);
The first four parameters are the same as recv, including the possible values for flags: MSG_OOB and MSG_PEEK. The same warnings for using the MSG_PEEK flag also apply to connectionless sockets. The from parameter is a SOCKADDR structure for the given 
protocol of the listening socket, with fromlen pointing to the size of the address structure. When the API call returns with data, the SOCKADDR structure is filled with the address of the workstation that sent the data.

The Winsock 2 version of the recvfrom function is WSARecvFrom. The prototype for this function is

int WSARecvFrom(
    SOCKET s,
    LPWSABUF lpBuffers,
    DWORD dwBufferCount,
    LPDWORD lpNumberOfBytesRecvd,
    LPDWORD lpFlags,
    struct sockaddr FAR * lpFrom,
    LPINT lpFromlen,
    LPWSAOVERLAPPED lpOverlapped,
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
The difference is the use of WSABUF structures for receiving the data. You can supply one or more WSABUF buffers to WSARecvFrom with dwBufferCount indicating this. By supplying multiple buffers, scatter-gather I/O is possible. The total number of bytes 
read is returned in lpNumberOfBytesRecvd. When you call WSARecvFrom, the lpFlags parameter can be 0 for no options, MSG_OOB, MSG_PEEK, or MSG_PARTIAL. These flags can be bitwise OR together. If MSG_PARTIAL is specified when the function is called, the 
provider knows to return data even if only a partial message has been received. Upon return, the flag MSG_PARTIAL is set if only a partial message was received. Upon return, WSARecvFrom will store the address of the sending machine in the lpFrom 
parameter (a pointer to a SOCKADDR structure). Again, lpFromLen points to the size of the SOCKADDR structure, except that in this function it is a pointer to a DWORD. The last two parameters, lpOverlapped and lpCompletionRoutine, are used for 
overlapped I/O (which we'll discuss in Chapter 5).

Another method of receiving (and sending) data on a connectionless socket is to establish a connection. This might seem strange, but it's not quite what it sounds like. Once a connectionless socket is created, you can call connect or WSAConnect with 
the SOCKADDR parameter set to the address of the remote machine to communicate with. No actual connection is made, however. The socket address passed into a connect function is associated with the socket so recv and WSARecv can be used instead of 
recvfrom or WSARecvFrom because the data's origin is known. The capability to connect a datagram socket is handy if you intend to communicate with only one endpoint at a time in your application.

The following code sample demonstrates how to construct a simple UDP receiver application. You will find a complete version of this application in a file named UDPRECEIVER on the companion CD.

#include <winsock2.h>

void main(void)
{
   WSADATA              wsaData;
   SOCKET               ReceivingSocket;
   SOCKADDR_IN          ReceiverAddr;
   int                  Port = 5150;
   char                 ReceiveBuf[1024];
   int                  BufLength = 1024;
   SOCKADDR_IN          SenderAddr;
   int                  SenderAddrSize = sizeof(SenderAddr);
   
   // Initialize Winsock version 2.2

   WSAStartup(MAKEWORD(2,2), &wsaData);
   
      // Create a new socket to receive datagrams on.
 
      ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

      // Set up a SOCKADDR_IN structure that will tell bind that we
      // want to receive datagrams from all interfaces using port
      // 5150.

      ReceiverAddr.sin_family = AF_INET;
      ReceiverAddr.sin_port = htons(Port);    
      ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

      // Associate the address information with the socket using bind.

      bind(ReceivingSocket, (SOCKADDR *)&SenderAddr, sizeof(SenderAddr));

   // At this point you can receive datagrams on your bound socket.
   recvfrom(ReceivingSocket, ReceiveBuf, BufLength, 0,
            (SOCKADDR *)&SenderAddr, &SenderAddrSize);

   // When your application is finished receiving datagrams close
   // the socket.

   closesocket(ReceivingSocket);

   // When your application is finished call WSACleanup.

   WSACleanup();
}
Now that you understand how to construct a receiver that can receive a datagram, we will describe how to construct a sender.

Sender
There are two options to send data on a connectionless socket. The first, and simplest, is to create a socket and call either sendto or WSASendTo. We'll cover sendto first, which is defined as

int sendto(
    SOCKET s,
    const char FAR * buf,
    int len,
    int flags,
    const struct sockaddr FAR * to,
    int tolen
);
The parameters are the same as recvfrom except that buf is the buffer of data to send and len indicates how many bytes to send. Also, the to parameter is a pointer to a SOCKADDR structure with the destination address of the workstation to receive the 
data. The Winsock 2 function WSASendTo can also be used. This function is defined as

int WSASendTo(
    SOCKET s,                                               
    LPWSABUF lpBuffers,                                     
    DWORD dwBufferCount,                                    
    LPDWORD lpNumberOfBytesSent,                            
    DWORD dwFlags,                                          
    const struct sockaddr FAR * lpTo,                       
    int iToLen,                                             
    LPWSAOVERLAPPED lpOverlapped,                           
    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
Again, WSASendTo is similar to its ancestor. This function takes a pointer to one or more WSABUF structures with data to send to the recipient as the lpBuffers parameter, with dwBufferCount indicating how many structures are present. You can send 
multiple WSABUF structures to enable scatter-gather I/O. Before returning, WSASendTo sets the fourth parameter, lpNumberOfBytesSent, to the number of bytes actually sent to the receiver. The lpTo parameter is a SOCKADDR structure for the given 
protocol, with the recipient's address. The iToLen parameter is the length of the SOCKADDR structure. The last two parameters, lpOverlapped and lpCompletionRoutine, are used for overlapped I/O (discussed in Chapter 5).

As with receiving data, a connectionless socket can be connected to an endpoint address and data can be sent with send and WSASend. Once this association is established, you cannot go back to using sendto or WSASendTo with an address other than the 
address passed to one of the connect functions. If you attempt to send data to a different address, the call will fail with WSAEISCONN. The only way to disassociate the socket handle from that destination is to call connect with the destination address 
of INADDR_ANY.

The following code sample demonstrates how to construct a simple UDP sender application. You will find a complete version of this application on the companion CD in a file named UDPSENDER.

#include <winsock2.h>

void main(void)
{
   WSADATA              wsaData;
   SOCKET               SendingSocket;
   SOCKADDR_IN          ReceiverAddr;
   int                  Port = 5150;
   char                 SendBuf[1024];
   int                  BufLength = 1024;

   // Initialize Winsock version 2.2

   WSAStartup(MAKEWORD(2,2), &wsaData);
   
   // Create a new socket to receive datagrams on.
 
   SendingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   
   // Set up a SOCKADDR_IN structure that will identify who we
   // will send datagrams to. For demonstration purposes, let's
   // assume our receiver's IP address is 136.149.3.29 and waits
   // for datagrams on port 5150.

   ReceiverAddr.sin_family = AF_INET;
   ReceiverAddr.sin_port = htons(Port);    
   ReceiverAddr.sin_addr.s_addr = inet_addr("136.149.3.29");

   // Send a datagram to the receiver.

   sendto(SendingSocket, SendBuf, BufLength, 0, 
          (SOCKADDR *)&ReceiverAddr, sizeof(RecieverAddr));
          
   // When your application is finished sending datagrams close
   // the socket.

   closesocket(SendingSocket);

   // When your application is finished call WSACleanup.

   WSACleanup();
}
Message-Based Protocols
Just as most connection-oriented communication is also streaming, connectionless communication is almost always message-based. Thus, there are some considerations when you're sending and receiving data. First, because message-based protocols preserve 
data boundaries, data submitted to a send function blocks until completed. For non-blocking I/O modes, if a send cannot be completely satisfied, the send function returns with the error WSAEWOULDBLOCK. This means that the underlying system was not able 
to process that data and you should attempt the send call again at a later time. This scenario will be discussed in greater detail in Chapter 5. The main point to remember is that with message-based protocols, the write can occur as an autonomous 
action only.

---------------------------------------
On the flip side, a call to a receive function must supply a sufficiently large buffer. If the supplied buffer is not large enough, the receive call fails with the error WSAEMSGSIZE. If this occurs, the buffer is filled to its capacity, but the 
remaining data is discarded. The truncated data cannot be retrieved. The only exception is for protocols that do support partial messages, such as the AppleTalk PAP protocol. Prior to returning, the WSARecv, WSARecvEx, or WSARecvFrom functions set the 
in-out flag parameter to MSG_PARTIAL when it receives only part of a message.
---------------------------------------


For datagrams based on protocols supporting partial messages, consider using one of the WSARecv* functions because when you make a call to recv/recvfrom, there is no notification that the data read is only a partial message. It is up to the programmer 
to implement a method for the receiver to determine if the entire message has been read. Subsequent calls to recv/recvfrom return other pieces of the datagram. Because of this limitation, it can be convenient to use the WSARecvEx function, which allows 
the setting and reading of the MSG_PARTIAL flag to indicate if the entire message was read. The Winsock 2 functions WSARecv and WSARecvFrom also support this flag. See the descriptions for WSARecv, WSARecvEx, and WSARecvFrom for additional information 
about this flag.

Finally, let's take a look at one of the more frequently asked questions about sending UDP/IP messages on machines with multiple network interfaces: What happens when a UDP socket is bound explicitly to a local IP interface and datagrams are sent? With 
UDP sockets, you don't really bind to the network interface; you create an association whereby the IP interface that is bound becomes the source IP address of UDP datagrams sent. The routing table actually determines which physical interface the 
datagram is transmitted on. If you do not call bind but instead use either sendto/WSASendTo or perform a connect first, the network stack automatically picks the best local IP address based on the routing table. So if you explicitly bind first, the 
source IP address could be incorrect. That is, the source IP might not be the IP address of the interface on which the datagram was actually sent.

Releasing Socket Resources
Because there is no connection with connectionless protocols, there is no formal shutdown or graceful closing of the connection. When the sender or the receiver is finished sending or receiving data, it simply calls the closesocket function on the 
socket handle. This releases any associated resources allocated to the socket.

posted on 2008-05-07 09:45 大龍 閱讀(1639) 評論(1)  編輯 收藏 引用

評論

# re: MSG_PARTIAL 選項解釋(畫----的那部分) 2011-08-30 14:57 QVOD

很有幫助!  回復(fù)  更多評論   


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            日韩亚洲精品在线| 亚洲福利视频网| 国产麻豆日韩| 国产精品亚洲不卡a| 欧美视频免费| 国产精品自在线| 狠狠色香婷婷久久亚洲精品| 国产一区在线视频| 在线免费观看一区二区三区| 伊人影院久久| 在线亚洲美日韩| 亚洲专区国产精品| 亚洲无毛电影| aa级大片欧美三级| 亚洲在线电影| 美脚丝袜一区二区三区在线观看 | 欧美精品成人| 国产精品亚洲人在线观看| 狠狠色丁香久久综合频道| 亚洲欧洲精品一区二区| 亚洲一区美女视频在线观看免费| 欧美在线视频在线播放完整版免费观看 | 亚洲国产欧美日韩另类综合| 亚洲伦理中文字幕| 欧美一区二区啪啪| 亚洲东热激情| 欧美一区二区三区四区视频| 欧美国产免费| 好吊日精品视频| 亚洲一二三四区| 欧美二区在线观看| 亚洲欧美精品一区| 欧美人交a欧美精品| 一区二区三区中文在线观看 | 一区二区欧美激情| 裸体歌舞表演一区二区| 亚洲网站在线看| 欧美激情欧美狂野欧美精品| 国内精品免费在线观看| 亚洲欧美日韩系列| 亚洲精品乱码| 麻豆乱码国产一区二区三区| 国产日产欧美a一级在线| 亚洲天堂男人| 亚洲精品日韩一| 免费观看不卡av| 一区三区视频| 久久精品亚洲乱码伦伦中文 | 一区二区三区日韩精品视频| 欧美成人黄色小视频| 狠狠狠色丁香婷婷综合激情| 欧美亚洲专区| 亚洲一区欧美一区| 国产精品女人毛片| 亚洲在线中文字幕| 一本一本a久久| 国产精品jvid在线观看蜜臀 | 国产主播一区二区三区四区| 国产精品二区三区四区| 欧美日韩国产美| 91久久国产综合久久91精品网站 | 99在线热播精品免费| 欧美日韩精品二区| 亚洲一区二区三区久久| 亚洲天堂网站在线观看视频| 亚洲狼人精品一区二区三区| 久久精品国产精品亚洲精品| 亚洲欧美另类中文字幕| 欧美性猛交99久久久久99按摩| 这里只有精品在线播放| 日韩一级黄色av| 国产精品热久久久久夜色精品三区| 亚洲一区二区三区午夜| 亚洲视频欧洲视频| 国产欧美va欧美不卡在线| 久久婷婷国产综合国色天香| 久久永久免费| 亚洲天堂第二页| 久久激情网站| 一区二区国产精品| 欧美一区二区三区久久精品茉莉花 | 国产伦精品一区二区三区在线观看| 羞羞答答国产精品www一本| 午夜精品久久久久久| 在线观看91精品国产麻豆| 亚洲第一在线综合在线| 欧美精品在线播放| 午夜影院日韩| 欧美插天视频在线播放| 亚洲欧美成人精品| 欧美在线|欧美| 99v久久综合狠狠综合久久| 亚洲免费在线观看| 亚洲高清视频一区| 亚洲综合999| 亚洲人成人一区二区三区| 亚洲一区二区三| 亚洲欧洲一区二区在线播放| 午夜精品久久久久久99热软件| 亚洲欧洲综合| 欧美一区亚洲二区| 亚洲一区中文| 欧美电影免费观看网站| 欧美一区二区三区在线看| 免费亚洲网站| 久久国产精品72免费观看| 欧美国产欧美综合| 蜜桃av噜噜一区二区三区| 国产免费观看久久| 一区二区三区www| 日韩一区二区电影网| 久久五月天婷婷| 久久久久久色| 国产欧美精品一区aⅴ影院| 亚洲黄色免费电影| 国产精品自拍在线| 在线一区日本视频| 欧美成人综合| 一区二区在线看| 一区二区三区精品视频在线观看| 精品99一区二区| 午夜在线精品偷拍| 亚洲一区二区视频在线| 免费人成精品欧美精品| 久久精品99无色码中文字幕| 欧美色综合天天久久综合精品| 欧美搞黄网站| 一色屋精品视频免费看| 亚洲欧美综合国产精品一区| 亚洲伊人网站| 国产精品激情电影| 一区二区三区欧美在线观看| 在线亚洲观看| 欧美三级日本三级少妇99| 亚洲激情视频| 亚洲理论在线| 欧美日韩18| 亚洲视频在线观看| 亚洲女人天堂av| 国产乱人伦精品一区二区| 亚洲一区二区三区四区五区午夜 | 久久精品官网| 国产一区二区三区久久久久久久久| 午夜国产欧美理论在线播放 | 亚洲精品免费网站| 欧美国产日本在线| 亚洲人www| 亚洲一区二区在线视频| 国产精品你懂的在线| 久久国产精品久久久| 欧美国产精品人人做人人爱| 亚洲精品社区| 欧美日韩精品欧美日韩精品一| 亚洲无限av看| 玖玖玖国产精品| 夜夜嗨一区二区三区| 国产精品一国产精品k频道56| 欧美一区视频在线| 欧美成人免费全部| 一区二区激情视频| 国产日韩欧美在线播放| 久久夜色精品国产噜噜av| 亚洲精品美女在线观看| 欧美亚洲视频在线观看| 在线成人av.com| 欧美啪啪一区| 午夜激情一区| 亚洲激情在线| 欧美一区二区高清在线观看| 亚洲福利av| 国产精品网站在线| 牛牛精品成人免费视频| 一区二区三区成人精品| 久久婷婷国产麻豆91天堂| 亚洲图片自拍偷拍| 在线观看视频一区| 国产精品亚洲综合天堂夜夜| 麻豆av一区二区三区| 亚洲一区在线免费观看| 亚洲国产精品久久久| 久久国产精品色婷婷| 99视频精品免费观看| 蜜臀av一级做a爰片久久| 国产精品视频九色porn| 毛片av中文字幕一区二区| 亚洲一二三区在线| 欧美激情偷拍| 久久久亚洲高清| 亚洲一区二区三区欧美| 亚洲电影在线播放| 国产嫩草影院久久久久| 欧美韩日一区二区三区| 久久久久九九九| 午夜国产精品视频免费体验区| 亚洲日韩视频| 欧美国产日韩一区二区在线观看| 久久精品视频va| 欧美亚洲视频一区二区| 亚洲系列中文字幕| 一卡二卡3卡四卡高清精品视频|