Java网络编程 TCP编程 发送接收对象 对象序列化反序列化
对象序列化
前提条件:必须实现java.io.Serializable
接口.
1 2 3 4 5 6 7 8 9
|
outputToServer = new ObjectOutputStream(client.getOutputStream());
outputToServer.writeObject(args);
outputToServer.flush();
|
对象反序列化
1 2 3 4
| inputByServer = new ObjectInputStream(client.getInputStream());
String[] inputArg = (String[]) inputByServer.readObject();
|
实例
通信流程
客户端和服务端通信流程如下
客户端会把接收到的命令行参数(String数组)发送给服务端,
服务端接收该字符串数组,然后、打印到服务端的控制台上,并把这个字符串数组原样发送给客户端,
然后客户端接收服务端发送过来的字符串数组,转换之后打印到控制台上.
服务端主线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package tcp.arg; import java.net.ServerSocket; import java.net.Socket; public class EchoThreadServerByArgs { private static boolean isServerAlive = true; private static int clientNum = 0; public static void main(String args[]) throws Exception { ServerSocket server = null; Socket client = null; server = new ServerSocket(6666); while (isServerAlive) { System.out.println("等待客户端连接..."); client = server.accept(); System.out.println(" 客户端连接成功,当前客户端数量:" + clientNum); if (isServerAlive) { new Thread(new EchoThreadByArgs(client)).start(); } } server.close(); client.close(); System.out.println("服务端已经停止..."); } public static void addClientNum() { clientNum = clientNum + 1; } public static void minusClientNum() { clientNum = clientNum - 1; } public static int getClientNum() { return clientNum; } public static void shutdownServer() { isServerAlive = false; } public static boolean isServerAlive() { return isServerAlive; } }
|
服务端响应线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package tcp.arg; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import tcp.EchoThreadServer; public class EchoThreadByArgs implements Runnable { private Socket client = null; int clienId; public EchoThreadByArgs(Socket client) { this.client = client; this.clienId = EchoThreadServer.getClientNum(); EchoThreadServer.addClientNum(); } public void run() { ObjectInputStream inoutFromClient = null; ObjectOutputStream outputToClient = null; try { inoutFromClient = new ObjectInputStream(client.getInputStream()); String[] inputArg = (String[]) inoutFromClient.readObject(); for (String string : inputArg) { System.out.println(string); } outputToClient = new ObjectOutputStream(client.getOutputStream()); outputToClient.writeObject(inputArg); outputToClient.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (inoutFromClient != null) { try { inoutFromClient.close(); } catch (IOException e) { } } if (outputToClient != null) { try { outputToClient.close(); } catch (IOException e) { } } } } }
|
客户端线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package tcp.arg; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; public class EchoClientByArgs { public static void main(String[] args) { Socket client = null; try { client = new Socket("localhost", 6666); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ObjectOutputStream outputToServer = null; ObjectInputStream inputByServer = null; try { outputToServer = new ObjectOutputStream(client.getOutputStream()); outputToServer.writeObject(args); outputToServer.flush(); inputByServer = new ObjectInputStream(client.getInputStream()); String[] inputArg = (String[]) inputByServer.readObject(); for (String string : inputArg) { System.out.println(string); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { if (outputToServer != null) { try { outputToServer.close(); } catch (IOException e) { } } if (inputByServer != null) { try { inputByServer.close(); } catch (IOException e) { } } } } }
|
运行效果