Code example for Socket

Methods: bind, close, connect, getInputStream, getOutputStream, setSoTimeout

0
				Log.i(TAG, "SendCMD 错误", e);
				socketSendCMD.close();
			}  
		//接收传感器数据套接字建立 
		try{ 
			socketRecvData = new Socket();
			socketRecvData.bind(null);
			socketRecvData.setSoTimeout(SOCKET_TIMEOUT);
			socketRecvData.connect(new InetSocketAddress(address,portRecvData), SOCKET_TIMEOUT);
			/*String tmp = null; 
			if ((tmp = socketRecvDataStr(socketRecvData)) != null) 
			{ 
				textWorkTime.setText("工作时间:" + tmp); 
			}*/ 
			socketSendData("cmdSendStart",socketRecvData);
		//取得输入流 
			mBufferedReader = new BufferedReader(new InputStreamReader(socketRecvData.getInputStream()));
			 
			}catch(IOException e){
				Log.i(TAG, "socketRecvData 错误", e);
				socketRecvData.close();
			} 
		return mBufferedReader;
	} 
	//返回发送命令套接字 
	public Socket getSocketSendCMD()
	{ 
		return socketSendCMD;
	} 
	//返回接受信息套接字 
	public Socket getSocketRecvData()
	{ 
		return socketRecvData;
	} 
	 
	private void socketSendData(String str, Socket sck ) throws IOException{
		//发送数据 
		try{ 
		PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter
						(sck.getOutputStream())),true);
		out.println(str);
		}catch(Exception e)
		{ 
			Log.i(TAG, "发送数据错误",e);
		}