当前位置:编程学习 > VB >>

请问一下VB高手~~如何在这程序(winsock)基础下~继续编程可以传送图片文件、声音文件?最好给些代码来看看~~谢谢拉~下面是我已经完成好的程序代码,只能传

请问一下VB高手~~如何在这程序(winsock)基础下~继续编程可以传送图片文件、声音文件?最好给些代码来看看~~谢谢拉~下面是我已经完成好的程序代码,只能传送文字的代码!!但不知如何发送图形文件、声音文件~

服务器
Option Explicit
Private connectedflag As Boolean



Private Sub cmdCloseListen_Click()
If sckTCPserver.State = sckListening Or sckTCPserver.State = sckConnected Then
 sckTCPserver.Close
 connectedflag = False
 cmdCloseListen.Caption = "停止监听"
 cmdListen.Enabled = True
 cmdCloseListen.Enabled = False
 cmdSendData.Enabled = False
 ClientName.Caption = ""
 ClientIP.Caption = ""
 ClientPort.Caption = ""
 txtServerPort.Text = "5000"
End If


End Sub

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdListen_Click()
sckTCPserver.Close
On Error GoTo Listen_Err

sckTCPserver.LocalPort = CLng(txtServerPort.Text)
sckTCPserver.Listen
If sckTCPserver.State = sckListening Then

cmdCloseListen.Enabled = True
cmdCloseListen.Caption = "停止监听"
cmdListen.Enabled = False
txtServerPort.Text = CStr(sckTCPserver.LocalPort)
End If


listen_exit:
Exit Sub
Listen_Err:
Select Case Err
Case 380: MsgBox "端口属性值无效", vbOKOnly, "服务器警告"
Case 10048: MsgBox "端口已占用", vbOKOnly, "服务器警告"
Case Else: MsgBox "服务器错误:" & vbCrLf & CStr(Err), vbExclamation Or vbOKOnly, "服务器警告"
End Select
Resume listen_exit

End Sub

Private Sub cmdSendData_Click()

If Trim(txtSend.Text) = "" Then
txtSend.Text = ""
Exit Sub
End If
Dim data As String
On Local Error GoTo senddata_err

sckTCPserver.SendData txtSend.Text
If Len(txtReceive.Text) Then
data = vbCrLf & "服务器信息:" & txtSend.Text
Else
data = "服务器信息:" & txtSend.Text
End If
txtReceive.SelStart = Len(txtReceive.Text)
txtReceive.SelText = data
txtReceive.SelStart = Len(txtReceive.Text)
txtSend.Text = ""
senddata_exit:
Exit Sub
senddata_err:
Select Case Err
Case sckBadState: MsgBox Err.Description & vbCrLf & "服务器未连接到客户端", vbExclamation Or vbOKOnly, "服务器警告"
Case Else: MsgBox Err.Description & vbExclamation Or vbOKOnly, "服务器警告"
End Select
Resume senddata_exit
End Sub

Private Sub Form_Load()
Picture = LoadPicture(App.Path + "\20050925100417712.jpg")
labelLocalHostName.Caption = sckTCPserver.LocalHostName
LabelLocalHostIP.Caption = sckTCPserver.LocalIP
cmdListen.Enabled = True
cmdCloseListen.Enabled = False
cmdSendData.Enabled = False
txtServerPort.Text = "5000"
End Sub
Private Sub Form_Unload(Cancel As Integer)
sckTCPserver.Close


End Sub

Private Sub sckTCPserver_Close()
If connectedflag = True Then
If sckTCPserver.State = sckClosing Then
connectedflag = False
MsgBox "到客户端的连接意外终止。", vbExclamation Or vbOKOnly, "服务器警告"
sckTCPserver.Close
cmdCloseListen.Enabled = False
cmdListen.Enabled = True
cmdSendData.Enabled = False
ClientName.Caption = ""
ClientIP.Caption = ""
ClientPort.Caption = ""
txtServerPort.Text = "5000"
End If

End If

End Sub

Private Sub sckTCPserver_ConnectionRequest(ByVal requestID As Long)
If sckTCPserver.State <> sckClosed Then
sckTCPserver.Close
End If
sckTCPserver.Accept requestID
cmdCloseListen.Caption = "断开连接"

End Sub

Private Sub sckTCPserver_DataArrival(ByVal bytesTotal As Long)
Dim data As String
If connectedflag = True Then
sckTCPserver.GetData data
If Len(txtReceive.Text) Then
   data = vbCrLf & "客户端信息:" & data
Else
data = "客户端信息:" & data
End If
txtReceive.SelStart = Len(txtReceive.Text)
txtReceive.SelText = data
txtReceive.SelStart = Len(txtReceive.Text)

Else
connectedflag = True
sckTCPserver.GetData data
sckTCPserver.SendData sckTCPserver.LocalHostName
txtSend.SetFocus
cmdSendData.Enabled = True
ClientName.Caption = data
ClientIP.Caption = sckTCPserver.RemoteHostIP
ClientPort.Caption = sckTCPserver.RemotePort
End If

End Sub

Private Sub sckTCPserver_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 "服务器错误:" & vbCrLf & Description, vbExclamation Or vbOKOnly, "服务器警告"
sckTCPserver.Close

End Sub


Private Sub Timer1_Timer()
Select Case sckTCPserver.State
Case 0: StatusBar1.Panels(1) = "服务器已关闭"
Case 6: StatusBar1.Panels(1) = "服务器正在连接客户端"
Case 7: StatusBar1.Panels(1) = "与客户端建立连接"
End Select

End Sub

客户端
Option Explicit
Private connectedFlag As Boolean

