简体   繁体   English

无法在Windows 7 64bit中连接套接字

[英]Trouble getting sockets to connect in windows7 64bit

Hey. 嘿。 I've been searching around for a solution to this problem with no luck. 我一直在寻找解决这个问题的方法,但是没有运气。 I was wondering if this is a known issue when switching socket code from WinXP 32 bit to Win7 64 bit. 我想知道将套接字代码从WinXP 32位切换到Win7 64位时是否是已知问题。 I have a fairly simple socket routine which works fine in WinXP 32bit, but the socket.connect call is throwing the exception "No connection could be made because the target machine actively refused it 127.0.0.1:48000" 我有一个相当简单的套接字例程,该例程在WinXP 32位环境下可以正常工作,但是socket.connect调用引发了异常“无法建立连接,因为目标计算机主动拒绝了127.0.0.1:48000”

I've added an exception to the win7 firewall for the program, and doubled checked to make sure the rule it added was allowing all ports. 我为程序的win7防火墙添加了一个例外,并仔细检查以确保它添加的规则允许所有端口。

The code I use to setup these simple sockets is as follows: 我用来设置这些简单套接字的代码如下:

Listening Socket: 监听套接字:

byte[] bytes = new Byte[8192];
IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 48000);

_ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try
{
    _ListenerSocket.Bind(localEndPoint);
    _ListenerSocket.Listen(1000);

    while (_Running)
    {
        _ListenerSync.Reset();
        _ListenerSocket.BeginAccept(new AsyncCallback(AcceptCallback), _ListenerSocket);
        _ListenerSync.WaitOne();
    }

    _ListenerSocket.Shutdown(SocketShutdown.Both);
    _ListenerSocket.Close();
}

Connecting Socket: 连接插座:

IPAddress _IP;
IPAddress.TryParse("127.0.0.1", out _IP)
Socket tTarget = null;

if (tTarget == null)
{
    tTarget = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

tTarget.Connect(_IP, 48000);
_Connected = true;
byte[] tBuffer = new byte[8192];
string tRecvBuff = "";

while (_Connected)
{
    int tRecv = tTarget.Receive(tBuffer);
    //{ does stuff here }
}

Seems like everything works until tTarget.Connect(), where it pauses for a second and then throws the exception listed above. 似乎一切正常,直到tTarget.Connect()暂停一秒钟,然后引发上面列出的异常。 AcceptCallback is never called. 永远不会调用AcceptCallback。

Thanks. 谢谢。

Based on your comment your listening on IPV6. 根据您的评论,您将收听IPV6。 Instead of 代替

ipHostInfo.AddressList[0]

try 尝试

ipHostInfo.AddressList.ToList().Find(p=>p.AddressFamily==AddressFamily.InterNetw‌​ork);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM