JavaSE - Network Programming Exercises

Posted by lc on Mon, 30 Sep 2019 23:25:41 +0200

1. Write a server-side program to read a set of integers sent by the client, which is represented as a string separated by spaces between a set of numbers. After sorting these integers, the corresponding string is returned to the client. If the data format is incorrect, the error message is returned, and the local server is used as the server. [must do]

1. Define a set of methods for functional design: parameters and return values of design methods according to functional requirements
2. Attribute analysis of classes, design class attributes: Attention to data type selection of attributes (number values, characters, time, class)

package classroom.chap14;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
public class SortServer {
	private int port = 8888;
	public SortServer() {
	}
	public SortServer(int port) {
		this.port = port;
	}
	/**
	 * 1.Accept numeric strings from clients 2. Resolve strings 3. Sort data 4. Return sorting results
	 */
	public void work() {
		ServerSocket serverSocket = null;
		Socket socket = null;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			// 1. Start server listening
			serverSocket = new ServerSocket(port);
			// 2. Waiting for Client Connection
			socket = serverSocket.accept();
			// 3. Open the I/O stream
			inputStream = socket.getInputStream();
			outputStream = socket.getOutputStream();
			// 4. Read the data from the client
			byte[] buffer = new byte[512];
			//boolean chating = true;
			while (true) {
				int len = inputStream.read(buffer);
				// 5. Data Processing
				String string = new String(buffer, 0, len);
				String[] data = string.trim().split(" ");
				try {
					int[] intData = transferAndSort(data);
					string = intArrayToString(intData, " ");
					System.out.print("Presorted data:");
					for (String a : data) {
						System.out.print(a + " ");
					}
					System.out.println();
					System.out.println("Sorted data:" + string);
				} catch (Exception e) {
					string = e.getMessage();
					string = "data format error!";
					System.out.println(string);
				}
				// 6. Send the data back to the client
				outputStream.write(string.getBytes());
			}
			// 7. Turn off streams and networks
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			closeAll(outputStream, inputStream, serverSocket, socket);
		}
	}
	//Converting strings to integers
	private int[] transferAndSort(String[] data) {
		int[] intData = new int[data.length];
		for (int i = 0; i < data.length; i++) {
			intData[i] = Integer.parseInt(data[i]);
		}
		Arrays.sort(intData);
		return intData;

	}

	private String intArrayToString(int[] data, String flag) {
		StringBuffer buffer = new StringBuffer();
		for (int d : data) {
			buffer.append(d).append(flag);
		}
		return buffer.toString();
	}

	// Closing Stream and Network Method
	public void closeAll(OutputStream outputStream, InputStream inputStream,
			ServerSocket serverSocket, Socket socket) {
		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (serverSocket != null) {
			try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new SortServer().work();
	}
}

Topics: socket Java Attribute network