使用套接字连接时出现问题 [英] Problem to connect using Socket

查看:109
本文介绍了使用套接字连接时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用套接字编程连接到远程IP.
本程序成功连接到其中任何IP都有真实IP(例如,诸如72.55.168.241之类的IP)的计算机.

但是,如果有一台以上的计算机(具有192.168.1.3、192.168.1.2等LAN IP的计算机)连接到路由器(具有IP的路由器如59.99.50.126的计算机),
套接字无法从IP 72.55.168.241连接IP 192.168.1.3(路由器下的LAN IP).

错误来自ConnectionRemoteEndPoint()函数:

错误:不允许发送或接收数据的请求,因为未连接套接字,并且(当使用sendto调用在数据报套接字上发送时)并且未提供地址.
客户端代码:(IP> 72.55.168.241)

I want to connect to a remote IP using Socket Programming.
The Programme successfully connects to a machine where there is a Real IP (e.g IP like 72.55.168.241 ) from any IP.

However, when there are more than one computer(computer having LAN IP like 192.168.1.3, 192.168.1.2 etc.) connected to a router (router having IP Like 59.99.50.126),
the socket can''t connect the IP 192.168.1.3(LAN IP Under the router ) from the IP 72.55.168.241.

Error come from ConnectionRemoteEndPoint() function:

Error: A request to send or receive data was disallowed because the socket is not connected and ( when sending on datagram socket using sendto call)and no address was supplied.
Client Code : (IP > 72.55.168.241 )

private void ConnectRemoteEndPoint()
        {
            TcpClient tcp_Server=new TcpClient();
            IPAddress ipAddr = IPAddress.Parse(192.168.1.3)
            try
            {
                tcpServer.Connect(ipAddr, 2000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


服务器代码:(IP> 192.168.1.3)


Server Code :(IP > 192.168.1.3)

private void AcceptRemoteEndPoint()
{
      TcpListener listener_tcp;
      TcpClient tcp_Remote;
      listener_tcp = new TcpListener(IPAddress.Parse(192.168.1.3), 2000);
      listener_tcp.Start();
            while (true)
            {
                tcp_Remote= listener_tcp.AcceptTcpClient();
            }
)



我是网络编程的新手..:confused:
如何解决问题??

谢谢.



I am new in network programming..:confused:
How to solve the problem???

Thanks.

推荐答案

您将无法连接到外部的192.168.*地址.实际上,您实际上需要在路由器上启用端口转发,并将端口映射到所需的IP,这也意味着它可能不会是动态的(如果没有先行连接,您将无法连接到任何旧的192.168地址.更新路由器中的映射).

192.168地址不可在专用网络外部路由.请参见这篇文章 [
You won''t be able to connect to a 192.168.* address from the outside world. You''ll actually need to enable port forwarding on the router and map the port to the desired IP, meaning also that it likely won''t be dynamic (you won''t be able to connect to any old 192.168 address without first updating the mapping in the router).

192.168 addresses are not routable outside of the private network. See this article[^].

Cheers.


将您的地址放在一边,您在哪里TcpListener.AcceptSocketTcpListener.AcceptTcpClient?现在,这就是TcpListener的工作方式:).同样,通常,您将需要接受一个线程的套接字,并通过在另一个线程上对网络流进行读/写操作来与客户端进行交换.

顺便说一句,当我查看应该作为客户端的部分时:

Set aside you address, where is you TcpListener.AcceptSocket or TcpListener.AcceptTcpClient? This is now how TcpListener works :). Also, normally, you would need accepting sockets one thread and exchange with client(s) via reading/writing from/to network stream on the other thread.

By the way, when I look at what''s supposed to be a client part:

TcpClient tcp_Server = new TcpClient();



尤其是在您的命名(一个名为tcp_Server(!)的客户端)上,有些东西告诉我,您很困惑:).

现在,请告诉我为什么这个问题与另一个问题几乎相同:
使用套接字连接远程计算机的问题 [ ^ ] ?!
你不应该那样做.

—SA



in particular, at your naming (a client called tcp_Server(!)), something tells me that you''re in deep confusion :).

Now please tell me why this Question is nearly identical to the other one:
Problem to connect remote computer using socket[^]?!
You should not do that.

—SA


这篇关于使用套接字连接时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