以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]请教socket问题 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=105904) |
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 8:00:00 -- [求助]请教socket问题 问题:如何写MyServer.Start()?如OpenQQ, MyServer.ServerIp = "192.168.100.6"
\'指定IP地址 MyServer.ServerPort = 52177 \'指定端口 MyServer.HeartbeatInterval = 30 \'指定心跳时间 MyServer.HeartbeatTimeout = 100 \'指定心跳超时时间 MyServer.Start() \'启动QQServer Public server As MyServer Public Class MyServer Private server As String Private port As Integer Private Const maxPacket As Integer = 64 * 1024 Public tcpl As System.Net.Sockets.TcpListener = Nothing Public user_tb As New System.Collections.Hashtable() \'客户端列表 Public msg As String \'客户端登录信息 Public Sub New(ByVal ip As String, ByVal p As Integer) server = ip port = p Dim clientThread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf newThread)) \'clientThread.IsBackground = True clientThread.Start(Nothing) End Sub Public Sub newThread(ByVal obj As Object) Dim ip As System.Net.IPAddress = System.Net.IPAddress.Parse(server) \'转换成IP tcpl = New System.Net.Sockets.TcpListener(ip, port) Try tcpl.Start() \'开始监听 Catch ex As Exception MsgBox("端口" & port & "被占用,请改用其它端口") Return End Try \'开始侦听传入的连接请求. try msg &= "服务器已启动,正在监听..." & vbCrLf msg &= String.Format("服务器IP:{0}" & vbTab & "端口号:{1}" & vbCrLf, ip, port) While True Dim packetBuff As Byte() = New Byte(maxPacket) {} Dim newClient As System.Net.Sockets.Socket = tcpl.AcceptSocket() \'接受客户端请求 Dim rlen As Integer = newClient.Receive(packetBuff) Dim data As String = Encoding.Unicode.GetString(packetBuff, 0, rlen) \'将接受的远程SOCKET,将数据存入接受缓冲区 user_tb.Add(data, newClient) \'将客户端标识添加进hash表 Dim svrlog As String = String.Format("[系统消息]新用户 {0} 在 {1} 已连接... 当前在线人数: {2}" & vbCrLf, data, DateTime.Now, user_tb.Count) msg &= svrlog Dim clientThread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf ThreadFunc)) clientThread.Start(data) \'clientThread.IsBackground = True newClient.Send(Encoding.Unicode.GetBytes("OK")) End While catch ex As exception \'msgbox("监听关闭成功") End try End Sub Private Sub ThreadFunc(ByVal obj As Object) Dim clientSkt As System.Net.Sockets.Socket = user_tb(obj) While True Try Dim cmdBuff(128) As Byte Dim rlen As Integer = clientSkt.Receive(cmdBuff) \'静候客户端关闭 Catch ex As exception msgbox("有一个客户端被关闭了") user_tb.Remove(obj) msg &= String.Format("[系统消息]用户 {0} 的客户端在 {1} 关闭!当前在线人数:{2}" & vbCrLf, obj, DateTime.Now, user_tb.Count) System.Threading.Thread.CurrentThread.Abort() End Try End While End Sub Public Sub RemoveClient(ByVal obj As Object) Dim clientSkt As System.Net.Sockets.Socket = user_tb(obj) clientSkt.Send(New Byte() {1, 1}) \'让客户正常关闭 End Sub End Class
|
-- 作者:有点甜 -- 发布时间:2017/8/29 8:43:00 -- 参考
http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=100826&skin=0
|
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 9:23:00 -- 如何为类MyServer定义属性?如MyServer.IP [此贴子已经被作者于2017/8/29 9:27:56编辑过]
|
-- 作者:有点甜 -- 发布时间:2017/8/29 9:43:00 -- 以下是引用pcxjxjhkw在2017/8/29 9:23:00的发言:
如何为类MyServer定义属性?
Public Class MyServer
Public server As String
Public port As Integer
-------------------------------------------
Dim server As New MyServer
server.server = "123456"
server.port = "10086"
|
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 10:20:00 -- 老师,如何写MyServer.Start() \'启动 谢谢
|
-- 作者:有点甜 -- 发布时间:2017/8/29 10:21:00 -- 认认真真看懂2楼例子和代码。 |
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 15:24:00 -- 1.当客户端成功连接服务器,服务器上如何获取该客户端的IP,机器码等信息?谢谢 2.假如有多个客户端连接上服务器,服务器如何向指定客户端发送信息?
[此贴子已经被作者于2017/8/29 15:36:21编辑过]
|
-- 作者:有点甜 -- 发布时间:2017/8/29 16:22:00 -- 1、socket对象.RemoteEndPoint.Address.ToString
https://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.remoteendpoint(VS.80).aspx
2、可以socket连接后给其分配一个编号,以后根据编号发送信息;这个编号可以是 用户名、ip、或者任意不重复的编号 |
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 16:31:00 -- 谢谢 |
-- 作者:pcxjxjhkw -- 发布时间:2017/8/29 16:33:00 -- 2、可以socket连接后给其分配一个编号,以后根据编号发送信息;这个编号可以是 用户名、ip、或者任意不重复的编号 老师,这个没明白.。有多个客户端连接服务器,也就有同样多个socket连接,根据客户IP如何反回去找对应的socket?
|