1

我的服务器代码

 public class RemoteServer {

public static void main(String[] args) {


    ServerSocket serverSocket = null;
    Socket socket = null;
    DataInputStream dataInputStream = null;
    DataOutputStream dataOutputStream = null;

    try {
        serverSocket = new ServerSocket(8888);
        System.out.println("Listening :8888");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (true) {
        try {
            socket = serverSocket.accept();
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            System.out.println("ip: " + socket.getInetAddress());
            System.out.println("message: " + dataInputStream.readUTF().toUpperCase());
            dataOutputStream.writeUTF("sajol");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
     }

我的安卓设备客户端代码

public class Client extends Activity {

EditText textOut;
TextView textIn; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Button b = findViewById(R.id.b)

    textOut = (EditText) findViewById(R.id.t);
    Button buttonSend = (Button) findViewById(R.id.b);
    textIn = (TextView) findViewById(R.id.te);
    buttonSend.setOnClickListener(buttonSendOnClickListener);
}

Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener() {

    @Override 
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    Thread t = new Thread(r);
    t.start();
    }
};
Runnable r = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;

        try {
            socket = new Socket("192.168.1.239", 8888);
            dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream.writeUTF(textOut.getText().toString());
            textIn.setText(dataInputStream.readUTF());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("Sajol", e+"");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("Sajol", e+"");
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("Sajol", e+"");
                }
            }

            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("Sajol", e+"");
                }
            }

            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("Sajol", e+"");
                }
            }
        }







    }
};
  }

当我在模拟器服务器响应上运行此客户端代码时。但是当我在真实设备服务器上运行客户端代码时,服务器程序没有响应。我通过在 cmd 上执行 ipconfig 命令找到了在我的代码中使用的 IP 地址。我的电脑(Windows 7)和安卓设备都连接到 wifi。

我有以下异常

java.net.SocketTimeoutException:连接超时

请给我解决方案

4

0 回答 0