为了简单起见,我所要做的就是从我的Android模拟器向Eclipse上的Java控制台服务器发送一条包含简单字符串的消息,然后我希望服务器用另一个字符串进行回复。 当然;我连接到主机10.0.2.2并使用任何开放端口(在我的情况下为4000)。每次我点击模拟器上的连接按钮时,服务器都会获得一个“连接”的消息,但我找不到传送数据包或消息的方法。
如果你想阅读一些代码,这里是我的(我认为)太长的代码,用于简单的任务。
客户端 package 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);
}
}
感谢〜
答案 0 :(得分:0)
可能会或可能不会解决问题的一些想法:
不推荐使用ObjectInputStream的readLine()方法。请尝试使用DataInputStream。类似的东西:
try {
outToClient = new DataOutputStream(socket.getOutputStream());
inFromClient = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后:
inFromClient.readLine();
请注意,读取行将继续读取,直到遇到换行符'\ n'为止。因此readLine()将阻塞,直到它读取换行符。
尝试这些事情,看看他们是否有帮助
编辑: 似乎不推荐使用DataInputStream的readLine(),但是还有其他几种方法应该可以使用。例如readUTF()