1
我试图让PC客户端连接到Android服务器。这在仿真器中运行。我无法让PC客户端连接。Android服务器电脑客户端
Android的服务器..
try {
serverSocket = new ServerSocket(1234);
//tell logcat the server is online
Log.d("TCP", "C: Server Online...");
while (true) {
Log.d("TCP", "C: Waiting for client");
Socket client = serverSocket.accept();
Log.d("TCP", "C: Client has connected");
BufferedReader in = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
String input = in.readLine();
Log.d("TCP", "C: " + input);
client.close();
}
} catch (Exception e) {
Log.d("TCP", "C: " + e);
}
和PC客户端
String host = "127.0.0.1";
//Port the server is running on.
int port = 1234;
//Message to be sent to the PC
String message = "Hello from pc.";
//Get the address of the host.
InetAddress remoteAddr = InetAddress.getByName(host);
//Tell the user that we are attempting a connect.
System.out.println("Attempting connect");
//Make the connection
Socket socket = new Socket(host, port);
//Tell user the connection was established
System.out.println("Connection made");
//Make the output stream to send the data.
OutputStream output = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(output)), true);
//Send the data.
out.println(message);
//Tell user the message was sent
System.out.println("Message sent");
我不断收到消息拒绝连接在PC上..谁能帮助我?干杯
干杯,让它工作! – jlabroy
很高兴听到它。 – Squonk