首页 > 学院 > 开发设计 > 正文

socket接收服务器数据,输出为乱码或无法输出的解决方法

2019-11-08 01:16:51
字体:
来源:转载
供稿:网友

最近在做一个socket的小项目,其中出现了一个问题,就是当我用socket接收到服务器发送的数据后,无法输出接收到的数据或者输出的数据为乱码,经过几天的摸索,终于解决了这个问题,记录一下。不多说,直接上代码

public class client {public static final String ip_ADDR = "**.**.**.**";//服务器地址 public static final int PORT = **;//服务器端口号 public static void main(String[] args) throws IOException {       showScreen screen=new showScreen();      System.out.PRintln("客户端启动...");      Socket socket = null;      socket = new Socket(IP_ADDR, PORT);       boolean isConnection=socket.isConnected() && !socket.isClosed(); //判断当前是否处于连接      DataInputStream input = new DataInputStream(socket.getInputStream());  //用DataInputStream存放接收到的数据      byte []buf=new byte[1024];      int readnum=0;      while(true){         readnum=input.read(buf);                         if(readnum>0){                   System.out.println(Arrays.toString(buf));              }           }   }

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表