vb.net winform socket如何传输多个文件
我现在在做socket传输文件,可是我只会做传输单个文件的,做传输多个的总也不成功请教各位了
我有源代码,应该很简单的就可以改成功的
就是我想给程序一个目录,然后她就可以把这个目录下的文件全都传送给服务
Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Threading
Public Class Client
Private Socket As Socket
Private ep As IPEndPoint
Private T As Thread
Private m_FileName As String
Private m_FileFullName As String
Private m_FileLen As Integer
'//状态枚举
Enum State
Connecting
Connected
Sending
Completed
Close
End Enum
'//构造函数
Sub New(ByRef _to_ip As String, ByRef _to_port As Integer, ByRef _file As String)
'检查输入参数
If Not JudgeIP(_to_ip) Then Throw New Exception("错误的IP地址!")
If _to_port < 0 Then Throw New Exception("端口号必须大于0!")
If _file = String.Empty OrElse Not File.Exists(_file) Then Throw New Exception("文件未找到!")
'//If each_bytes < 1024 Then Throw New Exception("EachPackSize must greater than 1024!")
SendFilePath = _file
If each_bytes >= m_FileLen Then Throw New Exception("文件小于1024字节!")
m_remoteHostIP = _to_ip
m_remoteHostPort = _to_port
End Sub
Public Sub SendFile()
T = New Thread(AddressOf Start)
T.Start()
End Sub
Private Sub Start()
Call initialize_Socket()
Call SendFileInfo()
'//////////////// 循环发送数据包
Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Socket.Connect(ep) '//
_state = State.Connected
Using br As New BinaryReader(New FileStream(m_FileFullName, FileMode.OpenOrCreate, FileAccess.Read))
Dim bytes(each_bytes - 1) As Byte
Dim times As Integer = budget(m_FileLen, each_bytes)(0)
Dim lastSend As Integer = budget(m_FileLen, each_bytes)(1)
Dim _progress_plus As Integer = FormatNumber(100 / times, 0)
_state = State.Sending
For i = 1 To times + 1
If i = times + 1 And lastSend <> 0 Then '最后一次发送
ReDim bytes(lastSend - 1)
br.Read(bytes, 0, lastSend)
_sentlen += lastSend
ElseIf i = times + 1 And lastSend = 0 Then '如果最后一次没有数据
Exit For
Else
br.Read(bytes, 0, each_bytes) '最后字节发送
_sentlen += each_bytes
End If
Socket.Send(bytes)
System.Threading.Thread.Sleep(5) '//important!
_bags += 1
_progress += _progress_plus
Next
_state = State.Completed
'关闭所有连接
Socket.Close()
End Using
_state = State.Close
_progress = 100
End Sub
'初始化Socket
Private Sub initialize_Socket()
Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
ep = New IPEndPoint(IPAddress.Parse(RemoteHostIP), RemoteHostPort)
End Sub
End Class
Public Class Server
Private Socket As Socket
Private ep As IPEndPoint
Private T As Thread
Private m_FileName As String
Private m_FileLen As Integer
'//状态枚举
Enum State
Connecting
Connected
Sending
Completed
Close
End Enum
'//构造函数
Sub New(ByRef _ip As String, ByRef _port As Integer, ByRef _path As String)
'检查输入参数
If Not JudgeIP(_ip) Then Throw New Exception("错误的IP地址!")
If _port < 0 Then Throw New Exception("端口号必须大于0!")
If _path.Contains(".") Then Throw New Exception("错误的存储目录!")
If Not _path.EndsWith("\") Then _path &= "\"
_SavePath = _path
_LocalHostIP = _ip
_LocalHostPort = _port
End Sub
'//在一个单独的线程中监听客户连接
Public Sub Receive()
T = New Thread(AddressOf Start) '建立新的线程
T.Start() '启动线程
End Sub
'//监听函数
Private Sub Start()
Call initialize_Socket()
Call Get_FileInfo()
'//////////////////循环接收数据包
Dim new_soc As Socket = Socket.Accept() '//创建一个新的socket数据包
_state = State.Connected
Using bw As New BinaryWriter(New FileStream(_SavePath & m_FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
Dim bytes(each_bytes - 1) As Byte
Dim times As Integer = budget(m_FileLen, each_bytes)(0)
Dim lastRecive As Integer = budget(m_FileLen, each_bytes)(1)
Dim _progress_plus As Integer = FormatNumber(100 / times, 0)
_state = State.Sending
For i = 1 To times + 1
If i = times + 1 And lastRecive <> 0 Then '最后一次接收
ReDim bytes(lastRecive - 1)
new_soc.Receive(bytes)
bw.Write(bytes, 0, bytes.Length)
_receivedlen += lastRecive
ElseIf i = times + 1 And lastRecive = 0 Then '如果最后一次没有数据
Exit For
Else
new_soc.Receive(bytes) '发送数据包
bw.Write(bytes, 0, bytes.Length)
_receivedlen += each_bytes
End If
System.Threading.Thread.Sleep(5)
_bags += 1
_progress += _progress_plus
Next
_state = State.Completed
'关闭所有连接
End Using
new_soc.Close()
Socket.Close()
_progress = 100
_state = State.Close
End Sub
'初始化Socket
Private Sub initialize_Socket()
If Socket IsNot Nothing Then Socket.Close()
Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '创建套接字
Dim ep As New IPEndPoint(IPAddress.Parse(_LocalHostIP), _LocalHostPort)
Socket.Bind(ep) '绑定到SOCKET
Socket.Listen(10) '启动监听
End Sub
End Class
补充:.NET技术 , VB.NET