连接socket服务器

	/**
	 * 连接socket服务器返回数据接口方法
	 * @param host IP
	 * @param port 端口号
	 * @param xmlString 推送数据字符串
	 * @return String 返回数据字符串
	 * @throws IOException
	 */
	public String ConnectScoket(String host,int port,String xmlString ) throws IOException{
		Socket socket = new Socket(host, port);
		socket.setSoTimeout(10000);
		//流 输出流字符流
		OutputStream outs=socket.getOutputStream();
		System.out.println("发送的数据="+xmlString);
		outs.write(xmlString.getBytes("utf-8")); 
		outs.flush();
		//接收服务器返回数据
		InputStream ins=socket.getInputStream();
		BufferedReader reader=new BufferedReader(new InputStreamReader(ins));
	    String s=reader.readLine();
	    System.out.println("接到服务器回复:"+s);
	    socket.close();
	    return s;
	}

猜你喜欢

转载自nxdjava.iteye.com/blog/2388126