在互联网上使用套接字 [英] Using sockets over internet
问题描述
我正在创建一个允许消息发送到多个客户端的应用程序。它适用于在本地计算机上打开的服务器和客户端,但我需要它与一台机器上打开的一台服务器和许多机器上的许多客户机一起使用。
我正在将它用于客户端:
Hi,
I'm creating an app that allows messages to be to several clients. It works with the server and client open on a local machine, but I need it to work with one server open on one machine and many clients on many machines.
I'm using this for the client:
Const READ_BUFFER_SIZE As Integer = 255
Const PORT_NUM As 整数 = 49230
Const PORT_NUM As Integer = 49230
私人 客户 As TcpClient
Private client As TcpClient
私人 readBuffer(READ_BUFFER_SIZE) As Byte
Private readBuffer(READ_BUFFER_SIZE) As Byte
私人 Sub frmMain_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) 句柄 MyBase .Load
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim frmConnectUser As 新 frmConnectUser()
Dim frmConnectUser As New frmConnectUser()
Dim ipa As 字符串 = InputBox( "请输入服务器机器的IP" ,, " 89.145.1x9.1xx" )
Dim ipa As String = InputBox("Please enter the IP of the server machine", , "89.145.1x9.1xx")
尝试
Try
Me 。显示()
Me.Show()
AttemptLogin()
AttemptLogin()
Catch Ex As Exception
Catch Ex As Exception
MsgBox( "服务器未激活。请启动服务器并重试。" ,_
MsgBox("Server is not active. Please start server and try again.", _
MsgBoxStyle.Exclamation, Me .Text)
MsgBoxStyle.Exclamation, Me.Text)
Me .Dispose()
Me.Dispose()
End 尝试
End Try
End Sub
对于我正在使用的服务器:
End Sub
For the server I'm using:
Const PORT_NUM As 我nteger = 49230
Const PORT_NUM As Integer = 49230
私人 客户 如 新 Hashtable( )
Private clients As New Hashtable()
私人 < font size = 2> listener As TcpListener
Private listener As TcpListener
私人 listenerThread As 线程。线程
Private listenerThread As Threading.Thread
私人 Sub frmMain_Load( ByVal 发件人 As System.Object, ByVal e As System.EventArgs) 句柄 MyBase .Load
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listenerThread = 新 Threading.Thread( AddressOf DoListen)
listenerThread = New Threading.Thread(AddressOf DoListen)
listenerThread.Start()
listenerThread.Start()
UpdateStatus( " Listener started" )
UpdateStatus("Listener started")
End Sub
End Sub
我在两台计算机的路由器上设置服务以允许该端口号和通过在线检查来检查服务器计算机的IP。我还允许在Windows防火墙中使用端口号。
当我尝试连接客户端时,它显示服务器未打开的错误消息。
任何人都可以帮我找到哪里我出错了还是更好的方法呢?
这让我很生气我只能在同一台机器上运行。
谢谢
_
I've set services on two computer's routers to allow that port number and checked the IP of the Server machine by checking it online. I've also allowed the port number in Windows Firewall.
When I try to connnect a client it show the error message for the server not being open.
Can anyone please help me find where I've gone wrong or a better way to do it?
It's driving me mad cos I can only be at one machine at once.
Thanks
推荐答案
我很想知道你的AttemptLogin和DoRead中有什么子程序。
提示:混合异步和同步套接字方法不是一个好主意。
-Steve
I'd be curious to know what's in your AttemptLogin and DoRead subroutines.
Hint: mixing async and sync socket methods are not a good idea.
-Steve
这篇关于在互联网上使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!