TCP适合传输自定义原始的字节流,比如传输一个序列化为字节流后的对象或者结构体,发送方按约定的自定义报文结构发送,接收方按约定的自定义报文解码。一种传输字流节,适合传输结构体和对象,一种传输出字符串。
??
class="java"> package com.magcomm.net; 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.io.Reader; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.nio.CharBuffer; import org.apache.http.util.EncodingUtils; import android.util.Log; public class SocketClient { private static final String tags = "com.magcomm.nmc, SocketClient"; private Socket socket; private final String SERVER_HOST_IP = "192.168.0.15"; private final String SERVER_HOST_NAME = "mail.magcomm.cn"; private final int SERVER_HOST_PORT = 58889; private InputStream mInputStream; private OutputStream mOutputStream; private PrintWriter mPrintWriter; private BufferedReader reader; public SocketClient() { } public boolean connection() { boolean bRet = false; try { //创建一个客户端连接 InetAddress ia = InetAddress.getByName(SERVER_HOST_NAME); socket = new Socket(ia, SERVER_HOST_PORT); //socket.connect(new InetSocketAddress(SERVER_HOST_NAME, SERVER_HOST_PORT),10000); bRet = true; } catch (Exception e) { // TODO: handle exception Log.i(tags, "socket open error" + e.getMessage()); } return bRet; } //发送消息 public boolean sendMsg(String msg) { boolean bRet = false; if (socket != null) { if (socket.isConnected()) { //获取这个客户端连接SOCKET的输入输出 try { mPrintWriter = new PrintWriter(socket.getOutputStream(), true); mPrintWriter.print(msg); mPrintWriter.flush(); bRet = true; } catch (IOException e) { // TODO Auto-generated catch block Log.i(tags, "write socket error"); e.printStackTrace(); } } } return bRet; } public boolean sendMsg2(byte[] buffer, int offset, int count) { boolean bRet = false; if (socket != null) { if (socket.isConnected()) { //获取这个客户端连接SOCKET的输入输出 try { mOutputStream = socket.getOutputStream(); mOutputStream.write(buffer, offset, count); mOutputStream.flush(); Log.i("NMCDataUnPack", "socket send success--------"); bRet = true; } catch (IOException e) { // TODO Auto-generated catch block Log.i(tags, "write socket error"); e.printStackTrace(); } } } return bRet; } //读取消息 public String readMsg() { String msgString = ""; try { mInputStream = socket.getInputStream(); byte buffer[] = new byte[1024]; int reCount = mInputStream.read(buffer); msgString = EncodingUtils.getString(buffer, /*"GB2312"*/"US-ASCII"); }catch (Exception e) { Log.i(tags, "read socket error"); e.printStackTrace(); } return msgString; } public String readMsg2() { String msgString = ""; try { char[] buffer = new char[1024]; Reader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); reader.read(buffer); msgString = buffer.toString(); } catch (IOException e) { Log.i(tags, "read socket error"); e.printStackTrace(); } return msgString; } public void close() { try { mInputStream.close(); mPrintWriter.close(); mOutputStream.close(); socket.close(); } catch (Exception e) { // TODO: handle exception Log.i(tags, "socket close error" + e.getMessage()); } } }
?