Socket通信实例一

来源:互联网 发布:炒股哪个软件排名 编辑:程序博客网 时间:2024/05/16 09:50

Socket通信实例:

两个平板通过网络通信,一个作为client,一个作为server

如下为client的代码:

private class ListenThread extends Thread {@Overridepublic void run() {// TODO Auto-generated method stubwhile (!isClose) {try {socket = new Socket();if (MainApplication.getInstance().playerIp != null) {mplayerIP = MainApplication.getInstance().playerIp;}SocketAddress socAddress = new InetSocketAddress(mplayerIP,1235);socket.connect(socAddress, Config.QUICKPASS_CONNECT_OUTTIME);} catch (IOException e) {Log.e(TAG, "连接客显失败,请设置客显IP地址或者启动客显");try {Thread.sleep(2000);} catch (InterruptedException interupt) {interupt.printStackTrace();}try {socket.close();} catch (IOException e1) {e1.printStackTrace();}continue;}try {socket.setSoTimeout(Config.QUICKPASS_READ_OUTTIME);socketOutputStream = socket.getOutputStream();socketInputStream = socket.getInputStream();int buffer_len = 0;int size = 0;while ((size != -1) && (!isClose)) {if(socketInputStream == null || buffer == null){Log.e(TAG, "sorry,socketInputStream or buffer null");break;}try {size = socketInputStream.read(buffer, buffer_len,buffer.length - buffer_len);} catch(IOException e) {Log.e(TAG,"socket timeout read nothing");continue;//读取超时,重新执行内部while循环}if(size == -1){continue;}buffer_len = (buffer_len + size) % buffer.length;if (buffer[(buffer_len - 1 + buffer.length) % buffer.length] == (byte) endflag) {if (Debug) {debug_buffer.append("buffer_len = "+ buffer_len + "\nbuffer = " + Funs.bytesToHexStr(buffer,0,buffer_len));Log.i(TAG,debug_buffer.toString());debug_buffer.setLength(0);}if (handler != null) {handler.obtainMessage(msg_what,(Integer) buffer_len).sendToTarget();}}}} catch (OptionalDataException e) {e.printStackTrace();} catch (StreamCorruptedException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {Log.i(TAG, "close socket");try {if(socketOutputStream != null){socketOutputStream.close();socketOutputStream = null;}if(socketInputStream != null){socketInputStream.close();socketInputStream = null;}if(socket != null){socket.close();socket = null;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}Log.i(TAG,"close socket client thread!");}}

两层while循环,外层8-113行,里层47到78行。
外层while循环的10到37行可以说也是一个循环,不断的去尝试连接服务端,也就是另一个平板。如果连接超时,17行就会抛出IOException异常,异常被catch捕获,catch中语句37行,continue,又让程序执行到while开始,如此循环,知道连接上服务端。
连上服务端之后,分别得到输入输出流,同时设置读取超时,然后执行到内层while循环。
不断读取从服务端发送过来的数据,如果读取超时,会捕获异常并重复读取(54-59行),如果读取的是-1,说明服务端已经关闭close,此时也会根据内层while循环的判断条件跳出内层while循环,并执行到finally。

然后又开始循环执行到外层while循环,又开始不断的尝试连接服务端。


如下为server端的代码:

private class ListenThread extends Thread{@Overridepublic void run() {// TODO Auto-generated method stubif(serverSocket == null){Log.e(TAG, "sorry ,serverSocket null");return;}while(!isClose) {Socket socket = null;try {socket = serverSocket.accept();} catch (IOException e) {// TODO Auto-generated catch blockLog.e(TAG, "连接主显失败,请设置IP地址或者启动主显");try {socket.close();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}continue;}int buffer_len = 0;int size = 0;try {socket.setSoTimeout(6000);socketInputStream = socket.getInputStream();socketOutputStream = socket.getOutputStream();while(!isClose && (size != -1)){try {size = socketInputStream.read(buffer,buffer_len,buffer.length - buffer_len);} catch(IOException ee){Log.e(TAG,"socket timeout read nothing");continue;//读取超时,重新执行while循环}if(size == -1){continue;}buffer_len = (buffer_len +size)%buffer.length;if(buffer[(buffer_len-1+buffer.length)%buffer.length] == (byte)0x03){if(Debug){debug_buffer.append("buffer_len = "+buffer_len+"\nbuffer = "+Funs.bytesToHexStr(buffer, 0, buffer_len));Log.i(TAG,debug_buffer.toString());debug_buffer.setLength(0);}if(handler != null){handler.obtainMessage(msg_what,(Integer)buffer_len).sendToTarget();}}}} catch (OptionalDataException e) {e.printStackTrace();} catch (StreamCorruptedException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch(IOException e) {e.printStackTrace();} finally {try {Log.e(TAG,"close socket");if(socketInputStream!= null) {socketInputStream.close();socketInputStream= null;}if(socketOutputStream != null){socketOutputStream.close();socketInputStream = null;}if(socket != null){socket.close();socket = null;}} catch(IOException e){e.printStackTrace();}}}Log.e(TAG,"server socket thread close");}}


也是两层while循环,外层12-94,内层,34-61。
外层一直阻塞在15行,等待客户端的连接,客户端连上之后,分别得到输入输出流,同时设置读取超时,然后执行到内层while循环。
内层while循环,其余的和客户端大同小异。

0 0
原创粉丝点击