正如標題所說:我可以從客戶端接收服務器上的消息,但是我無法從服務器上檢索到客戶端的回覆。 任何想法爲什麼?這是客戶端和服務器的代碼。Java Android - 可以發送消息到服務器,但不能從它接收
客戶端代碼
package com.example.clienttest;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Thread m_objThreadClient;
Socket clientSocket;
TextView serverMessage;
EditText clientMessage;
String sIn = "Nothing", sOut;
DataOutputStream oos;
DataInputStream ois;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverMessage = (TextView) findViewById(R.id.textView1);
clientMessage = (EditText) findViewById(R.id.editText1);
m_objThreadClient = new Thread(new Runnable(){
public void run()
{
try {
clientSocket = new Socket("192.168.1.55", 4000);
oos = new DataOutputStream (clientSocket.getOutputStream());
ois = new DataInputStream (clientSocket.getInputStream());
} catch (Exception e) {
serverMessage.setText(e.getMessage());
}
}
});
m_objThreadClient.start();
}
public void Start(View view) {
sOut = clientMessage.getText().toString();
try {
oos.writeUTF(sOut);
ServerConn sc = new ServerConn(clientSocket);
Thread t = new Thread(sc);
t.start();
serverMessage.setText("Reply: " + sc.msg + "\n");
} catch (Exception e) {
serverMessage.setText(e.getMessage());
}
}
public void onStop(){
try {
oos.close();
ois.close();
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ServerConn implements Runnable {
private DataInputStream ois = null;
public String msg;
public ServerConn(Socket server) throws IOException {
ois = new DataInputStream (server.getInputStream());
}
public void run() {
try {
while ((msg = ois.readUTF()) != null) {
}
} catch (IOException e) {
msg = e.toString();
}
}
}
Server代碼
import java.net.ServerSocket;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.Hashtable;
public class Server2 {
@SuppressWarnings("resource")
public static void main (String[] args) throws IOException {
ServerSocket server = null;
try {
server = new ServerSocket(4000);
} catch (IOException e) {
System.err.println("Could not start up on: " + "4000" + "Maby server is already open? Or a portforwording messup?");
System.err.println(e);
System.exit(1);
}
Socket client = null;
while(true) {
try {
client = server.accept();
System.out.print("Connected ");
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
}
Thread t = new Thread(new ClientConn(client));
t.start();
}
}
}
class ClientConn implements Runnable {
private Socket client;
String Recv, Send;
DataInputStream inFromClient;
DataOutputStream outToClient;
ClientConn(Socket client) {
this.client = client;
try {
inFromClient = new DataInputStream(client.getInputStream());
outToClient = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while ((Recv = inFromClient.readUTF()) != null) {
System.out.print("Msg: " + Recv + " \n");
if(Recv.equals("Hi")){
Send = "Wa alaikum";
}
else{
Send = "Wat?";
}
outToClient.writeUTF(Send);
outToClient.flush();
System.out.print("Replying:" + Send + "\n");
}
} catch (IOException e) {
System.out.print("No input ");
System.err.println(e);
}
}
}
客戶端的Start()方法是如何調用的? – NormR
當我按下按鈕 –