码迷,mamicode.com
首页 > 移动开发 > 详细

android socket 之间的信息交互

时间:2014-11-10 17:16:49      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   os   使用   

创建java项目    

SocketServer.java

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class SocketServer {

  private static ServerSocket serivce;

  

  public static void main(String[] args) throws IOException {

    socket1();
  }
  public static void socket1()throws IOException{
  serivce = new ServerSocket(30000);
  while (true) {
  //等待客户端连接
    Socket socket = serivce.accept();
    new Thread(new AndroidRunable(socket)).start();
  }
  }

}

 

创建使用线程

 

AndroidRunable.java

 

package cn.socket.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class AndroidRunable implements Runnable{
Socket socket = null;

public AndroidRunable(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
// 向android客户端输出hello worild
String line = null;
InputStream input;
OutputStream output;
String str = "你好!hello word";
try {
//向客户端发送信息
output = socket.getOutputStream();
input = socket.getInputStream();
BufferedReader bff = new BufferedReader(
new InputStreamReader(input));
output.write(str.getBytes("UTF-8")); //这个编码格式 可以使用gbk
output.flush();
//半关闭socket
socket.shutdownOutput();
//获取客户端的信息
while ((line = bff.readLine()) != null) {
System.out.print("\n"+line);
}
//关闭输入输出流
output.close();
bff.close();
input.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}

}

 

点击运行 java 

bubuko.com,布布扣

创建android 项目

MainActivity.java 

 

package com.example.socketan;

import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener{
private EditText edt_ip, edt_socket, edt_connect;
private Button btn_socket;
private String ip="192.168.12.6";
private int tcp=30000;
Socket socket = null;
String buffer = "";
String geted1;
// 定义与服务器通信的子线程
// ClientThread clientThread;
public Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x11) {
Bundle bundle = msg.getData();
edt_socket.append("server:"+bundle.getString("msg")+"\n");
}
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_ip = (EditText) findViewById(R.id.edt_ip);
edt_socket = (EditText) findViewById(R.id.edt_socket);
edt_connect = (EditText) findViewById(R.id.edt_connect);
btn_socket = (Button) findViewById(R.id.btn_socket);
btn_socket.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_socket) {
geted1 = edt_connect.getText().toString();
edt_socket.append("client:"+geted1+"\n");
// 客户端启动ClientThread线程创建网络连接、读取来自服务器的数据
new MyThread(geted1,ip,tcp,myHandler).start();
}

}

}

 

 

创建android使用线程

 

MyThread.java

 

package com.example.socketan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MyThread extends Thread {
private String str,ip;
private int tcp;
Socket socket=null;
String buffer=null;
private Handler myHandler;
public MyThread(String str,String ip,int tcp,Handler myHandler){
this.str=str;
this.ip=ip;
this.tcp=tcp;
this.myHandler=myHandler;
}
@Override
public void run() {
super.run();
//定义消息
Message msg = new Message();
msg.what = 0x11;
Bundle bundle = new Bundle();
bundle.clear();
try {
//连接服务器 并设置连接超时为5秒
socket = new Socket();
socket.connect(new InetSocketAddress(ip, tcp), 5000);
//获取输入输出流
OutputStream ou = socket.getOutputStream();
BufferedReader bff = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
//读取发来服务器信息
String line = null;
buffer="";
while ((line = bff.readLine()) != null) {
buffer = line + buffer;
}

//向服务器发送信息
ou.write(str.getBytes("gbk"));
ou.flush();
bundle.putString("msg", buffer.toString());
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
//关闭各种输入输出流
bff.close();
ou.close();
socket.close();
} catch (SocketTimeoutException aa) {
//连接超时 在UI界面显示消息
bundle.putString("msg", "服务器连接失败!请检查网络是否打开");
msg.setData(bundle);
//发送消息 修改UI线程中的组件
myHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
}

 bubuko.com,布布扣

android socket 之间的信息交互

标签:android   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/-long/p/4087382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!