Java 如何转换长-->;byteArray->;字符串-->;byteArray-->;长过插座?
我想通过包含长值的套接字发送消息。我想发送字符串消息。为了节省字节并缩短消息,我想发送一个64位长的8字符字符串的字节值。以下是我的代码,但我在套接字的另一侧收到了错误的值: 此服务器:Java 如何转换长-->;byteArray->;字符串-->;byteArray-->;长过插座?,java,string,sockets,byte,Java,String,Sockets,Byte,我想通过包含长值的套接字发送消息。我想发送字符串消息。为了节省字节并缩短消息,我想发送一个64位长的8字符字符串的字节值。以下是我的代码,但我在套接字的另一侧收到了错误的值: 此服务器: public class ClockSkewServer { public static void main (String args[]){ try { ServerSocket s = new ServerSocket(3000); System.out.printl
public class ClockSkewServer {
public static void main (String args[]){
try {
ServerSocket s = new ServerSocket(3000);
System.out.println("Server is listening!");
Socket connection = s.accept();
connection.setKeepAlive(true);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter pw = new PrintWriter(connection.getOutputStream(), true);
while (true) {
String message = br.readLine();
System.out.println(message);
Long receivedTime = ByteUtil.stringToLong(message);
System.out.println(receivedTime);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是客户:
public class ClockSkewClient {
public static void main (String args[]) throws UnknownHostException, IOException, InterruptedException{
Socket s1 = new Socket(args[0], 3000);
s1.setKeepAlive(true);
PrintWriter out1 = new PrintWriter(s1.getOutputStream(), true);
long l = new Long("1490917469228");
//System.out.println(l);
while (true){
Thread.sleep(1000);
String message = ByteUtil.longToString(l);
System.out.println("Client message: " + message);
System.out.println(ByteUtil.stringToLong(message));
out1.println(message);
}
}
}
这是我的字节转换类:
public class ByteUtil {
private static ByteBuffer longBuffer = ByteBuffer.allocate(Long.BYTES);
private static ByteBuffer shortBuffer = ByteBuffer.allocate(Short.BYTES);
private static ByteBuffer byteBuffer = ByteBuffer.allocate(Byte.BYTES);
public static byte[] longToBytes(long x) {
longBuffer.putLong(0, x);
return longBuffer.array();
}
public static long bytesToLong(byte[] bytes) {
return ByteBuffer.wrap(bytes).getLong();
}
public static String longToString (long x) throws UnsupportedEncodingException{
return new String (longToBytes(x), "ISO-8859-1");
}
public static long stringToLong (String s) throws UnsupportedEncodingException{
return bytesToLong(s.getBytes("ISO-8859-1"));
}
}
ByteUtil工作正常,因为客户端可以正确地检索原始的long值,但当我通过套接字发送时,它会失真
我在客户端成功地得到了long值1490917469228,但在服务器端得到了14909911439916
我知道我们可以发送字节并避免这个问题,但出于某种原因,我坚持通过套接字发送字符串
谢谢
我想通过包含长值的套接字发送消息。我想发送字符串消息。为了节省字节并缩短消息,我想发送一个64位长的8字符字符串的字节值
为什么??将64位作为8个字符发送可能需要16个字节。128位。它当然不会节省任何字节,所有这些转换只是浪费时间和代码空间:尤其是开发时间,尤其是在代码无法工作的情况下
只需使用
DataOutputStream.writeLong()
和DataInputStream.readLong()
InputStreamReader
有一个用于编码的可选参数,这与采用计算机默认编码一样重要。使用long
-不要使用long
。这种方法有几个问题。例如,包含换行字符的字节序列。完全二进制,使用InputStream而不是Reader。或者简单地打开读写器上的压缩(GZipOutputStream,deflate的头)String
不是二进制数据的容器。您“坚持通过套接字发送字符串”,原因如下:?即使它与你的其他目标相冲突?你是对的。但这就是原因。我有一个非常简单的协议,它接收消息并通过分隔符检索消息的各个部分,例如;或者:你是对的,但问题是我有一个应用程序,它是用基于字符串的协议编写的。是的。最好使用GoogleProtocolBuffer作为索引,但我现在不想对代码做太多更改。我只是想节省一些开销。如果yiu有很多长或短的值,我甚至需要16个字节。什么是16个字节?如果要使用String
,则无法保存任何内容,除非值很小。您必须以ASCII格式发送数据,与您的文本格式相同。对于indtance,如果您以字符串形式发送1490917469228,则数据将为13个字符,因此为13个字节。但是你只能发送8个字节。若你们有更大的数字,那个么它可以超过13个字节。当然,但你们永远不能保存16个字节。最长的long
是9223372036854775807,是19位数字,每个二进制long
需要8个字节。保存:最多11字节。任何保存都需要大量数据才能成为siginficant。想象一下,你有很多clinets,它们每秒的运算量总计为1m。在这种情况下,仅保存一个字节每秒可节省1MB。