java——创建一个简单的Hello world Android客户端服务器程序
发布时间:2022-04-25 00:39:53 458
相关标签: # 移动端
为了简单起见,我所要做的就是从我的Android模拟器向Eclipse上的Java控制台服务器发送一条包含一个简单字符串的消息,然后我希望服务器用另一个字符串回复。当然;我将连接到主机10.0.2.2并使用任何打开的端口(在我的情况下是4000)。每次我点击模拟器上的连接按钮时,服务器都会收到一条“connected”消息,但我找不到传递数据包或消息的方法。
如果你想阅读一些代码,下面是我的(我认为是)对于一个简单任务来说太长的代码。
客户端包com。实例客户测试;
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);
}
}
谢谢~
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报