SOCKET==============================Receive接收端 Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '初始化SOCKET Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim bytes() As Byte = New Byte(1024) {} Dim handler As Socket = listener.Accept() '建立连接 If handler Is Nothing Then MsgBox("Winsock error: " & Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())) End If Dim data As String = Nothing bytes = New Byte(1024) {} Dim bytesRec As Integer = handler.Receive(bytes) data += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec) '进行编码转换,将接收到的二进制数据转化为字符串进行显示 ReceMsg.Text = data Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data) handler.Shutdown(SocketShutdown.Both) handler.Close() Catch ex As Exception MsgBox(Err.Description) End Try End Sub Private Sub ChatReceiver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '指定IP和端口 Dim ipHostInfo As IPHostEntry = Dns.Resolve("cap35") Dim ipAddress As IPAddress = ipHostInfo.AddressList(0) Dim localEndPoint As New IPEndPoint(ipAddress, 11000) listener.Bind(localEndPoint) listener.Listen(10) '接收传来的文件 Dim hostipendpoint As New Net.IPEndPoint(Net.IPAddress.Parse("192.168.0.8"), 8888) receiveSocket.Bind(hostipendpoint) receiveSocket.Listen(2) '监听socket End Sub Dim receiveSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '接收传来的文件 Dim recfs As New IO.FileStream("p.doc", IO.FileMode.OpenOrCreate) Dim recbyte(229888) As Byte Dim hostsocket As Socket = receiveSocket.Accept Dim newfilestr As New IO.BinaryWriter(recfs) hostsocket.Receive(recbyte) newfilestr.Write(recbyte, 0, recbyte.Length - 1) recfs.Close() hostsocket.Shutdown(SocketShutdown.Receive) hostsocket.Close() End Sub =========================SEND发送端 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim bytes(1024) As Byte '声明字节数组用来保存要发送的数据 Dim sender1 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '初始化SOCKET Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(SendMsg.Text) '对发送的数据进行编码,将它转化为二进制格式 '指定IP和端口 Dim ipHostInfo As IPHostEntry = Dns.Resolve("cap35") Dim ipAddress As IPAddress = ipHostInfo.AddressList(0) Dim ipe As New IPEndPoint(ipAddress, 11000) sender1.Connect(ipe) '建立连接 Dim bytesSent As Integer = sender1.Send(msg) sender1.Shutdown(SocketShutdown.Both) sender1.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim SendSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipendpoint As New IPEndPoint(Net.IPAddress.Parse("192.168.0.8"), 8888) '指定IP和PORT Dim fs As New IO.FileStream("c:\p.doc", FileMode.OpenOrCreate, IO.FileAccess.Read) '将来传输的文件进行流处理 Dim fssize(fs.Length - 1) As Byte '设置要传输的文件大小 Dim strread As New IO.BinaryReader(fs) '流处理要传输的文件 strread.Read(fssize, 0, fssize.Length - 1) '将流处理过的要发送的文件读入到fssize中 SendSocket.Connect(ipendpoint) SendSocket.Send(fssize) Label2.Text = fs.Length '显示文件大小 fs.Close() SendSocket.Shutdown(SocketShutdown.Send) SendSocket.Close() End Sub
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '初始化SOCKET Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim bytes() As Byte = New Byte(1024) {}
Dim handler As Socket = listener.Accept() '建立连接
If handler Is Nothing Then
MsgBox("Winsock error: " & Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()))
End If
Dim data As String = Nothing
bytes = New Byte(1024) {}
Dim bytesRec As Integer = handler.Receive(bytes)
data += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec)
'进行编码转换,将接收到的二进制数据转化为字符串进行显示
ReceMsg.Text = data
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub Private Sub ChatReceiver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'指定IP和端口
Dim ipHostInfo As IPHostEntry = Dns.Resolve("cap35")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 11000)
listener.Bind(localEndPoint)
listener.Listen(10) '接收传来的文件
Dim hostipendpoint As New Net.IPEndPoint(Net.IPAddress.Parse("192.168.0.8"), 8888)
receiveSocket.Bind(hostipendpoint)
receiveSocket.Listen(2) '监听socket End Sub Dim receiveSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'接收传来的文件
Dim recfs As New IO.FileStream("p.doc", IO.FileMode.OpenOrCreate)
Dim recbyte(229888) As Byte
Dim hostsocket As Socket = receiveSocket.Accept
Dim newfilestr As New IO.BinaryWriter(recfs)
hostsocket.Receive(recbyte)
newfilestr.Write(recbyte, 0, recbyte.Length - 1)
recfs.Close()
hostsocket.Shutdown(SocketShutdown.Receive)
hostsocket.Close()
End Sub
=========================SEND发送端
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes(1024) As Byte '声明字节数组用来保存要发送的数据
Dim sender1 As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '初始化SOCKET
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(SendMsg.Text) '对发送的数据进行编码,将它转化为二进制格式
'指定IP和端口
Dim ipHostInfo As IPHostEntry = Dns.Resolve("cap35")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim ipe As New IPEndPoint(ipAddress, 11000)
sender1.Connect(ipe) '建立连接 Dim bytesSent As Integer = sender1.Send(msg)
sender1.Shutdown(SocketShutdown.Both)
sender1.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SendSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim ipendpoint As New IPEndPoint(Net.IPAddress.Parse("192.168.0.8"), 8888) '指定IP和PORT
Dim fs As New IO.FileStream("c:\p.doc", FileMode.OpenOrCreate, IO.FileAccess.Read)
'将来传输的文件进行流处理
Dim fssize(fs.Length - 1) As Byte
'设置要传输的文件大小
Dim strread As New IO.BinaryReader(fs) '流处理要传输的文件
strread.Read(fssize, 0, fssize.Length - 1)
'将流处理过的要发送的文件读入到fssize中
SendSocket.Connect(ipendpoint)
SendSocket.Send(fssize)
Label2.Text = fs.Length '显示文件大小
fs.Close()
SendSocket.Shutdown(SocketShutdown.Send)
SendSocket.Close()
End Sub
http://dotnet.aspx.cc/ShowDetail.aspx?id=6381BD5F-51F3-4339-4239-1328564A1B2A