Socket不能使用问题

Oldman 2005-03-01 10:18:23
'发送端代码
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)就走不动了,不知为什么?

请各位各手帮帮忙,感激不尽。







...全文
517 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
TonyTonyQ 2005-03-02
  • 打赏
  • 举报
回复
Error: Only one usage of each socket address (protocol/network address/port)is normally permitted
是因为你监听了已被占用的端口,80端口是HTTP默认端口,所以会出错
TonyTonyQ 2005-03-02
  • 打赏
  • 举报
回复
还有就是,你要先启动接受端,再启动发送端。否则就会出现 No connection could be made because the target machine actively refused it 这样的错误。

另外,上面只是修改了你的代码,你写的代码只能接受一个发送端,如果你有多个发送端,需要修改代码和使用多线程操作。
TonyTonyQ 2005-03-02
  • 打赏
  • 举报
回复
接收端不能使用connect方法后再使用bind和listen方法。

MSDN中有例子,很详细。这里说一下大致的做法:

.........
Dim EPhost As New IPEndPoint(hostadd, 80)
'connect to server
'skt2.Connect(EPhost) '不能使用这句!!!

skt2.Bind(EPhost) '使用这句邦定本地端口

skt2.Listen(2) '开始侦听端口
If Not skt2.Connected Then
MessageBox.Show("Connect server failure!")
Exit Sub
Else
MessageBox.Show("Conneck OK")
End If
..........................
Oldman 2005-03-02
  • 打赏
  • 举报
回复
up
Oldman 2005-03-02
  • 打赏
  • 举报
回复
多谢,问题解决。
lonelydreamsym 2005-03-01
  • 打赏
  • 举报
回复
up
Oldman 2005-03-01
  • 打赏
  • 举报
回复
我看看别的端口
Korny 2005-03-01
  • 打赏
  • 举报
回复
你先看看是否有其他程序如iis,占用了80端口,建议你换端口试
有问题与我联系
nga96 2005-03-01
  • 打赏
  • 举报
回复
UP
amendajing 2005-03-01
  • 打赏
  • 举报
回复
mark
Oldman 2005-03-01
  • 打赏
  • 举报
回复
我现在把端口换成了8080
但是在发送端做连接时skt1.Connect(iEndPoint)
出现错误:No connection could be made because the target machine actively refused it

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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