麻烦套接字在Windows7 64位连接 [英] Trouble getting sockets to connect in windows7 64bit

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

问题描述

嘿。我一直在寻找周围的解决这个问题没有运气。我想知道,这是一个已知的问题交换插槽code从WinXP的32位的Win7 64位时。我有一个相当简单的socket例程,后者在WinXP中的32位工作正常,但socket.connect调用抛出异常无连接可以作出,因为目标机器积极地拒绝它127.0.0.1:48000

我添加了一个例外WIN7防火墙程序,并成倍检查,以确保其加入允许所有端口的规则。

在code我用它来设置这些简单的插座如下:

监听套接字:

 字节[]字节=新字节[8192];
IPHostEntry ipHostInfo = Dns.GetHostEntry(localhost的);
的IPAddress ip地址= ipHostInfo.AddressList [0];
IPEndPoint localEndPoint =新IPEndPoint(ip地址,48000);

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

尝试
{
    _ListenerSocket.Bind(localEndPoint);
    _ListenerSocket.Listen(1000);

    而(_Running)
    {
        _ListenerSync.Reset();
        _ListenerSocket.BeginAccept(新的AsyncCallback(AcceptCallback),_ListenerSocket);
        _ListenerSync.WaitOne();
    }

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

连接插座:

  ip地址_IP;
IPAddress.TryParse(127.0.0.1,出_IP)
插座tTarget = NULL;

如果(tTarget == NULL)
{
    tTarget =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
}

tTarget.Connect(_IP,48000);
_Connected = TRUE;
byte []的t缓冲=新的字节[8192];
字符串tRecvBuff =;

而(_Connected)
{
    INT tRecv = tTarget.Receive(t缓冲);
    // {做的东西在这里}
}
 

好像一切正​​常,直到tTarget.Connect(),它暂停第二,然后抛出上面列出的例外。 AcceptCallback永远不会被调用。

感谢。

解决方案

根据您的评论你的听力在IPV6。而不是

  ipHostInfo.AddressList [0]
 

尝试

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

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"

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:

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. AcceptCallback is never called.

Thanks.

解决方案

Based on your comment your listening on IPV6. Instead of

ipHostInfo.AddressList[0]

try

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

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

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