package socket; import java.net.*; import java.io.*; public class Datareceive { public static void main(String args[]) { String clientOrder; byte[] inputBytes = null; DataInputStream dataInputStream = null; try { ServerSocket ss = new ServerSocket(8000); System.out.println("正在監聽8000端口"); Socket s = ss.accept(); dataInputStream = new DataInputStream(s.getInputStream()); inputBytes = new byte[7]; dataInputStream.read(inputBytes); clientOrder = new String(inputBytes, 0, 7); System.out.println("客戶端命令為:" + clientOrder); } catch (IOException e) { e.printStackTrace(); } } }
package socket; import java.net.*; import java.io.*; public class Datasend { public static void main(String args[]) { DataOutputStream dataOutputStream = null; String order; byte[] outBytes; try { Socket s = new Socket("127.0.0.1", 8000); // 應該寫服務器的地址,由于這是本機測試所以用回環地址 order = "MAKEDIR"; outBytes = order.getBytes(); dataOutputStream = new DataOutputStream(s.getOutputStream()); dataOutputStream.write(outBytes); } catch (IOException e) { e.printStackTrace(); } } }