使用套接字

时间:2016-04-01 18:14:58

标签: android

我正在制作一个使用套接字通过TCP / IP发送数据的项目,但在共享数据方面存在问题。我使用了Android设备和安装了OS Linux mint的PC。当我向设备提供IP和端口号时,两个设备都连接在同一网络上。它无法在移动设备和设备之间建立连接。

它给出的错误是:

connect failed: ECONNREFUSED (Connection refused)

这是主要活动代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ip = getLocalIpAddress();
    ip2 = getMyLocalIpAddress();
    ipadd = (TextView) findViewById(R.id.ipaddress);
    ipadd.setText(ip);
    ipadddddd = (TextView) findViewById(R.id.ipaddddd);
    ipadddddd.setText(ip2);
    connect = (Button) findViewById(R.id.connect);
    connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ClientSocket clientSocket = new ClientSocket("192.168.0.1", 8080);
            clientSocket.start();
        }
    });

}

public class ClientSocket extends Thread {
    String dstAddress;
    int dstPort;
    Boolean flag;

    ClientSocket(String address, int port) {
        dstAddress = address;
        dstPort = port;
        flag = false;
    }

    @Override
    public void run() {
        Socket socket = null;
        try {
            try {
                socket = new Socket(dstAddress, dstPort);
            } catch (IOException e) {
                Log.e("sockettttttttttttttttttttt ", "not connected " + e.toString());
            }
            Log.e("Socket Object", "Created");
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeUTF("Hello ishan.....1111111111111111111111111111111");
            outputStream.flush();
            outputStream.close();
            if (socket.isConnected()) {
                flag = true;
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
                Log.e("socket is connected ", flag + "");
            } else if (!socket.isConnected()) {
                flag = false;
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT).show();
                Log.e("socket is connected ", flag + "");
            }
        } catch (Exception e) {
            Log.e("exception", e.toString());
        }
        super.run();
    }
}

private String getLocalIpAddress() {
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    return ip;
}

public String getMyLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

我的活动的XML代码

<TextView
    android:id="@+id/ipaddress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Ip Adress" />

<TextView
    android:id="@+id/ipaddddd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/connect"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="Ip adressss" />

<Button
    android:id="@+id/connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ipaddress"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="connect" />

0 个答案:

没有答案