服务端:
package com.sux.demo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
public class TestServerSocket {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
ReceiveThread receiveThread = new ReceiveThread();
receiveThread.start();
}
}
class ReceiveThread extends Thread {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
Socket socket = serverSocket.accept();
receive(socket);
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void receive(Socket socket) throws IOException, ClassNotFoundException, InterruptedException {
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(dataInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int len;
byte[] bytes = new byte[10240];
while ((len = bufferedInputStream.read(bytes)) > 0) {
byteArrayOutputStream.write(bytes, 0, len);
}
System.out.println("Server端接收的数据:");
List<MyInfo> list = (List<MyInfo>) SerializeUtil.Deserialize(byteArrayOutputStream.toByteArray());
for (MyInfo myInfo : list) {
System.out.println(myInfo.toString());
}
}
}
View Code
客户端:
package com.sux.demo;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class TestSocket {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
SendThread sendThread = new SendThread();
sendThread.start();
}
}
class SendThread extends Thread {
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 8888);
send(socket);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void send(Socket socket) throws IOException, InterruptedException, ClassNotFoundException {
List<MyInfo> list = new ArrayList<MyInfo>();
for (int i = 0; i < 5; i++) {
MyInfo myInfo = new MyInfo();
myInfo.setName("数据" + i);
myInfo.setCode(i);
list.add(myInfo);
}
SocketUtil.Send(socket, list);
System.out.println("数据已发送");
}
}
class SocketUtil {
public static synchronized void Send(Socket socket, Object data) throws IOException, ClassNotFoundException, InterruptedException {
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(dataOutputStream);
byte[] bytes = SerializeUtil.Serialize(data);
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
}
}
View Code
Java序列化反序列化工具类:
package com.sux.demo;
import java.io.*;
public class SerializeUtil {
public static byte[] Serialize(Object object) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
return byteArrayOutputStream.toByteArray();
}
public static Object Deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
return objectInputStream.readObject();
}
}
View Code