android socket 之间的信息交互

创建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 

创建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();
}
}
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。