TCP 與 UDP 差異
熟悉網路的人,應該都聽過TCP與UDP,這裡我們簡單來
看看到底這兩個協定差別在那?
TCP(Transmission Control Protocol)
UDP(User Datagram Protocol)
TCP這個協定最主要的特色在於傳輸資料時,需要驗證資
料,確保正確性。所以花的時間稍多一點。
而UDP這個協定最主要的特色在於傳輸資料時,不需要驗
證資料,不保證正確性,所以比較省時間。而一般來說,
像是多媒體串流(streaming)就是使用這種協定。
參考資料:http://hazuki016.pixnet.net/blog/post/13288265-tcp%E8%88%87udp%E7%9A%84%E6%AF%94%E8%BC%83
連線實習
如何做-
按電腦圖示
改右邊的屬性
Client to server
Private Sub cmdConnect_Click()
Winsock1.LocalPort = "6666" '以便自動產生Local Port
Winsock1.Connect "192.168.15.31" '設定改成您 Server 電腦的IP 號碼
End Sub
Private Sub cmdExit_Click()
Winsock1.SendData "close"
DoEvents
Winsock1.Close
Winsock1.LocalPort = 0
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOutput.Text
DoEvents
End Sub
Private Sub Form_Load()
Winsock1.RemotePort = 7777 '設定與Server端做Listen的Port相同
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State <> sckClosed Then
cmdExit_Click
End If
End Sub
Private Sub Winsock1_Connect()
If Winsock1.State = sckConnected Then
lstInput.AddItem "Connected! LocalPort =" & Winsock1.LocalPort _
& " RemptePort = " & Winsock1.RemotePort
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim mydata As String
Winsock1.GetData mydata, vbString
lstInput.AddItem mydata
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
lstInput.AddItem Description
End Sub
server to Client
Option Explicit
Private Sub cmdSend_Click()
Winsock1.SendData txtSend.Text
End Sub
Private Sub Form_Load()
Winsock1.LocalPort = 7777
Winsock1.Listen '監聽
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
txtReceived.Text = strData
If strData = "close" Then
Winsock1.Close
cmdSend.Enabled = False
Winsock1.Listen
End If
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
MsgBox "有人敲門"
Winsock1.Close
Winsock1.Accept requestID '改成接受
cmdSend.Enabled = True
End Sub
沒有留言:
張貼留言