套接字问题(连接到域名或服务器名称) [英] Problem with Socket (connect to domain name or server name)

查看:191
本文介绍了套接字问题(连接到域名或服务器名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序中使用Socket对象.该代码可以在下面简单看到.

Hi, I am trying to use Socket object in my application. The code can simply seen below.

//Using TcpClient Class
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("http://myserver.com", 123);
// we can also say 
tcpClient.Connect(IPAddress.Parse("125.54.10.25"), 123);

//Using Socket Class
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect("http://myserver.com", 123);
//or

client.Connect(IPAddress.Parse("125.54.10.25"), 123);



使用Socket时,我无法使用这样的配置通过Internet连接到服务器



When using Socket I can''t connect to the server over the Internet with such configuration

(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

那么,AddressFamily,SocketType和ProtocolType的正确方法是什么?

So, what is the right way of AddressFamily, SocketType and ProtocolType?

推荐答案

您可以尝试提供有效的IP地址:
You could try giving a valid IP address:
125.54.10.0.0

无效-IPV4的数字块过多(其中有四个块,即"125.54.10.0"),或者IPV6的冒号需要冒号(具有六个数字块"125:54:10:0:0:0")

is not valid - it has too many number blocks for IPV4 (which has four blocks, "125.54.10.0") or it needs colons for IPV6 (which has six number blocks "125:54:10: 0: 0: 0")


client.Connect("http://myserver.com", 123);



尝试将其更改为:



try changing this to:

client.Connect("myserver.com", 123);



我认为这不会证明有任何用处,只是当我们明确提及端口时,无需在此处指定http.



I don''t think that this would prove of any use, its just that there is no need to specify http there when we are explicitly mentioning the port.


这篇关于套接字问题(连接到域名或服务器名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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