无法在 windows7 64bit 中连接套接字 [英] Trouble getting sockets to connect in windows7 64bit

查看:27
本文介绍了无法在 windows7 64bit 中连接套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿.我一直在寻找解决这个问题的方法,但没有运气.我想知道这是否是将套接字代码从 WinXP 32 位切换到 Win7 64 位时的已知问题.我有一个相当简单的套接字例程,它在 WinXP 32 位中运行良好,但是 socket.connect 调用抛出异常无法建立连接,因为目标机器主动拒绝它 127.0.0.1:48000"

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. 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"

我为该程序在 win7 防火墙中添加了一个例外,并仔细检查以确保它添加的规则允许所有端口.

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.

我用来设置这些简单套接字的代码如下:

The code I use to setup these simple sockets is as follows:

监听套接字:

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();
}

连接套接字:

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 }
}

似乎一切正常,直到 tTarget.Connect() 暂停一秒钟,然后抛出上面列出的异常.从不调用 AcceptCallback.

Seems like everything works until tTarget.Connect(), where it pauses for a second and then throws the exception listed above. AcceptCallback is never called.

谢谢.

推荐答案

根据您的评论,您对 IPV6 的收听.而不是

Based on your comment your listening on IPV6. Instead of

ipHostInfo.AddressList[0]

试试

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

这篇关于无法在 windows7 64bit 中连接套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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