• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

大家帮个忙 帮我讲解下Vb.net的socket怎么实现一个服务器端并行监听多个客户端~~

king8192 2006-11-02 03:24:01
我想在vb.net下用socket做一个服务器端来监听多个客户端,并可以与这多个客户端进行数据的收发,而且要求服务器端与每个客户端的连接都是独立的,我能随意的选择一个或多个客户端给他们发消息,也能收到他们发回的消息.
以前在vb6.0里,都是创建socket控件数组,并且可以根据连接号(index)来确定哪个socket对应的是哪个客户端,我现在刚接触vb.net里的socket,有点乱,搞不清怎么实现我说的功能,有哪位高手指点下,能给个例子最好不过,我在此谢谢大家了!!
...全文
895 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yuluke 2006-12-01
顶!
回复
king8192 2006-11-23
我是初学菜鸟,大家再帮忙看看~
回复
king8192 2006-11-23
继续顶,在这先谢谢大家了!
回复
king8192 2006-11-22
我想问一下,终端侦听的客户端有限制么?是不是客户端越多,开的线程就越多,程序处理速度就越慢呢?我要做的至少要连接500个客户端,进行数据的交互,会不会有问题呢。再顶下自己~~
回复
jedliu 2006-11-18
也就是用个Singleton模式!
回复
jedliu 2006-11-17
其实是一样的!当发现一个连接后,建立一个线程,然后处理,当另外一个来后,重新创建!至于线程池不用关心,那个系统自会考虑!
public sealed class Sockets
{
private Sockets()
{
//在这里可以建立连接
}

public static Sockets GetInstance(IPEndPoint point)
{
//在这里可以建立线程并连接
}
}
回复
explore_xu 2006-11-17
学习
回复
iland9876543210 2006-11-17
回复
HAVENT 2006-11-17
我觉得用.NET 的 Remoting 实现应该简单一点
回复
king8192 2006-11-17
顶下自己的帖子!
回复
king8192 2006-11-07
为什么我用异步操作后,客户端只能发一次消息给服务器端呢? 是不是我的代码有问题?


Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class Frm_AuTo

Public SvrIp As IPAddress '监听服务器IP地址
Public SvrPort As Integer '监听服务器端口号
Public Svr As IPEndPoint '服务器地址簇
Public SvrListenSocket As Socket '服务器侦听socket
Public SvrListener As Socket '表示异步操作的状态
Public SvrListenThread As Thread '服务器侦听线程
Public Done As New ManualResetEvent(True) '通知一个或多个正在等待的线程已发生事件
Public receiveDone As New ManualResetEvent(False)
Public alsock As New ArrayList '放置socket的队列
Public TNode As TreeNode

Public ConnSocket As Socket

Public Delegate Sub SetTreeViewCallback()

Public Sub SetNode()
If Tview.InvokeRequired Then
Dim d As New SetTreeViewCallback(AddressOf SetNode)
Me.Invoke(d)
Else
TNode.Nodes.Add(ConnSocket.RemoteEndPoint.ToString)
TNode.ExpandAll()
End If
End Sub



Private Sub btn_Listen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Listen.Click

'判断主机IP格式是否正确
Try
SvrIp = IPAddress.Parse(T_ip.Text)
Catch ex As Exception
MsgBox("您输入的IP地址不正确!")
Exit Sub
End Try
SvrPort = CInt(T_dk.Text)

'侦听50个客户端 并启动线程
Try
Svr = New IPEndPoint(SvrIp, Int32.Parse(SvrPort))
SvrListenSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
SvrListenSocket.Bind(Svr)
SvrListenSocket.Listen(50)
SvrListenThread = New Thread(AddressOf ListenThread)
SvrListenThread.Start()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

End Sub

'侦听线程
Public Sub ListenThread()

Try
While (True)
Done.Reset()
'开始一个异步操作来接受一个传入的连接尝试
SvrListenSocket.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), SvrListenSocket)
Done.WaitOne()
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

End Sub

'异步线程的回调函数
Public Sub AcceptCallback(ByVal ar As IAsyncResult)

Done.Set()
SvrListener = ar.AsyncState
ConnSocket = SvrListener.EndAccept(ar)

