62,616
社区成员
发帖
与我相关
我的任务
分享
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 8189);
OutputStream out = s.getOutputStream();
ObjectOutputStream objectout = new ObjectOutputStream(out);
packhead headheart = new packhead();
String strHeadFrom = "javaserver";
headheart.from = strHeadFrom.getBytes();
strHeadFrom = "vcclient";
headheart.to = strHeadFrom.getBytes();
headheart.nlength = 40;
headheart.ntype = 90;
objectout.writeObject(headheart);
System.out.println("发送完毕");
Thread.sleep(5000);
s.close();
}
}
class packhead implements Serializable {
byte[] from = new byte[16];
byte[] to = new byte[16];
int ntype;
int nlength;
}
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception{
ServerSocket server = new ServerSocket(8189);
Socket s = server.accept();
InputStream in = s.getInputStream();
ObjectInputStream objectin = new ObjectInputStream(in);
System.out.println("1");
packhead receive = (packhead)objectin.readObject();
System.out.println("接收完毕");
byte[] from = receive.from;
byte[] to = receive.to;
int ntype = receive.ntype;
int nlength = receive.nlength;
String fromString = new String(from);
String toString = new String(to);
System.out.println(fromString);
System.out.println(toString);
System.out.println(nlength);
System.out.println(ntype);
s.close();
}
}
import java.io.*;
import java.util.Arrays;
class PackHead implements Serializable {
byte[] from = new byte[16];
byte[] to = new byte[16];
int ntype;
int nlength;
public PackHead(byte[] from, byte[] to, int ntype, int nlength) {
super();
this.from = from;
this.to = to;
this.ntype = ntype;
this.nlength = nlength;
}
public String toString() {
return Arrays.toString(from) + " " + Arrays.toString(to) + " " + ntype + " " + nlength;
}
}
// 在socket通讯的时候这么写,但是效果却达不到,
public class ConvertByte {
public static void main(String[] args) throws Exception {
PackHead headheart = new PackHead("javaserver".getBytes(),"vcclient".getBytes(),40,90);
System.out.print("旧对象是");
System.out.println(headheart);
byte[] bufobject = Byte_File_Object.getBytesFromObject(headheart);
System.out.println("原对象转换后的字节对象为" + bufobject);
System.out.println("原对象转换后的字节数据为" );
for(int i=0 ; i<bufobject.length ; i++) {
System.out.print(bufobject[i] + " ");
}
System.out.println();
PackHead headto = (PackHead) Byte_File_Object
.getObjectFromBytes(bufobject);
System.out.print("新对象是");
System.out.println(headto);
byte[] bufobject2 = Byte_File_Object.getBytesFromObject(headto);
System.out.println("新对象转换后的字节数据为" + bufobject2);//此时是新对象,内存地址当然不同,结果的hashcode也是不同的
System.out.println("新对象转换后的字节数据为" );
for(int i=0 ; i<bufobject2.length ; i++) {
System.out.print(bufobject2[i] + " ");
}
System.out.println();
}
}
class Byte_File_Object {
public static Object getObjectFromBytes(byte[] objBytes) throws Exception {
if (objBytes == null || objBytes.length == 0) {
return null;
}
ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
ObjectInputStream ois = new ObjectInputStream(bi);
return ois.readObject();
}
public static byte[] getBytesFromObject(Serializable obj) throws Exception {
if (obj == null) {
return null;
}
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bo);
oos.writeObject(obj);
return bo.toByteArray();
}
}
/*
旧对象是[106, 97, 118, 97, 115, 101, 114, 118, 101, 114] [118, 99, 99, 108, 105, 101, 110, 116] 40 90
原对象转换后的字节对象为[B@1a679b7
原对象转换后的字节数据为
-84 -19 0 5 115 114 0 8 80 97 99 107 72 101 97 100 -16 69 -4 -30 28 -10 78 -70 2 0 4 73 0 7 110 108 101 110 103 116 104 73 0 5 110 116 121 112 101 91 0 4 102 114 111 109 116 0 2 91 66 91 0 2 116 111 113 0 126 0 1 120 112 0 0 0 90 0 0 0 40 117 114 0 2 91 66 -84 -13 23 -8 6 8 84 -32 2 0 0 120 112 0 0 0 10 106 97 118 97 115 101 114 118 101 114 117 113 0 126 0 3 0 0 0 8 118 99 99 108 105 101 110 116
新对象是[106, 97, 118, 97, 115, 101, 114, 118, 101, 114] [118, 99, 99, 108, 105, 101, 110, 116] 40 90
新对象转换后的字节数据为[B@1e0bc08
新对象转换后的字节数据为
-84 -19 0 5 115 114 0 8 80 97 99 107 72 101 97 100 -16 69 -4 -30 28 -10 78 -70 2 0 4 73 0 7 110 108 101 110 103 116 104 73 0 5 110 116 121 112 101 91 0 4 102 114 111 109 116 0 2 91 66 91 0 2 116 111 113 0 126 0 1 120 112 0 0 0 90 0 0 0 40 117 114 0 2 91 66 -84 -13 23 -8 6 8 84 -32 2 0 0 120 112 0 0 0 10 106 97 118 97 115 101 114 118 101 114 117 113 0 126 0 3 0 0 0 8 118 99 99 108 105 101 110 116
*/