# 关于 winsock UDP 这样写也错吗？

Posted by trytsai on December 14, 1998 at 15:38:17:

我曾用 winsock 写过 TCP/IP 的程式，也都没什么问题
不过对于现在用 winsock 改写 UDP/IP 的程式却有问题！
能否请高手帮忙看看..

程式目的:
由 A 程式送出一段讯息给 B 程式然后 B 程式再回讯息给 A 程式.

假设 B 程式与 A 程式都在同一部 PC 上，所以 IP 都是 127.0.0.1
B 程式的 LocalPort 定为 1234

A 程式:
先在 text1 内输入要送出的讯息，再点一下 Command1 就会送到 B 程式
Private Sub Command1_Click()
Dim strSendMsg As String
strSendMsg = Time() & ":[" & Me.Text1.Text & "]"
Call Me.Winsock1.SendData(strSendMsg)
End Sub

Private Sub Form_Load()
Me.Winsock1.Protocol = sckUDPProtocol
Me.Winsock1.RemoteHost = "127.0.0.1"
Me.Winsock1.RemotePort = 1234
Me.Label1.Caption = "RemoteIP:" & Me.Winsock1.RemoteHost & " (" & Me.Winsock1.RemotePort & ")"
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
Me.Label1.Caption = Me.Label1.Caption & strData
End Sub

B 程式:
Private Sub Form_Load()
Me.Winsock1.Protocol = sckUDPProtocol
Me.Winsock1.LocalPort = 1234
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
Me.Label1.Caption = Me.Label1.Caption & strData
If (Me.Winsock1.State = sckConnected) Then
Me.Label2.Caption = Time() & ":Received[" & Me.Label1.Caption & "]"
Debug.Print Me.Winsock1.RemoteHost
Debug.Print Me.Winsock1.RemotePort
Call Me.Winsock1.SendData(Me.Label2.Caption)
End If
End Sub

- Jonathan's Web

Follow Ups:

- Re: 关于 winsock UDP 这样写也错吗？ Roger 15:55:56 12/14/98 (3)
  - Re: 关于 winsock UDP 这样写也错吗？ trytsai 16:31:52 12/14/98 (2)
    - 错!!!!!!!!!!!!!!!!!!!!!!!!!!!! windows NT user 18:35:00 12/18/98 (1)
      - Re: 错!!!!!!!!!!!!!!!!!!!!!!!!!!!! trytsai 17:04:12 12/28/98 (0)