我可以在服务器上接收来自客户端的消息,但我无法检索从服务器到客户端的回复。知道为什么吗?这是客户端和服务器的代码。
客户代码
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();
}
}
}
服务器代码
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);
}
}
}