Private Sub cmdConnect_Click()
On Error GoTo conncet_err
sckTCPClient.LocalPort = 0
sckTCPClient.RemoteHost = txtServerName.Text
sckTCPClient.RemotePort = CLng(txtServerPort.Text)
sckTCPClient.Connect
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
connect_exit:
Exit Sub

conncet_err: Select Case Err
 Case 40014: MsgBox "端口属性值无效", vbExclamation Or vbOKOnly Or vbMsgBoxSetForeground, "客户端警告"
 Case Else: MsgBox Err.Description, , vbExclamation Or vbOKOnly Or vbMsgBoxSetForeground, "客户端警告"
 End Select
Resume connect_exit


End Sub

Private Sub cmdDisconnect_Click()
If sckTCPClient.State = sckConnected Then
sckTCPClient.Close
connectedFlag = False
cmdDisconnect.Enabled = False
cmdConnect.Enabled = True
cmdSendData.Enabled = False
ServerIP.Caption = ""
ClientPort.Caption = ""
End If

End Sub

Private Sub cmdExit_Click()
Unload Me

End Sub

Private Sub cmdSendData_Click()
If Trim(txtSend.Text) = "" Then
txtSend.Text = ""
Exit Sub
End If
Dim data As String
On Local Error GoTo senddata_error
sckTCPClient.SendData txtSend.Text
If Len(txtReceive.Text) Then
data = vbCrLf & "客户端信息:" & txtSend.Text
Else
data = "客户端信息:" & txtSend.Text
End If
txtReceive.SelStart = Len(txtReceive.Text)
txtReceive.SelText = data
txtReceive.SelStart = Len(txtReceive.Text)
txtSend.Text = ""
senddata_exit:
          Exit Sub
senddata_error:
Exit Sub
Select Case Err
Case sckBadState:
MsgBox Err.Description & vbCrLf & "未连接到服务器", vbExclamation Or vbOKOnly, "客户端警告"
Case Else:
MsgBox Err.Description & vbExclamation Or vbOKOnly, "客户端警告"
End Select
Resume senddata_exit
End Sub


Private Sub Form_Load()
Picture = LoadPicture(App.Path + "\200462520403444.jpg")
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
cmdSendData.Enabled = False
txtServerName.Text = "computer"
txtServerPort.Text = "5000"
connectedFlag = False
ClientName.Caption = sckTCPClient.LocalHostName
ClientIP.Caption = sckTCPClient.LocalIP


End Sub

Private Sub Form_Unload(Cancel As Integer)
sckTCPClient.Close

End Sub

Private Sub sckTCPClient_Close()
If connectedFlag = True Then
If sckTCPClient.State = sckClosing Then
connectedFlag = False
MsgBox "到服务器的连接意外终止。", vbExclamation Or vbOKOnly, "客户端警告"
sckTCPClient.Close
cmdDisconnect.Enabled = False
cmdConnect.Enabled = True
cmdSendData.Enabled = False
ClientPort.Caption = ""
ServerIP.Caption = ""
End If
End If


End Sub


Private Sub sckTCPClient_Connect()
If connectedFlag = False Then
If sckTCPClient.State = sckConnected Then
sckTCPClient.SendData sckTCPClient.LocalHostName
ClientPort.Caption = sckTCPClient.LocalPort
ServerIP.Caption = sckTCPClient.RemoteHostIP
End If
txtSend.SetFocus
End If

End Sub



Private Sub sckTCPClient_DataArrival(ByVal bytesTotal As Long)
Dim data As String
If connectedFlag = True Then
sckTCPClient.GetData data
If Len(txtReceive.Text) Then
data = vbCrLf & "服务器信息:" & data
Else
data = "服务器信息:" & data
End If
txtReceive.SelStart = Len(txtReceive.Text)
txtReceive.SelText = data
txtReceive.SelStart = Len(txtReceive.Text)
Else
connectedFlag = True
sckTCPClient.GetData data
txtServerName.Text = data
txtSend.SetFocus
cmdSendData.Enabled = True
End If

End Sub

Private Sub sckTCPClient_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)
Select Case Number
Case 10061
MsgBox "错误:" & Number & vbCrLf & Description & vbCrLf & vbCrLf & "服务器端未运行或未建立连接。", vbExclamation Or vbOKOnly Or vbMsgBoxSetForeground, "客户端警告"
Case 10049
MsgBox "错误:" & Number & vbCrLf & Description & vbCrLf & vbCrLf & "服务器端口属性值无效", vbExclamation Or vbOKOnly Or vbMsgBoxSetForeground, "客户端警告"
Case Else
MsgBox "错误:" & Number & vbCrLf & Description, vbOKOnly Or vbExclamation Or vbMsgBoxSetForeground, "客户端警告"
End Select
sckTCPClient.Close
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False


End Sub

Private Sub Timer1_Timer()
Select Case sckTCPClient.State
Case 0: StatusBar1.Panels(1) = "客户端已关闭"
Case 6: StatusBar1.Panels(1) = "正在连接服务器"
Case 7: StatusBar1.Panels(1) = "已连接到服务端"
End Select

End Sub

请问一下VB高手~~如何在这程序(winsock)基础下~继续编程可以传送图片文件、声音文件?最好给些代码来看看~~谢谢拉~
--------------------编程问答-------------------- 关注,顶起来

试试使用二进制传输 --------------------编程问答-------------------- 将接收和发送数据改为二进制,定义一个字节(Byte)数组,数组维数是0-bytesTotal,然后保存在相应类型的文件中 --------------------编程问答-------------------- 将接收和发送数据改为二进制,定义一个字节(Byte)数组,数组维数是0-bytesTotal,然后保存在相应类型的文件中 --------------------编程问答-------------------- 二进制打开图片文件,装入字节数组,分包处理,发送处理,延时处理大体如此。
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,