package com.ntech.tcp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
TCPServerDemo server = new TCPServerDemo();
server.startServer();
}
private ServerSocket serverSocket;
private DataInputStream dataInputStream;
public void startServer(){
Socket socket =null;
try {
serverSocket = new ServerSocket(10003);
socket = serverSocket.accept();
// //3、获取输入流,并读取客户端信息
// InputStream is = socket.getInputStream();
// InputStreamReader isr =new InputStreamReader(is);
// BufferedReader br =new BufferedReader(isr);
// String info =null;
// while((info=br.readLine())!=null){
// System.out.println("我是服务器,客户端说:"+info);
// }
// socket.shutdownInput();//关闭输入流
// //4、获取输出流,响应客户端的请求
// OutputStream os = socket.getOutputStream();
// PrintWriter pw = new PrintWriter(os);
// pw.write("欢迎您!");
// pw.flush();
//
//
// //5、关闭资源
// pw.close();
// os.close();
// br.close();
// isr.close();
// is.close();
// socket.close();
// serverSocket.close();
dataInputStream = new DataInputStream(socket.getInputStream());
GetMessageFromClient();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(socket!=null){
try{
socket.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
private void GetMessageFromClient(){
try {
//获取消息的长度
int length = dataInputStream.available();
System.out.println(length);
//获取消息
byte[] body = new byte[length];
dataInputStream.read(body);
String message = HexStringUtil.bytesToHexString(body);
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ntech.tcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientDemo {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
String ip="127.0.0.1"; //服务器端ip地址
int port=10003; //端口号
Socket socket=new Socket(ip,port);
// //2、获取输出流,向服务器端发送信息
// OutputStream os = socket.getOutputStream();//字节输出流
// PrintWriter pw =new PrintWriter(os);//将输出流包装成打印流
// pw.write("用户名:admin;密码:123");
// pw.flush();
// socket.shutdownOutput();
// //3、获取输入流,并读取服务器端的响应信息
// InputStream is = socket.getInputStream();
// BufferedReader br = new BufferedReader(new InputStreamReader(is));
// String info = null;
// while((info=br.readLine())!=null){
// System.out.println("我是客户端,服务器说:"+info);
// }
//
// //4、关闭资源
// br.close();
// is.close();
// pw.close();
// os.close();
// socket.close();
//2.传输内容
String content_1 = "C3";
String content_2 = "30351A";
// byte[] b_1 = content_1.getBytes("UTF-8");
// byte[] b_2 = content_2.getBytes("UTF-8");
byte[] b_1 = HexStringUtil.hexStringToBytes(content_1);
byte[] b_2 = HexStringUtil.hexStringToBytes(content_2);
System.out.println(b_1.length);
System.out.println(b_2.length);
byte[] bstream = new byte[b_1.length + b_2.length];
System.arraycopy(b_1, 0, bstream, 0, b_1.length);
System.arraycopy(b_2, 0, bstream, b_1.length, b_2.length);
System.out.println(HexStringUtil.bytesToHexString(bstream));
// byte[] bstream=content.getBytes("UTF-8"); //转化为字节流
OutputStream os=socket.getOutputStream(); //输出流
os.write(bstream);
//3.关闭连接
socket.close();
}
}
package com.ntech.tcp;
public class HexStringUtil {
/**
* @Title:bytes2HexString
* @Description:字节数组转16进制字符串
* @param b
* 字节数组
* @return 16进制字符串
* @throws
*/
public static String bytes2HexString(byte[] b) {
StringBuffer result = new StringBuffer();
String hex;
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
/**
* @Title:hexString2Bytes
* @Description:16进制字符串转字节数组
* @param src
* 16进制字符串
* @return 字节数组
* @throws
*/
public static byte[] hexString2Bytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = (byte) Integer
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}
/**
* @Title:string2HexString
* @Description:字符串转16进制字符串
* @param strPart
* 字符串
* @return 16进制字符串
* @throws
*/
public static String string2HexString(String strPart) {
// StringBuffer hexString = new StringBuffer();
// for (int i = 0; i < strPart.length(); i++) {
// int ch = (int) strPart.charAt(i);
// String strHex = Integer.toHexString(ch);
// hexString.append(strHex);
// }
// return hexString.toString();
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = strPart.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
// sb.append(' ');
}
return sb.toString().trim();
}
/**
* @Title:hexString2String
* @Description:16进制字符串转字符串
* @param src
* 16进制字符串
* @return 字节数组
* @throws
*/
public static String hexString2String(String src) {
// String temp = "";
// for (int i = 0; i < src.length() / 2; i++) {
// temp = temp
// + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),
// 16).byteValue();
// }
// return temp;
String str = "0123456789ABCDEF";
char[] hexs = src.toCharArray();
byte[] bytes = new byte[src.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
public static void main(String[] args) {
String hex = "C330351A";
String str = "�05�";
System.out.println(hexString2String(hex));
System.out.println(string2HexString(hexString2String(hex)));
byte[] b = hexStringToBytes(hex);
System.out.println(new String(b));
System.out.println(bytes2HexString(b));
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* Convert hex string to byte[]
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* Convert char to byte
* @param c char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}