我是一个初学者,编的很烂,有不少bug,请大家多指教。我用的是TCP协议,请问改为UDP如何改?还有如果要能够发送图片,彩色字,应该怎么改。大家有谁会的帮帮忙。
Private Sub Form_Load()
cmdSend.Enabled = False
cmdClose.Enabled = False
sockSer.Listen '打开服务器进行侦听
End Sub
Private Sub cmdClose_Click()
sockClient.SendData "^Close^" '发送预先约好的关闭代码
End Sub
Private Sub cmdConnect_Click()
sockClient.RemoteHost = txtIP.Text '为客户机设置远程主机的主机名或ip
sockClient.Connect 'l连接远程主机
cmdSend.Enabled = True '激活按钮
cmdClose.Enabled = True
cmdConnect.Enabled = False
End Sub
Private Sub cmdClear_Click()
lstRecord.Clear '清楚记录
End Sub
Private Sub cmdExit_Click()
'检查WinSock控件是否关闭连接,如果没有,则关闭,然后退出程序
If sockClient.State <> sckClosed Then sockClient.Close
If sockSer.State <> sckClosed Then sockSer.Close
End
End Sub
Private Sub cmdSend_Click()
Dim strSend As String
strSend = sockClient.LocalHostName + ":" + txtSend.Text
sockClient.SendData strSend '发送字符串
lstRecord.AddItem strSend '将发送的信息添加到记录列表
lstRecord.ToolTipText = lstRecord.NewIndex '将新记录置于顶端
txtSend.Text = ""
End Sub
Private Sub sockClient_Connect()
MsgBox "连接成功!", vbInformation, "提示"
cmdConnect.Enabled = False
End Sub
'如果连接时发生错误,将执行下面的程序段
Private Sub sockClient_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)
MsgBox "连接出错!关闭连接!", vbInformation, "警告"
sockClient.Close
cmdConnect.Enabled = True
End Sub
Private Sub sockSer_ConnectionRequest(ByVal requestID As Long)
If sockSer.State <> sckClosed Then sockSer.Close
sockSer.Accept requestID
End Sub
Private Sub sockSer_DataArribal(ByVal bytesTotal As Long)
Dim strGet As String
sockSer.GetData strGet '服务器获取数据
If strGet = "^Close^" Then '如果手收到预定的关闭信号
sockSer.Close
sockSer.Listen
End If
lstRecord.AddItem strGet
lstRecord.TopIndex = lstRecord.NewIndex
End Sub