整了好久,查了好多文件才整的有点明白
package com.bn.tcp;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SocketClientActivity extends Activity {
// 设置服务器IP和端口
private static final String SERVERIP ="192.168.5.178";//服务器端IP
private static final int SERVERPORT = 4001;//端口号
private Socket _socket = null;
//private BufferedReader _bufferedReader = null;
private static PrintWriter _printWriter = null;
private static EditText _editText02 = null;
private static EditText _editText01 = null;
private static Button _connectBtn = null;
private static Button _sumbitBtn = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化文本框
_editText01 = (EditText)findViewById(R.id.EditText01);
_editText02 = (EditText)findViewById(R.id.EditText02);
// 接收消息不可编辑
_editText02.setEnabled(false);
_connectBtn = (Button)findViewById(R.id.connectBtn);
_sumbitBtn = (Button)findViewById(R.id.sumbitBtn);
// 连接服务器
_connectBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
// 连接按钮不可用
_connectBtn.setEnabled(false);
try {
// 连接服务器
_socket = new Socket(SERVERIP, SERVERPORT);
// 取得输入/输出流
//_bufferedReader = new BufferedReader(new InputStreamReader(_socket.getInputStream(), "GB2312"));
_printWriter = new PrintWriter(_socket.getOutputStream(), true);
// 开启监听线程
_thread.start();
}
catch (Exception e) {
System.out.println("Connect is failed!");
}
}
}
);
// 发送信息
_sumbitBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
String msg = _editText01.getText().toString();
sendMessage(msg);
}
}
);
}
// 发送信息到服务器
public void sendMessage(String message){
try {
//String s4="05 00 00 00 23 00 01 02 03 04";
char charArray[]=new char[13];//16进制发送
charArray[0]=0x08;
charArray[1]=0x00;
charArray[2]=0x00;
charArray[3]=0x00;
charArray[4]=0x23;
charArray[5]=0x00;
charArray[6]=0x01;
charArray[7]=0x02;
charArray[8]=0x03;
charArray[9]=0x04;
charArray[10]=0x05;
charArray[11]=0x06;
charArray[12]=0x07;
_printWriter.print(charArray);
_printWriter.flush();
}
catch (Exception e) {
}
}
private Thread _thread = new Thread(){
public void run(){
while(true){
try{
InputStream in = _socket.getInputStream();
byte[] buffer = new byte[in.available()];
in.read(buffer);
String mm="";
//接收到的数据转成16进制
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF);
if (hex.length() == 1) {
mm += '0' + hex+" ";
}else{
mm+=hex+" ";
}
}
String msg=mm;//转成16进制
if(msg.length() != 0){
// 在控制台中输出接收到的信息
System.out.println("in: " + msg);
// 该部分是传参并更新控件
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", msg);
message.setData(bundle);
// 发送消息到handler
_handler.sendMessage(message);
}
}
catch (Exception e){
}
}
}
};
Handler _handler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
// 刷新信息
try{
// 输出接受到的数据(更新控件)
SocketClientActivity._editText02.setText(msg.getData().getString("msg"));
}
catch (Exception e){
}
}
};
}
//-----------------------------------------------------------------------------------------------------------------------布局
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:text="连接到服务器"
android:id="@+id/connectBtn"
android:layout_width="300dip"
android:layout_height="60dip">
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/sumbitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/EditText02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
//---------------------------------------------------------------------------------------------------------------------AndroidManifest.xml
package="com.bn.tcp"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
//通信要加上这句话!