Socket不能使用问题
'发送端代码
Dim skt1 As Socket
skt1 = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim ServerAdd As IPAddress = IPAddress.Parse("172.16.8.221") '172.16.10.42
Dim iEndPoint As New IPEndPoint(ServerAdd, 80)
skt1.Connect(iEndPoint) 'connect to remote server
Dim arr() As Byte = System.Text.Encoding.ASCII.GetBytes("Test From Skt233" )
skt1.Send(arr, arr.Length, SocketFlags.None)
MessageBox.Show("Send Over")
skt1.Shutdown(SocketShutdown.Send)
skt1.Close()
'接收端
Dim skt2 As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim hostadd As IPAddress = IPAddress.Parse("172.16.8.221")
Dim EPhost As New IPEndPoint(hostadd, 80)
'connect to server
skt2.Connect(EPhost) '只能使用又句,如果使用bind和listen,就出现下面的错误
'skt2.Bind(EPhost)
'Error: Only one usage of each socket address (protocol/network address/port)is normally permitted
'skt2.Listen(2)
If Not skt2.Connected Then
MessageBox.Show("Connect server failure!")
Exit Sub
Else
MessageBox.Show("Conneck OK")
End If
Dim arrByte(256) As Byte
Dim str As String = ""
Dim bytes As Int32 = skt2.Receive(arrByte, arrByte.Length, 0) '这句走不动了!!
str = str & System.Text.Encoding.ASCII.GetString(arrByte, 0, bytes)
While bytes > 0
bytes = skt2.Receive(arrByte, arrByte.Length, 0)
str = str & System.Text.Encoding.ASCII.GetString(arrByte, 0, bytes)
End While
MessageBox.Show("Accept:" & str)
skt2.Shutdown(SocketShutdown.Receive)
skt2.Close()
在发送端没有错误,
但是在接收端我不能使用skt2.Bind(EPhost) 和 skt2.Listen(2)
一旦使用就出现错误:Error: Only one usage of each socket address (protocol/network address/port)is normally permitted
这是怎么回事?
在接收端如果我使用这句skt2.Connect(EPhost) 那么下面的这句skt2.Receive(arrByte, arrByte.Length, 0)就走不动了,不知为什么?
请各位各手帮帮忙,感激不尽。