网络传输图片的问题
传送图片接收不完整,请大家看一下是什么问题,帮我修改一下源代码,谢谢服务端代码
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Public Class Form1
Dim 服务端Socket As Socket
Dim 客户端Socket As Socket
Dim 客户端线程变量 As Thread
Dim 接收数据线程变量 As Thread
Dim Path As String = Application.StartupPath
Dim 文件名 As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
开始监听客户端()
End Sub
Private Sub 开始监听客户端()
客户端线程变量 = New Thread(AddressOf 监听客户端线程)
客户端线程变量.Start()
End Sub
Private Sub 停止监听客户端()
客户端Socket.Close()
End Sub
Private Sub 监听客户端线程()
Try
Dim localEndPoint As New IPEndPoint(Net.IPAddress.Parse("183.35.13.199"), 9000)
服务端Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
服务端Socket.Bind(localEndPoint)
服务端Socket.Listen(2000)
While True
Try
客户端Socket = 服务端Socket.Accept()
RichTextBox1.Text = "客户端已经连接上服务器" + vbNewLine
接收数据线程变量 = New Thread(AddressOf 接收数据线程)
接收数据线程变量.Start()
Catch ex As Exception
客户端线程变量.Abort()
End Try
End While
Catch e As SocketException
Finally
服务端Socket.Close()
End Try
End Sub
Private Sub 接收数据线程()
Dim bytes() As Byte = New [Byte](1024 * 100) {}
Dim data As String = ""
Dim tokens() As String
Dim length As Integer
While True
Dim bytesRec As Integer
While True
Try
bytesRec = 客户端Socket.Receive(bytes)
Catch ex As Exception
接收数据线程变量.Abort()
End Try
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
data = Trim(data)
data = data.Replace(Chr(0), "")
tokens = data.Trim.Split("|")
Select Case tokens(0)
Case "消息"
RichTextBox1.Text += tokens(1) + vbNewLine
Case "文件"
文件名 = tokens(1)
发送消息("开始传输文件")
length = 客户端Socket.Receive(bytes)
Dim writer As FileStream = New FileStream(Path + "\bmp2.jpg", FileMode.OpenOrCreate, FileAccess.Write)
writer.Write(bytes, 0, length)
writer.Close()
Case "开始传输文件"
Dim read As FileStream = New FileStream(Path + "\bmp.jpg", FileMode.Open, FileAccess.Read)
length = read.Read(bytes, 0, 1024 * 100)
客户端Socket.Send(bytes, 0, length, SocketFlags.None)
read.Close()
Case "退出"
客户端Socket.Shutdown(SocketShutdown.Both)
客户端Socket.Close()
接收数据线程变量.Abort()
End Select
End While
End While
End Sub
Private Function 发送消息(ByVal 消息 As String) As Boolean
Dim MsgByte As Byte() = Encoding.UTF8.GetBytes(消息)
Try
客户端Socket.Send(MsgByte)
Return True
Catch ex As Exception
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
发送消息("消息|" + TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
发送消息("文件|" + "bmp.bmp")
End Sub
End Class
客户端代码
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Public Class Form1
Dim 客户端Socket As Socket
Dim 接收数据线程变量 As Thread
Dim Path As String = Application.StartupPath
Dim 文件名 As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
连接服务端()
End Sub
Private Function 连接服务端() As Boolean
Dim remoteEP As New IPEndPoint(Net.IPAddress.Parse("183.35.13.199"), 9000)
客户端Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
客户端Socket.Connect(remoteEP)
接收数据线程变量 = New Thread(AddressOf 接收数据线程)
接收数据线程变量.Start()
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub 接收数据线程()
Dim bytes() As Byte = New [Byte](1024 * 100) {}
Dim data As String = ""
Dim tokens() As String
Dim length As Integer
While True
Dim bytesRec As Integer
While True
Try
bytesRec = 客户端Socket.Receive(bytes)
Catch ex As Exception
接收数据线程变量.Abort()
End Try
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
tokens = data.Trim.Split("|")
Select Case tokens(0)
Case "消息"
RichTextBox1.Text += tokens(1) + vbNewLine
Case "文件"
文件名 = tokens(1)
发送消息("开始传输文件")
length = 客户端Socket.Receive(bytes)
Dim writer As FileStream = New FileStream(Path + "\bmp2.jpg", FileMode.OpenOrCreate, FileAccess.Write)
writer.Write(bytes, 0, length)
writer.Close()
Case "开始传输文件"
Dim read As FileStream = New FileStream(Path + "\bmp.jpg", FileMode.Open, FileAccess.Read)
length = read.Read(bytes, 0, 1024 * 100)
客户端Socket.Send(bytes, 0, length, SocketFlags.None)
read.Close()
Case "退出"
客户端Socket.Shutdown(SocketShutdown.Both)
客户端Socket.Close()
接收数据线程变量.Abort()
End Select
End While
End While
End Sub
Private Function 发送消息(ByVal 消息 As String) As Boolean
Dim MsgByte As Byte() = Encoding.UTF8.GetBytes(消息)
Try
客户端Socket.Send(MsgByte)
Return True
Catch ex As Exception
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
发送消息("消息|" + TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
发送消息("文件|" + "bmp.bmp")
End Sub
End Class
--------------------编程问答-------------------- 服务器端发送文件的时候用以下方法,不要只扔一句Socket.send
Public Shared Function SendVarData(ByVal s As System.Net.Sockets.Socket, ByVal data() As Byte) As Integer
Dim total As Integer = 0
Dim size As Integer = data.Length
Dim dataleft As Integer = size
Dim sent As Integer
Dim datasize(3) As Byte
datasize = BitConverter.GetBytes(size)
Try
sent = s.Send(datasize)
While total < size '如果发送数不对,则重新断点发送
sent = s.Send(data, total, dataleft, Net.Sockets.SocketFlags.None)
total += sent
dataleft -= sent
End While
Return total
Catch ex As Exception
MsgBox(ex.Message)
Throw (ex)
End Try
End Function
客户端接收文件时用这个
Public Shared Function ReceiveVarData(ByVal s As System.Net.Sockets.Socket) As Byte()
Dim total As Integer = 0
Dim recv As Integer
Dim datasize(3) As Byte
Try
recv = s.Receive(datasize, 0, 4, Net.Sockets.SocketFlags.None)
Dim size As Integer = BitConverter.ToInt32(datasize, 0)
Dim dataleft As Integer = size
Dim data(size - 1) As Byte
While total < size '接受的字节小于size 则重新从断点开始接受
recv = s.Receive(data, total, dataleft, Net.Sockets.SocketFlags.None)
If recv = 0 Then
data = Nothing
Exit While
End If
total += recv
dataleft -= recv
End While
Return data
Catch ex As Exception
MsgBox(ex.Message)
Throw (ex)
End Try
End Function
--------------------编程问答-------------------- 看不明白!
补充:.NET技术 , VB.NET