16,555
社区成员
发帖
与我相关
我的任务
分享
Public MyHostIP As System.Net.IPAddress
Public MyServer As System.Net.IPEndPoint
Public MySocket As System.Net.Sockets.Socket
Public MyChattingState As Boolean
Public MyAcceptSocket As System.Net.Sockets.Socket
Dim MyThread As System.Threading.Thread
Dim thread As System.Threading.Thread
Public allDone As Threading.ManualResetEvent = New Threading.ManualResetEvent(False)
'创建聊天线程
Public Sub MyChatProcess()
Dim MyReceivedByte(7), MySendByte(4) As Byte
Dim MyReceivedString, MySendString As String
Dim socket As System.Net.Sockets.Socket = MyAcceptSocket
If (socket.Connected) Then
txt()
While (MyChattingState)
socket.Receive(MyReceivedByte, MyReceivedByte.Length, _
System.Net.Sockets.SocketFlags.None) '此处发生错误
MyReceivedString = _
System.Text.Encoding.BigEndianUnicode.GetString(MyReceivedByte)
list(MyReceivedString)
MySendString = "已收到"
MySendByte = System.Text.Encoding.BigEndianUnicode.GetBytes( _
MySendString.ToCharArray())
socket.Send(MySendByte, MySendByte.Length, _
System.Net.Sockets.SocketFlags.None)
MyReceivedString = Nothing
MySendString = Nothing
Array.Clear(MyReceivedByte, 0, MyReceivedByte.Length)
Array.Clear(MySendByte, 0, MySendByte.Length)
End While
End If
socket.Close()
End Sub
'创建聊天室
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Dim MyDelegate As New System.Threading.ThreadStart( _
AddressOf main)
thread = New System.Threading.Thread(MyDelegate)
thread.Start()
End Sub
Public Sub main()
Try
MyHostIP = System.Net.IPAddress.Parse(Me.textBox1.Text)
MyServer = New System.Net.IPEndPoint(MyHostIP, Int32.Parse("8888"))
MySocket = New System.Net.Sockets.Socket( _
System.Net.Sockets.AddressFamily.InterNetwork, _
System.Net.Sockets.SocketType.Stream, _
System.Net.Sockets.ProtocolType.Tcp)
MySocket.Bind(MyServer)
MySocket.Listen(50)
While (True)
MyAcceptSocket = MySocket.Accept
If MyAcceptSocket.Connected Then
Dim MyDelegate As New System.Threading.ThreadStart( _
AddressOf MyChatProcess)
MyThread = New System.Threading.Thread(MyDelegate)
MyThread.Start()
End If
End While
Catch ex As Exception
' Me.label5.Text = ex.Message
End Try
End Sub
Dim ipHostEntry As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostEntry.AddressList(0)
Dim socket0 As Socket = Nothing
Dim socket1 As Socket = Nothing
Dim socket2 As Socket = Nothing
Dim socket3 As Socket = Nothing
Dim socket4 As Socket = Nothing
Dim socket5 As Socket = Nothing
Dim listenList As New ArrayList()
listenList.Add(socket0)
listenList.Add(socket1)
listenList.Add(socket2)
Dim acceptList As New ArrayList()
acceptList.Add(socket3)
acceptList.Add(socket4)
acceptList.Add(socket5)
Dim i As Integer
For i = 0 To 2
listenList(i) = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
CType(listenList(i), Socket).Bind(New IPEndPoint(ipAddress, 11000 + i))
CType(listenList(i), Socket).Listen(10)
Next i
'Only the sockets that contain a connection request
'will remain in listenList after Select returns.
Socket.Select(listenList, Nothing, Nothing, 1000)
For i = 0 To listenList.Count - 1
acceptList(i) = CType(listenList(i), Socket).Accept()
Next i