求教!一个winsock接收数据的问题。

tfpe_24_11 2006-06-13 07:13:39
请问,当两台计算机用winsock连接以后,如果Server端发过来的的是用二进制保存的图像数据,在Client端如何接收并还原成原来的图片?
本人水平不高,望各位高手解释的详细一点儿,最好有源代码。
谢了先!
...全文
245 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
c4pt0r 2006-06-17
  • 打赏
  • 举报
回复
问题可能出在用WINSOCK.SENDDATA传输上面~~似乎每次传输有最大限制~~楼上的代码我没有测试过~~不好说啥~~~
下面转贴一个SENDFILE代码:
发送端:
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim flag As String
Winsock2.GetData flag

If flag = "ok" Then

Dim BytDate() As Byte
Dim FileName As String
Dim lngFile As Long
Dim i As Long
FileName = textPath.Text '取得文件名及路径
lngFile = FileLen(FileName) \ 1024 '取得文件长度
For i = 0 To lngFile
ReDim myFile(1023) As Byte
Open FileName For Binary As #1
Get #1, i * 1024 + 1, myFile
Close #1
Winsock1.SendData myFile
DoEvents
Next i
End If
End Sub

接受端:

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String

myPath = path

ReDim myFile(0 To bytesTotal - 1) '此处也可以是(0 To bytesTotal-1)

Winsock1.GetData myFile
Open myPath For Binary As #1 '新建文件
myLong = FileLen(myPath)
Put #1, myLong + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭


End Sub
tfpe_24_11 2006-06-17
  • 打赏
  • 举报
回复
能用了,谢谢!
tfpe_24_11 2006-06-16
  • 打赏
  • 举报
回复
谢谢你的解释,还有一个问题,我将接受的数据存储成 \2.jpg 可是在控件上加载的时候却出错了,我在程序外面也打不开这个图片(2.jpg)我的代码如下
发送端:
Me.Picture1 = Clipboard.GetData()
Me.Picture2.Width = Int(Me.Picture1.Width / 3.5)
Me.Picture2.Height = Int(Me.Picture1.Height / 4.3)
Picture2.PaintPicture Picture1.Picture, 0, 0, Picture2.Width, Picture2.Height, 0, 0, Picture1.Width, Picture1.Height

Clipboard.Clear
SavePicture Me.Picture2.Image, VB.App.Path & "\1.jpg"
Filename_1 = VB.App.Path & "\1.jpg"
LngFile = Len(Filename_1)

ReDim Pic_1(LngFile - 1) As Byte
Filenum_1 = FreeFile
Open VB.App.Path & "\1.jpg" For Binary As #Filenum_1
Get #Filenum_1, , Pic_1()
Close #Filenum_1

Me.Winsock1.SendData Pic_1()
接收端:
Dim Pic() As Byte
Dim Filenum_1 As Integer

Me.Winsock1.GetData Pic()

Filenum_1 = FreeFile
Open VB.App.Path & "\2.jpg" For Binary As #Filenum_1
Put #Filenum_1, , Pic()
Close #Filenum_1

Me.Image3.Picture = LoadPicture(VB.App.Path & "\2.jpg")
望不吝赐教
谢谢
xayzmb 2006-06-16
  • 打赏
  • 举报
回复
'*********************************************************

Private Sub cmdSend_Click()
'向服务器发送文件
Dim myFile() As Byte
Dim lngFile As Long '文件长度
Dim FileName As String '文件名称
Static i As Single

sockClient.Close
sockClient.Connect

Do
DoEvents
If sockClient.State = sckConnected Then
Exit Do
Else
i = i + 1
If i > 200000 Then
Dim box As String
box = MsgBox("是否放弃?", vbYesNo, "连接失败:")
If box = vbYes Then
i = 0
Exit Do
Else
i = 0
End If
End If
End If
DoEvents
Loop
lenth = 0
'检查是否与服务器连接
If sockClient.State = sckConnected Then

FileName = VB.App.Path & "\1.jpg" '取得文件名及路径
lngFile = FileLen(FileName) '取得文件长度
ReDim myFile(lngFile - 1) As Byte '初始化数组

Open FileName For Binary As #1 '打开文件
Get #1, , myFile '将文件写入数组
Close #1 '关闭文件

sockClient.SendData myFile '发送

Else
MsgBox "没有连接"
End If
End Sub

'******************************************************************

Private Sub sockServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'接收文件
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = VB.App.Path & "\a.doc"
cmdC.Caption = "正在接收数据... " & bytesTotal
ReDim myFile(0 To bytesTotal - 1) '此处也可以是(0 To bytesTotal-1)

sockServer(Index).GetData myFile
i = i + 1

Open myPath For Binary As #1 '新建文件
Put #1, lenth + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭
'记录文件长度
lenth = lenth + UBound(myFile) - LBound(myFile) + 1

End Sub

'******************************************************************
tfpe_24_11 2006-06-15
  • 打赏
  • 举报
回复
能给个例子吗?
本人水平不高
谢谢
苏门答腊 2006-06-15
  • 打赏
  • 举报
回复
先存到临时文件中,然后打开那个文件。就当作传输文件操作。

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