为了简单起见,我要做的就是从我的 Android 模拟器向 Eclipse 上的 Java 控制台服务器发送一条消息,其中包含一个简单的字符串,然后我希望服务器回复另一个字符串。当然; 我将连接到主机 10.0.2.2 并使用任何开放端口(在我的情况下为 4000)。每次我单击模拟器上的连接按钮时,服务器都会收到“已连接”消息,但我找不到传递数据包或消息的方法。
如果您想阅读一些代码,这是我的(我认为是)对于一个简单任务来说太长的代码。
客户端包 com.example.clienttest;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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, sOut;
ObjectOutputStream oos;
ObjectInputStream 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);
}
public void Start(View view) throws IOException{
final String Text;
String send = " ";
Text = clientMessage.getText().toString();
m_objThreadClient = new Thread( new Runnable(){
public void run()
{
try {
clientSocket = new Socket("10.0.2.2", 4000);
oos = new ObjectOutputStream (clientSocket.getOutputStream());
oos.writeObject(Text);
oos.flush();
} catch (IOException e) {
serverMessage.setText(e.getMessage());
}
}
});
serverMessage.setText(send);
m_objThreadClient.start();
}
}
和服务器
import java.net.ServerSocket;
import java.net.Socket;
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;
ObjectInputStream ois;
ObjectOutputStream oos;
ClientConn(Socket client) {
this.client = client;
try {
ois = new ObjectInputStream(client.getInputStream());
String Recv;
Recv = ois.readLine();
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeChars("Welcome");
oos.flush();
oos.close();
} catch (IOException e) {
System.err.println(e);
return;
}
}
public void run() {
String msg, response;
try {
System.out.print("checking");
while ((msg = ois.readLine()) != null) {
System.out.print(msg);
}
} catch (IOException e) {
System.err.println(e);
}
}
public void sendMsg(String msg) throws IOException {
oos.writeChars(msg);
}
}
谢谢~