TCP服務器和客戶端_Socket編程
阿新 • • 發佈:2018-08-07
cli 操作 cat next() png run 等待 響應 .net
-
TCP服務器
- 服務器啟動文件
1 import java.io.IOException; 2 import java.net.ServerSocket; 3 import java.net.Socket; 4 5 public class MyServer { 6 7 public static void main(String[] args) { 8 9 ServerSocket serverSocket = null; 10 Socket socket = null; 11 12try { 13 14 //創建服務器套接字ServerSocket,並綁定服務器端口號,port:1-65536,>1024 15 serverSocket =new ServerSocket(9999); 16 17 while(true) {//可以響應多個各戶端 18 System.out.println("服務器開始運行...."); 19 //阻塞等待客戶端 20 socket = serverSocket.accept();21 22 //開啟線程響應客戶端,並將通信套接字socket轉入子線程 23 new MyServerThread(socket).start(); 24 } 25 26 } catch (IOException e) { 27 28 e.printStackTrace(); 29 30 }finally { 31 //完成或者異常時,關閉套接字32 try { 33 34 socket.close(); 35 serverSocket.close(); 36 37 } catch (IOException e) { 38 39 e.printStackTrace(); 40 41 } 42 43 } 44 45 } 46 }
- 服務器線程文件
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.OutputStream; 4 import java.net.Socket; 5 6 public class MyServerThread extends Thread{ 7 8 private Socket socket = null; 9 private InputStream in; 10 private OutputStream out; 11 12 public MyServerThread(Socket socket) { 13 14 this.socket = socket; 15 16 //客戶端IP 17 String ip = socket.getInetAddress().getHostAddress(); 18 int port = socket.getPort(); 19 System.out.println("IP:" +ip +"port:"+port); 20 } 21 22 //發送和接收數據 23 @Override 24 public void run() { //響應客戶端(數據收發) 25 26 try { 27 //獲取輸入輸出流 28 in = socket.getInputStream(); 29 out = socket.getOutputStream(); 30 31 while(true) {//多次 32 //登陸協議 loginnamehellopass123456end 33 34 byte []b = new byte[32];//用於存放read到的數據流 35 in.read(b);//讀取數據並存放到b數組中 36 37 String data = new String(b);//將字節流轉為字符串 38 39 //讀取數據解析登陸信息 40 String logo = data.substring(0,5);//login標誌 41 String name = data.substring(data.indexOf("name")+4,data.indexOf("pass"));//data.indexOf("name")返回n的下標 42 String pass = data.substring(data.indexOf("pass")+4,data.indexOf("end"));//data.indexOf("name")返回n的下標 43 44 String msg =""; 45 if (logo.equals("login")) {//登陸? 46 if (name.equals("hello")) { 47 if (pass.equals("123456")) { 48 msg += "登錄成功"; 49 } 50 else { 51 msg += "密碼錯誤"; 52 } 53 } 54 else { 55 msg += "用戶不存在"; 56 } 57 } 58 else { 59 msg += "輸入錯誤"; 60 } 61 62 out.write(msg.getBytes());//將字符串轉為字節數組發送到客戶端 63 out.flush(); 64 65 } 66 } catch (IOException e) { 67 68 e.printStackTrace(); 69 70 }finally { 71 72 //g關閉流和套接字 73 try { 74 75 in.close(); 76 out.close(); 77 socket.close(); 78 79 } catch (IOException e) { 80 81 e.printStackTrace(); 82 83 } 84 } 85 } 86 }
-
TCP客戶端
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.OutputStream; 4 import java.net.Socket; 5 import java.util.Scanner; 6 7 public class MyClient { 8 9 public static void main(String[] args) { 10 11 Socket socket = null; 12 InputStream in = null; 13 OutputStream out = null; 14 15 try { 16 //1.創建通信套接字,並綁定IP和PORT 17 socket = new Socket("127.0.0.1", 9999);//127.0.0.1 B/C都在 18 19 //2.獲取輸入輸出流 20 in = socket.getInputStream(); 21 out = socket.getOutputStream(); 22 23 Scanner input = new Scanner(System.in);//從控制臺獲取數據 24 25 while(true) {//循環發送和接收消息 26 27 String msg = input.next(); 28 29 //fa 30 out.write(msg.getBytes()); 31 out.flush(); 32 33 //收 34 byte []b = new byte[32]; 35 in.read(b); 36 String data = new String(b); 37 System.out.println(data.trim());//裝不滿時去空隔 38 39 } 40 41 42 } catch (IOException e) { 43 e.printStackTrace(); 44 }finally { 45 try { 46 out.close(); 47 in.close(); 48 socket.close(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 53 } 54 55 56 } 57 }
-
運行效果
- 運行服務端
- 運行客戶端後服務器獲得信息
- 客戶端操作以及對應的服務端響應結果
TCP服務器和客戶端_Socket編程