Try
Dim state As New StateObject
state.workSocket = ConnSocket
ConnSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
'向队列添加连接到的Socket
alsock.Add(ConnSocket)
SetNode()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

End Sub

Public Sub ReadCallback(ByVal ar As IAsyncResult)

Try
Dim ResponseStr As String
Dim state As StateObject
state = ar.AsyncState
Dim ClientSocket As Socket
Dim ReadBytes As Integer
Done.Set()
ClientSocket = state.workSocket
ReadBytes = ClientSocket.EndReceive(ar)
'ReadBytes = ClientSocket.Receive(state.buffer, StateObject.BufferSize, SocketFlags.None, SocketFlags.None)

If ReadBytes > 0 Then
state.sb.Append(System.Text.Encoding.BigEndianUnicode.GetString(state.buffer, 0, ReadBytes))
'ClientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
'SetNode()
ResponseStr = state.sb.ToString
Rb.Text = ResponseStr
Else
ClientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
'If state.sb.Length > 1 Then
' ResponseStr = state.sb.ToString
'End If
'receiveDone.Set()
'Rb.Text = ResponseStr
End If


Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

End Sub


Private Sub Frm_AuTo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Control.CheckForIllegalCrossThreadCalls = False
Tview.Nodes.Clear()
TNode = Tview.Nodes.Add("Head", "当前在线终端")
Tview.ExpandAll()

End Sub
End Class

Public Class StateObject
Public workSocket As Socket
Public Const BufferSize As Integer = 1024
Public buffer(BufferSize) As Byte
Public sb As New StringBuilder
End Class
回复
Qim 2006-11-03
楼上正解
回复
yuqi508 2006-11-03
ServerSocket.vb
负责监听是否有客户端连接,开线程接受客户端Socket

回复
yuqi508 2006-11-03
ListenClient.vb

Imports System.Threading
Imports System.Net
Imports System.Net.Sockets

Public Class ListenClient
Private serverSocket As System.Net.Sockets.Socket
Private clientSocket As System.Net.Sockets.Socket

' 篶ㄧΑ
Public Sub New(ByVal serverSocket As System.Net.Sockets.Socket)
Me.serverSocket = serverSocket
End Sub

Public Sub ServerThreadProc()
Do While True
Try
Dim clientSocket As Socket = serverSocket.Accept()

Dim clientInfo As IPEndPoint = CType( _
clientSocket.RemoteEndPoint, _
IPEndPoint)
Dim serverInfo As IPEndPoint = CType( _
serverSocket.LocalEndPoint, _
IPEndPoint)

Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
Console.WriteLine("Server: " + serverInfo.Address.ToString() + ":" + serverInfo.Port.ToString())
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
End Try
Loop
End Sub
End Class
回复
yuqi508 2006-11-03
ServerSocket.vb

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class ServerSocket
Public Shared Sub Main()
Try
Dim serverSocket As New Socket( _
AddressFamily.InterNetwork, _
SocketType.Stream, _
ProtocolType.Tcp)
Dim serverIP As IPAddress = IPAddress.Parse("127.0.0.1")
'Dim serverIP As IPAddress = Dns.Resolve("localhost").AddressList(0)

' Port = 80
Dim Port As String = "80"
Dim serverhost As New IPEndPoint(serverIP, Int32.Parse(Port))

serverSocket.Bind(serverhost)

' Backlog = 50
serverSocket.Listen(50)

Console.WriteLine("Server started at: " + serverIP.ToString() + ":" + Port)

Dim lc As New ListenClient(serverSocket)
Dim serverthread As New Thread( _
New ThreadStart(AddressOf lc.ServerThreadProc))
serverthread.Start()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
End Try
End Sub
End Class
回复
Qim 2006-11-03
思路:
1:服务器端有个Listener线程,用来监听。
2:服务器端再来个server socket线程,用来和客户端连接。
3:当监听到客户连接时,创建server socket线程,独立和客户端连接。
4:服务器端要有一个结构来保存客户端信息。可以自己写一个类来封装。如用户ip等。
回复
chaircat 2006-11-02
有固定模式...不过网上的多是C#写的,看的懂就去看看...
基本上是多线程:一个线程监听,Accept之后创建一个线程处理请求...
回复
gao256 2006-11-02
学习ing
回复
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2006-11-02 03:24
社区公告
暂无公告