packagecom.thinkgem.jeesite.modules.socketTest.demo1;importjava.io.BufferedInputStream;importjava.io.DataInputStream;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;/*** @Author: zhouhe
* @Date: 2019/4/10 16:56*/
public classServer {private static class ClientHandler implementsRunnable {privateSocket socket;publicClientHandler(Socket socket) {this.socket =socket;
}
@Overridepublic voidrun() {try{//封装输入流(接收客户端的流)
BufferedInputStream bis = newBufferedInputStream(
socket.getInputStream());
DataInputStream dis= newDataInputStream(bis);byte[] bytes = new byte[1]; //一次读取一个byte
System.out.println("bytes:"+bytesToHexString(bytes));
String ret= "";while (dis.read(bytes) != -1) {
ret+= bytesToHexString(bytes) + "";if (dis.available() == 0) { //一个请求
System.out.println(socket.getRemoteSocketAddress() + ":" +ret);
System.out.println("转换为字符串后:"+hexStringToString(ret));
ret= "";
}
}
}catch(Exception e) {
e.printStackTrace();
}finally{
System.out.println("client is over");if (socket != null) {try{
socket.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}/*** byte[]数组转换为16进制的字符串
*
*@parambytes 要转换的字节数组
*@return转换后的结果*/
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb= newStringBuilder();for (int i = 0; i < bytes.length; i++) {
String hex= Integer.toHexString(0xFF &bytes[i]);if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}returnsb.toString();
}/*** 16进制转换成为string类型字符串
* 这个方法中文会乱码,字母和数字都不会乱码
*
* @Author zhouhe
*@params
*@return
*/
public staticString hexStringToString(String s) {if (s == null || s.equals("")) {return null;
}
s= s.replace(" ", "");byte[] baKeyword = new byte[s.length() / 2];for (int i = 0; i < baKeyword.length; i++) {try{
baKeyword[i]= (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
}catch(Exception e) {
e.printStackTrace();
}
}try{
s= new String(baKeyword, "UTF-8");newString();
}catch(Exception e1) {
e1.printStackTrace();
}returns;
}//测试方法
public static voidmain(String[] args) {
ServerSocket server= null;try{
server= new ServerSocket(10010);while (true) {
System.out.println("listening...");
Socket socket=server.accept();
System.out.println("连接客户端地址:" +socket.getRemoteSocketAddress());
System.out.println("connected...");
ClientHandler handler= newClientHandler(socket);
Thread t= newThread(handler);
t.start();
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (server != null) {try{
server.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}