/** * 连接服务器 * @param serverIp 服务器ip * @param port 端口 * @param msg 消息 * @return 服务端返回数据 */public static String getSocketResult(String serverIp,int port,String msg){Socket socket=null;String result=null;InputStream in=null;OutputStream outputStream=null;try {//套接字连接服务器InetAddress severAddress=InetAddress.getByName(serverIp);socket=new Socket(severAddress, port);    outputStream=socket.getOutputStream();outputStream.write(msg.getBytes("gbk"));outputStream.flush();in=socket.getInputStream();int count=0;        byte[] inDatas = null;while (count == 0) {count = in.available();}inDatas = new byte[count];in.read(inDatas);in.close();return new String(inDatas, "gb2312");} catch (Exception e) {// TODO: handle exception}finally{if(outputStream!=null){try {outputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(in!=null){try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return result;}

更多相关文章

  1. Android(安卓)封装Okhttp3 + RxJava 工具类(包含下载)
  2. Android(安卓)表单模拟,文件上传!
  3. PC通过USB连接Android通信(Socket)
  4. Android之HttpPost与HttpGet使用
  5. android上传图片或文件
  6. 如何保证手机端的app访问web服务器的安全
  7. Binder框架的一些简单总结(关于自定义服务中的Binder)
  8. android和PC(Python)通过USB(adb模式)基于Socket传输图像(视频)
  9. android上传文件至服务器(android端+服务器端)

随机推荐

  1. 失眠三重天
  2. 3-8(单链表相关算法习题+双链表)
  3. C++入门第一课(命名空间;输入输出;缺省参数;
  4. 闲聊微服务之服务注册中心
  5. php中的类及trait
  6. 第十周作业
  7. Python VS Java如何选择?Python学习分析!
  8. 2021-03-08:在一个数组中,任何一个前面的数
  9. vue基础知识 指令
  10. 2021年值得推荐的几个可视化软件