socket = new Socket(serverAddr,PORT);

时间:2017-02-22 14:33:11

标签: java android unity3d

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

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

我的问题是,android打开什么类型的套接字?

1 个答案:

答案 0 :(得分:-1)

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

服务器代码

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