天天看点

Android简单实现Socket通信,client连接server后,server向client发送文字数据

案例实现的是简单的Socket通信,当client(Androidclient)连接到指定server以后,server向client发送一句话文字信息(你能够拓展其他的了)

先看一下服务端程序的实现吧

Server.java

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

// 创建一个ServerSocket,用于监听clientsocket的连接请求
ServerSocket ss = new ServerSocket(30000);
// 採用循环不断接受来自client的请求,server端也相应产生一个Socket
while (true) {
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
os.write("你好,由于你上线了,所以server发给了你这条信息".getBytes("utf-8"));
os.close();
s.close();
}

}
}
      

接下来实现的就是手机client的上线并接收数据了,看一下 MainActivity.java

package com.example.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView display;
private Handler handler;
private String host;
private Button btn;
private Socket socket;
private String line;
private EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
btn = (Button) findViewById(R.id.send);
et = (EditText)findViewById(R.id.editText1);
et.setText("192.168.1.100");
OnClickListener listener = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread() {
public void run() {
try {
host = et.getText().toString();
socket = new Socket(host, 30000);
// 设置10秒之后即觉得是超时
socket.setSoTimeout(10000);

BufferedReader br = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
line = br.readLine();
Log.d("Read:", line);

br.close();
socket.close();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("UnknownHost", "没找到主机");
e.printStackTrace();
} catch (IOException e) {
Log.e("IOException", "输入输出出现错误");
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
display = (TextView) findViewById(R.id.display);
display.setText(line);
}});
}
}.start();
}
};
btn.setOnClickListener(listener);
}
}
      

这里需要注意的是,与网络相关的更新UI界面的一定不以在主线程中进行更,必需要用到Handler实现UI更新

好了。看一下布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="72dp"
android:text="Link to Server" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:ems="10" >

<requestFocus />
</EditText>

</RelativeLayout>

 
      

最后。不得不提的是,由于用到了网络,所以在AndroidMainFest.xml中千万不要忘记加上下面权限

<uses-permission android:name="android.permission.INTERNET" />