环境配置完毕并了解了基本原理后,直接上代码,下面是客户端的代码,用java实现:
src/BogoclientActivity.java
- package bogo.client.com;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class BogoclientActivity extends Activity
- {
- /* 服务器地址 */
- private final String SERVER_HOST_IP = "192.168.1.2";
- /* 服务器端口 */
- private final int SERVER_HOST_PORT = 9400;
- private Button btnConnect;
- private Button btnSend;
- private EditText editSend;
- private Socket socket;
- private PrintStream output;
- public void toastText(String message)
- {
- Toast.makeText(this, message, Toast.LENGTH_LONG).show();
- }
- public void handleException(Exception e, String prefix)
- {
- e.printStackTrace();
- toastText(prefix + e.toString());
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- btnConnect.setOnClickListener(new Button.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- initClientSocket();
- }
- });
- btnSend.setOnClickListener(new Button.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- sendMessage(editSend.getText().toString());
- }
- });
- }
- public void initView()
- {
- btnConnect = (Button)findViewById(R.id.btnConnect);
- btnSend = (Button)findViewById(R.id.btnSend);
- editSend = (EditText)findViewById(R.id.sendMsg);
- btnSend.setEnabled(false);
- editSend.setEnabled(false);
- }
- public void closeSocket()
- {
- try
- {
- output.close();
- socket.close();
- }
- catch (IOException e)
- {
- handleException(e, "close exception: ");
- }
- }
- private void initClientSocket()
- {
- try
- {
- /* 连接服务器 */
- socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);
- /* 获取输出流 */
- output = new PrintStream(socket.getOutputStream(), true, "utf-8");
- btnConnect.setEnabled(false);
- editSend.setEnabled(true);
- btnSend.setEnabled(true);
- }
- catch (UnknownHostException e)
- {
- handleException(e, "unknown host exception: " + e.toString());
- }
- catch (IOException e)
- {
- handleException(e, "io exception: " + e.toString());
- }
- }
- private void sendMessage(String msg)
- {
- output.print(msg);
- }
- }
layout/main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/btnConnect"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/connect" />
- <EditText
- android:id="@+id/sendMsg"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="text" />
- <Button
- android:id="@+id/btnSend"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/send" />
- </LinearLayout>
不要忘了,在AndroidManifest.xml中添加访问网络权限:
<uses-permission android:name="android.permission.INTERNET" />
把上面的代码编译并下载到模拟器中
