·使用Socket进行简单服务器通信
简单服务器端:
public class ServerThread implements Runnable{
Socket s=null;
BufferedReader br=null;
public ServerThread(Socket s) throws UnsupportedEncodingException, IOException {
this.s=s;
br=new BufferedReader(new InputStreamReader(s.getInputStream(), "utf-8"));
}
@Override
public void run() {
try{
String content=null;
while((content=readFromClient())!=null){
for(Iterator<Socket>it=Myserver.socketList.iterator();it.hasNext();){
Socket s=it.next();
try{
OutputStream os=s.getOutputStream();
os.write((content+"\n").getBytes("utf-8"));
}catch(SocketException e){
e.printStackTrace();
it.remove();
System.out.println(Myserver.socketList);
}
}
}
}catch(IOException e){
e.printStackTrace();
}
}
private String readFromClient(){
try{
return br.readLine();
}catch(IOException e){
e.printStackTrace();
Myserver.socketList.remove(s);
}
return null;
}
}
public class Myserver {
public static ArrayList<Socket>socketList=new ArrayList<Socket>();
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(31510);
while(true){
Socket s=ss.accept();
socketList.add(s);
new Thread(new ServerThread(s)).start();
}
}
}
android应用端:
public class ClientThread implements Runnable{
private Socket s;
//发送,接受UI线程信息的Handler对象
private Handler handler;
public Handler revHandler;
BufferedReader br=null;
OutputStream os=null;
//构造函数
public ClientThread(Handler handler){
this.handler=handler;
}
@Override
public void run() {
try{
s=new Socket("222.26.207.88",31510);
//输入流将Socket读入BufferedReader
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
os=s.getOutputStream();
new Thread(){
public void run() {
String content=null;
try{
while((content=br.readLine())!=null){
Message msg=new Message();
msg.what=0x123;
//将BufferedREader中的数据读入content后加入message
msg.obj=content;
//通过handler发送消息至消息队列
handler.sendMessage(msg);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}.start();
//初始化Looper
Looper.prepare();
revHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
//接收到UI线程中用户输入的数据
if(msg.what==0x345){
try{
//将用户在文本框的内容写入网络
os.write((msg.obj.toString()+"\r\n").getBytes("utf-8"));
}
catch(Exception e){
e.printStackTrace();
}
}
}
};
//启动Looper
Looper.loop();
}catch(SocketException e){
System.out.println("超时");
}catch(Exception e){
e.printStackTrace();
}
}
}
public class MainActivity extends Activity {
EditText input;
TextView show;
Button send;
Handler handler;
//与服务器通信的子线程
ClientThread clientThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.input);
show=(TextView)findViewById(R.id.show);
send=(Button)findViewById(R.id.send);
//将子线程中的内容显示在文本框
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
show.append("\n"+msg.obj.toString());
}
super.handleMessage(msg);
}
};
clientThread=new ClientThread(handler);
//读取来自服务器的数据
new Thread(clientThread).start();
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
Message msg=new Message();
msg.what=0x345;
msg.obj=input.getText().toString();
clientThread.revHandler.sendMessage(msg);
input.setText("");
}
catch(Exception e){
e.printStackTrace();
}
}
});
}
}