JAVA UDP Echo Server/Client Example Code
說實話,我不會寫 JAVA。我個人的程式語言母語是 C++(感謝廖婉君教授),工作上比較常用的是 C,JAVA 還真的沒碰過。雖然我一直跟別人說,程式語言這東西,只要掌握住一種,要跨入其他語言並不困難,因為已經有最起碼的程式邏輯(迴圈、判斷式等),但其實我也很少去用其他的程式語言。最近因為工作的關係,不得不使用 JAVA,所以就紀錄一些 JAVA 的範例程式在這裡,以便未來使用。
在這裡要紀錄的是 UDP 的 Echo Server 和 Client 的程式。資料來源為 http://www.cs.uic.edu/~troy/spring05/cs450/sockets/socket.html。
UDP Echo Server
UDP Echo Client
因為我跟 JAVA 不熟,順便紀錄一下編譯的指令。
javac javaUdpServer.java
javac javaUdpClient.java
java UdpServer 9876
java UdpClient 127.0.0.1 9876
在這裡要紀錄的是 UDP 的 Echo Server 和 Client 的程式。資料來源為 http://www.cs.uic.edu/~troy/spring05/cs450/sockets/socket.html。
UDP Echo Server
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { int serverPort = 9876; try { if ( args.length > 0 ) { serverPort = new Integer( args[0] ).intValue(); } DatagramSocket serverSocket = new DatagramSocket( serverPort ); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { receiveData = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket( receiveData, receiveData.length ); System.out.println ("Waiting for datagram packet"); serverSocket.receive( receivePacket ); String sentence = new String( receivePacket.getData() ); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); System.out.println ("From: " + IPAddress + ":" + port); System.out.println ("Message: " + sentence); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } catch (SocketException ex) { System.out.println("UDP Port " + serverPort + " is occupied."); System.exit(1); } } }
UDP Echo Client
import java.io.*; import java.net.*; import java.lang.Integer; class UDPClient { public static void main(String args[]) throws Exception { try { String serverHostname = new String ("127.0.0.1"); int serverPort = 9876; if ( args.length > 0 ) { serverHostname = args[0]; } if ( args.length > 1 ) { serverPort = new Integer( args[1] ).intValue(); } BufferedReader inFromUser = new BufferedReader( new InputStreamReader( System.in ) ); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(serverHostname); System.out.println ("Attemping to connect to " + IPAddress + ") via UDP port " + serverPort ); while( true ) { byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; System.out.print("Enter Message (q for exit): "); String sentence = inFromUser.readLine(); if( sentence.equals( "q" ) ) { System.out.print("Exit! "); break; } sendData = sentence.getBytes(); System.out.println ("Sending data to " + sendData.length + " bytes to server."); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, serverPort ); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); System.out.println ("Waiting for return packet"); clientSocket.setSoTimeout(10000); try { clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); InetAddress returnIPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); System.out.println ("From server at: " + returnIPAddress + ":" + port); System.out.println ("Message: " + modifiedSentence); } catch ( SocketTimeoutException ste ) { System.out.println ("Timeout Occurred: Packet assumed lost"); } } clientSocket.close(); } catch (UnknownHostException ex) { System.err.println(ex); } catch (IOException ex) { System.err.println(ex); } } }
因為我跟 JAVA 不熟,順便紀錄一下編譯的指令。
javac javaUdpServer.java
javac javaUdpClient.java
java UdpServer 9876
java UdpClient 127.0.0.1 9876
留言
張貼留言