有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Androidsocket连接错误:连接失败:ETIMEDOUT

我正在开发一个安卓应用程序,它将使用TCP与wifi控制器(固定IP和端口)通信,但因为我还没有控制器。因此,我创建了一个简单的C#服务器程序,可以接收一些测试消息

但当我尝试在安卓中打开连接时,它给了我一个例外:

java.net.ConnectException: failed to connect to /10.0.2.2 (port 1234): connect failed: ETIMEDOUT (Connection timed out)

C#计划:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
    class Program
    {
        static byte[] Buffer { get; set; }
        static Socket sck;
        static void Main(string[] args)
        {
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sck.Bind(new IPEndPoint(0, 1234));
            sck.Listen(100);
            Socket accepted = sck.Accept();
            Buffer = new byte[accepted.SendBufferSize];
            int bytesRead = accepted.Receive(Buffer);
            byte[] formatted = new byte[bytesRead];
            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = Buffer[i];
            }
            string strData = Encoding.ASCII.GetString(formatted);
            Console.Write(strData + "\r\n");
            Console.Read();
            sck.Close();
            accepted.Close();

        }
    }
}

Android代码:

try 
{

    if (socket == null) {
        int SERVERPORT = 1234;
        InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
        socket = new Socket(serverAddr, SERVERPORT); // FAILS
    }
}
} catch (IOException e) {
    e.printStackTrace();
}

我第一次用它的插座。而我在人际网络方面很笨。所以这可能是一个简单的错误

当然,我用这个错误检查了所有相关的问题,但仍然无法解决这个问题

我正在使用GenyMotion模拟器。 使用Windows 8.1 64x。 我将防火墙设置为允许C#应用程序通过windows防火墙(公共和私有)进行通信

有人知道解决办法吗?谢谢


共 (0) 个答案