最近在做一个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)); } } }
}
新闻热点
疑难解答