0430 简单 WINSOCK VB VC UDP

Option Explicit

Private Sub Command1_Click()
   Winsock1.SendData "11111111111"
End Sub

Private Sub Form_Load()
Winsock2.Bind 10003
 Winsock1.Bind 10002
 Winsock1.RemoteHost = "127.0.0.1"
    Winsock1.RemotePort = 10003
End Sub

Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock2.GetData strData
Debug.Print strData
End Sub

#include <afxsock.h>
CSocket m_SendSocket;
 WORD wVersionRequested;
 WSADATA wsaData;
 wVersionRequested = MAKEWORD( 2, 2 );

 if (WSAStartup( wVersionRequested, &wsaData ))
 &leftsign;
  return ;
 &rightsign;
 BOOL b=m_SendSocket.Create(10001,SOCK_DGRAM);//创建一个套接字句柄(UDP)
 DWORD dw=GetLastError();
 int i=m_SendSocket.SendTo( "123456",6,10003,"127.0.0.1");//发送数据给本地计算机

CSocket m_ReceiveSocket;
 char *buff1[256];

         //接收数据
 m_ReceiveSocket.Create(10001,SOCK_DGRAM);
 m_ReceiveSocket.Receive(buff1,256);

Class CSocket derives from CAsyncSocket and inherits its encapsulation of the Windows Sockets API. A CSocket object represents a higher level of abstraction of the Windows Sockets API than that of a CAsyncSocket object. CSocket works with classes CSocketFile and CArchive to manage the sending and receiving of data.

A CSocket object also provides blocking, which is essential to the synchronous operation of CArchive. Blocking functions, such as Receive, Send, ReceiveFrom, SendTo, and Accept (all inherited from CAsyncSocket), do not return a WSAEWOULDBLOCK error in CSocket. Instead, these functions wait until the operation completes. Additionally, the original call will terminate with the error WSAEINTR if CancelBlockingCall is called while one of these functions is blocking.

历史博文

标签:, , , , ,
十月 14, 2006 at 9:34 上午 by yippee 1,037 次
Category: Dev
Tags: , , , , ,