【问题标题】:socket = new Socket(serverAddr, PORT);socket = new Socket(serverAddr, PORT);
【发布时间】:2017-02-22 14:33:11
【问题描述】:

当我说 socket = new Socket(serverAddr, PORT);在android中,打开的是什么类型的socket? UDP 套接字还是 TCP 套接字?

我正在尝试从我的 android 发送数据并从我的 PC 读取它。我正在从 UDP 接收器 C# 脚本(Unity3D)中读取它。但我的 android 报告连接超时异常。

我的问题是,android打开的是什么类型的socket?

【问题讨论】:

  • Socket.connect打开一个TCP套接字要使用UDP,你需要创建一个DatagramSocket
  • 谢谢,有帮助。我试图从我的电脑上实时读取手机上的加速度计数据。我打开了一个 tcp 连接以通过 WiFi 获取数据流。但是有一些滞后。有什么更好的方法来做到这一点? wifi是正确的方法,还是我应该尝试蓝牙?最终我的手机应该像无线鼠标一样传输数据。

标签: java android unity3d


【解决方案1】:

你从 tcp 使用过。使用下面的示例代码使用 udp 套接字:

服务器代码

public class udp_server
{
public static void main(String args[])
{
    DatagramSocket sock = null;

    try
    {
        //1. creating a server socket, parameter is local port number
        sock = new DatagramSocket(7777);

        //buffer to receive incoming data
        byte[] buffer = new byte[65536];
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

        //2. Wait for an incoming data
        echo("Server socket created. Waiting for incoming data...");

        //communication loop
        while(true)
        {
            sock.receive(incoming);
            byte[] data = incoming.getData();
            String s = new String(data, 0, incoming.getLength());

            //echo the details of incoming data - client ip : client port - client message
            echo(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " - " + s);

            s = "OK : " + s;
            DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
            sock.send(dp);
        }
    }

    catch(IOException e)
    {
        System.err.println("IOException " + e);
    }
}

//simple function to echo data to terminal
public static void echo(String msg)
{
    System.out.println(msg);
}
}

客户端代码

public class udp_client
{
public static void main(String args[])
{
    DatagramSocket sock = null;
    int port = 7777;
    String s;

    BufferedReader cin = new BufferedReader(new   InputStreamReader(System.in));

    try
    {
        sock = new DatagramSocket();

        InetAddress host = InetAddress.getByName("localhost");

        while(true)
        {
            //take input and send the packet
            echo("Enter message to send : ");
            s = (String)cin.readLine();
            byte[] b = s.getBytes();

            DatagramPacket  dp = new DatagramPacket(b , b.length , host , port);
            sock.send(dp);

            //now receive reply
            //buffer to receive incoming data
            byte[] buffer = new byte[65536];
            DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
            sock.receive(reply);

            byte[] data = reply.getData();
            s = new String(data, 0, reply.getLength());

            //echo the details of incoming data - client ip : client port -     client message
            echo(reply.getAddress().getHostAddress() + " : " +         reply.getPort() + " - " + s);
        }
    }

    catch(IOException e)
    {
        System.err.println("IOException " + e);
    }
}

//simple function to echo data to terminal
public static void echo(String msg)
{
    System.out.println(msg);
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多