在Android中可以直接利用java中的Socket与ServerSocket构建socket通信。 代码的运行环境: pc端:普通pc,作为服务器,在5648端口进行监听。
代码运行一切正常,客户端发送的文字将在服务器端接收并显示,服务器每接收到客户端的一行文字,就会返回一个从0开始递增的整数,此整数将在客户端显示出来。 pc端代码:
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Test implements Runnable{ /** * @param args */ int max=10; //最大开启线程数 int i=0; //回复数字 int temp; ServerSocket serverSocket; Socket socket[]; public Test(){ try { serverSocket=new ServerSocket(5648); //在5648端口进行侦听 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("can't initate ServerSocket!"); return; } socket=new Socket[max]; System.out.println("waiting for connect"); try { while((socket[i]=serverSocket.accept())!=null){ temp=i; i++; new Thread(this).start(); //每侦听到一个客户端的连接,就会开启一个工作线程 } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { new Test(); } @Override public void run() { Socket sk=socket[temp]; System.out.println("accept:"+sk.getInetAddress().getHostAddress()); InputStream is=null; OutputStream os=null; BufferedReader br=null; PrintWriter pw=null; try { is=sk.getInputStream(); os=sk.getOutputStream(); br=new BufferedReader(new InputStreamReader(is)); pw=new PrintWriter(os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); try { sk.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return; } String str; try { int m=0; while((str=br.readLine())!=null){ System.out.println(str); pw.println(m); pw.flush(); m++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
android设备客户端代码: MainActivity.java;代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; 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.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener,Runnable{ private Button button; private EditText editText; private Socket socket; private PrintWriter pw; private BufferedReader br; private Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler=new Handler(); button=(Button)findViewById(R.id.button); button.setOnClickListener(this); editText=(EditText)findViewById(R.id.edittext); try { socket=new Socket("tobacco5648.xicp.net",5648); //连接到tobacco5648.xicp.net的5648端口 } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("socket","unknown host"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("socket","io execption"); } if(socket==null){ Log.e("socket","null"); } else try { pw=new PrintWriter(socket.getOutputStream()); br=new BufferedReader(new InputStreamReader(socket.getInputStream())); if(pw!=null&&br!=null){ new Thread(this).start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View view) { if(view==button){ String str; str=editText.getText().toString(); pw.println(str); pw.flush(); } } public void run() { try { String str; while((str=br.readLine())!=null){ final String s=str; handler.post(new Runnable(){ public void run() { Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show(); }}); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/3229.html