JAVA實現的一個簡單的Socket Server範例

隨手做的一個簡單的Socket服務器,用來模擬全向移動平臺機上控制器的響應的,配合全向移動平臺手機控制器調試用的。

在主線程裏死循環監聽端口12000,有客戶端連接就新建一個子線程,在子線程裏接受客戶端數據(byte[10],每個字節分別定義了運動狀態、運動速度等信息),進行解析,在控制檯顯示當前速度或狀態,回傳數據。客戶端退出則關閉子線程,返回主線程繼續等待連接。

命令行運行 java -jar  程序名.jar

完整源碼

http://download.csdn.net/detail/kagami1983/8807765


主線程代碼:

	public SocketServerForMecanum() {
		try {
			serverSocket = new ServerSocket(12000);
		} catch (IOException e) {			
			//e.printStackTrace();
			System.out.println("Create ServerSocket Error...");
		}
	}

	@Override
	public void run() {
		while (true) {
			System.out.println("Listenning...");
			try {
				// 每個請求交給一個線程去處理
				socket = serverSocket.accept();
				ServerThread serverThread = new ServerThread(socket);
				serverThread.start();
				sleep(1000);
			} catch (Exception e) {
				//e.printStackTrace();
				System.out.println("Create Socket Error...");
			}

		}

	}
子線程代碼:

	class ServerThread extends Thread {
		private Socket socket;
		private InputStream ins;
		private OutputStream ous;
		private byte[] buffer_received = new byte[10];
		private short moveX = 0, moveY = 0, rotate = 0;
		private int recount = 0;

		public ServerThread(Socket socket) {
			// super();
			this.socket = socket;
			try {
				ins = socket.getInputStream();
				ous = socket.getOutputStream();
			} catch (IOException e) {				
				//e.printStackTrace();
				System.out.println("Create ServerThread Error...");
			}

		}

		@Override
		public void run() {

			while (true) {
				try {
					ins.read(buffer_received);

					if (recount >= 5) {
						recount = 0;
						moveX = 0;
						moveX = (short) (buffer_received[5] & 0x00ff);
						moveX <<= 8;
						moveX |= (buffer_received[4] & 0x00ff);
						moveY = 0;
						moveY = (short) (buffer_received[7] & 0x00ff);
						moveY <<= 8;
						moveY |= (buffer_received[6] & 0x00ff);
						rotate = 0;
						rotate = (short) (buffer_received[9] & 0x00ff);
						rotate <<= 8;
						rotate |= (buffer_received[8] & 0x00ff);
						System.out.println("moveX=" + moveX + ";" + "moveY="
								+ moveY + ";" + "rotate=" + rotate + ";");

					} else {
						recount++;
					}

					/*//buffer的頭字節爲2,則認爲收到了客戶端主動請求關閉連接
					if (buffer_received[0] == 2) {
						ins.close();
						ous.close();
						socket.close();
						return;
					}*/
					
					//模擬上電
					if (buffer_received[2] == 1) {
						System.out.println("Power ON...");
					}
					
					//模擬下電
					if (buffer_received[2] == 2) {
						System.out.println("Power OFF...");
					}
					
					//模擬停止
					if (buffer_received[2] == 4) {
						System.out.println("Stop Button Pushed!...");
					}
					
					//將首字節置1然後回傳
					buffer_received[0] = 1;
					ous.write(buffer_received);

				} catch (IOException e) {					
					//e.printStackTrace();
					System.out.println("Client Disconnected...");
					//當客戶端因各種原因關閉連接而且沒有通知服務端的,包括屏幕翻轉、打開首選項等情況
					try {
						ins.close();
						ous.close();
						socket.close();
					} catch (IOException e1) {						
						e1.printStackTrace();
					}
					return;
				}

			}

		}

	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章