1. 注意
  2. 代码

用Java来写一个简单的服务器(server),对客户端(client)的request进行回应。

这个sample主要使用socket来进行演示,分别可以接收与发送string和object。

注意

  • 接收与发送object时应使用同一个package的类,且要implements java.io.Serializable

代码

getString()getObject()分别是接收string和object,并对其进行回应的方法。

  • Main.java

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;

    public class Main implements Runnable {
    public static final int PORT = 12345;
    private ServerSocket serverSocket;

    public void getString(Socket socket) {
    try {
    // receive message
    BufferedReader in = new BufferedReader(new InputStreamReader(
    socket.getInputStream()));
    String request = in.readLine();
    System.out.println("S: Received: " + request);

    // send message
    PrintWriter out = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(socket.getOutputStream())), true);
    String response = "S: Received: " + request;
    out.println(response);
    out.flush();

    in.close();
    out.close();
    } catch (Exception e) {
    System.out.println("S: Error");
    e.printStackTrace();
    }
    }

    public void getObject(Socket socket) {
    try {
    // receive object
    ObjectInputStream in = new ObjectInputStream(
    socket.getInputStream());
    Object obj = in.readObject();
    ArrayList<Point> points = (ArrayList<Point>) obj;
    System.out.println("S: Received:");
    String request = "";
    for (Point point : points) {
    request += point.toString() + "\n";
    }
    System.out.println(request);

    // send object
    ObjectOutputStream out = new ObjectOutputStream(
    socket.getOutputStream());
    out.writeObject(points);
    out.flush();

    in.close();
    out.close();
    } catch (Exception e) {
    System.out.println("S: Error");
    e.printStackTrace();
    }
    }

    public void run() {
    try {
    System.out.println("S: Connecting...");
    serverSocket = new ServerSocket(PORT);
    while (true) {
    Socket socket = serverSocket.accept();
    System.out.println("S: Receiving...");
    try {
    getObject(socket);
    } catch (Exception e) {
    System.out.println("S: Error");
    e.printStackTrace();
    } finally {
    if (socket != null) {
    socket.close();
    }
    System.out.println("S: Done.");
    }
    }
    } catch (Exception e) {
    System.out.println("S: Error");
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    Thread thread = new Thread(new Main());
    thread.start();
    }
    }

  • Point.java

    1
    2
    3
    4
    5
    6
    7
    8
    public class Point implements java.io.Serializable {
    public double x, y;

    public Point(double x, double y) {
    this.x = x;
    this.y = y;
    }
    }