進程由線程組成,多線程能夠實現多個任務的同時運行,是網絡編程的必要一環java
普通線程編程
1 class Mythread implements Runnable{ 2 private String name; 3 public Mythread(String nn){ 4 this.name=nn;//對象標識 5 } 6 7 @Override 8 public void run() { 9 //啓動線程後自動執行的內容 10 for(int i=0;i<100;i++) { 11 try { 12 Thread.sleep(200); 13 System.out.println(name+"===>"+i); 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 } 18 } 19 } 20 21 public class Main{ 22 public static void main(String[] args) { 23 Thread t1=new Thread(new Mythread("A"));//建立線程對象1 24 Thread t2=new Thread(new Mythread("B"));//建立線程對象2 25 t1.start();//啓動線程 26 t2.start(); 27 } 28 }
匿名線程數組
1 public class Main{ 2 public static void main(String[] args) { 3 //匿名線程----------- 4 new Thread() { 5 public void run() { 6 //啓動線程後自動執行的內容 7 for(int i=0;i<100;i++) { 8 try { 9 Thread.sleep(200); 10 System.out.println("===>"+i); 11 } catch (InterruptedException e) { 12 e.printStackTrace(); 13 } 14 } 15 } 16 }.start();//啓動 17 } 18 }
TCP客戶端
鏈接服務器,能夠實現鏈接錯誤判斷、接收錯誤判斷、服務器關閉判斷服務器
1 import java.io.InputStream; 2 import java.io.OutputStreamWriter; 3 import java.io.PrintWriter; 4 import java.net.Socket; 5 class MySocket{ 6 private Socket sock; 7 private InputStream reader; 8 private PrintWriter writer; 9 private boolean Runtobool; 10 //啓動socket客戶端 11 public MySocket() { 12 //鏈接socket 13 try{ 14 sock=new Socket("192.168.2.110",8001); 15 writer=new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));//獲取輸出流 16 reader=sock.getInputStream();//獲取輸入流//獲取輸入流 17 //寫數據,發送數據 18 writer.write("-東小東-"); 19 writer.flush(); 20 //開始讀數據 21 Runtobool=true; 22 startServ(); 23 }catch(Exception e){ 24 Runtobool=false; 25 System.out.println("open socket err"); 26 } 27 } 28 //接收數據函數 29 public void startServ(){ 30 //匿名線程 31 new Thread(){ 32 public void run(){ 33 try{ 34 //接收緩衝區 35 byte[] bytes = new byte[1024]; 36 while(Runtobool){ 37 //接收函數,此函數會阻塞 38 while((reader.read(bytes, 0, bytes.length))!=-1){ 39 String datax=new String(bytes,"gbk"); 40 System.out.println("收到數據:"+datax); 41 bytes = new byte[1024];//清空數組的內容,必須 42 } 43 //接收爲空時進入 44 System.out.println("server break err"); 45 Runtobool=false; 46 } 47 }catch(Exception e){ 48 Runtobool=false; 49 System.out.println("rev socket err"); 50 }} 51 }.start(); 52 } 53 } 54 public class Main{ 55 public static void main(String[] args) { 56 new MySocket(); 57 } 58 }
TCP服務器
實現單個客戶端接入網絡
1 import java.io.InputStream; 2 import java.io.OutputStreamWriter; 3 import java.io.PrintWriter; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 class MySocket{ 7 private ServerSocket sockser; 8 private Socket sock; 9 private InputStream reader; 10 private PrintWriter writer; 11 private boolean Runtobool; 12 //啓動socket客戶端 13 public MySocket() { 14 //鏈接socket 15 try{ 16 sockser=new ServerSocket(8001);//監聽端口8001 17 sock=sockser.accept();//阻塞等待客戶接入 18 writer=new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));//獲取輸出流 19 reader=sock.getInputStream();//獲取輸入流//獲取輸入流 20 //寫數據,發送數據 21 writer.write("-東小東-"); 22 writer.flush(); 23 //開始讀數據 24 Runtobool=true; 25 startServ(); 26 }catch(Exception e){ 27 Runtobool=false; 28 System.out.println("open socket err"); 29 } 30 } 31 //接收數據函數 32 public void startServ(){ 33 //匿名線程 34 new Thread(){ 35 public void run(){ 36 try{ 37 //接收緩衝區 38 byte[] bytes = new byte[1024]; 39 while(Runtobool){ 40 //接收函數,此函數會阻塞 41 while((reader.read(bytes, 0, bytes.length))!=-1){ 42 String datax=new String(bytes,"gbk"); 43 System.out.println(sock.getRemoteSocketAddress()+"==>收到數據:"+datax); 44 bytes = new byte[1024];//清空數組的內容,必須 45 } 46 //接收爲空時進入 47 System.out.println("client server break err"); 48 Runtobool=false; 49 } 50 }catch(Exception e){ 51 Runtobool=false; 52 System.out.println("rev socket err"); 53 }} 54 }.start(); 55 } 56 } 57 public class Main{ 58 public static void main(String[] args) { 59 new MySocket(); 60 } 61 }
TCP服務器改進版
可同時接收多個鏈接多線程
1 import java.io.InputStream; 2 import java.io.OutputStreamWriter; 3 import java.io.PrintWriter; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 class MySocket{ 7 //啓動socket客戶端 8 public MySocket() { 9 //匿名線程 10 new Thread(){ 11 public void run(){ 12 //鏈接socket 13 try{ 14 ServerSocket sockser=new ServerSocket(8001);//監聽端口8001 15 while(true) { 16 Socket sock=sockser.accept();//阻塞等待客戶接入 17 startServ(sock); 18 } 19 }catch(Exception e){ 20 System.out.println("open socket err"); 21 } 22 } 23 }.start(); 24 } 25 //接收數據函數 26 public void startServ(Socket sock){ 27 try { 28 PrintWriter writer=new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));//獲取輸出流 29 InputStream reader=sock.getInputStream();//獲取輸入流//獲取輸入流 30 //寫數據,發送數據 31 writer.write("-東小東-"); 32 writer.flush(); 33 //匿名線程 34 new Thread(){ 35 public void run(){ 36 boolean Runtobool=true; 37 try{ 38 //接收緩衝區 39 byte[] bytes = new byte[1024]; 40 while(Runtobool){ 41 //接收函數,此函數會阻塞 42 while((reader.read(bytes, 0, bytes.length))!=-1){ 43 String datax=new String(bytes,"gbk"); 44 System.out.println(sock.getRemoteSocketAddress()+"==>收到數據:"+datax); 45 bytes = new byte[1024];//清空數組的內容,必須 46 } 47 //接收爲空時進入 48 System.out.println("client server break err"); 49 Runtobool=false; 50 } 51 }catch(Exception e){ 52 Runtobool=false; 53 System.out.println("rev socket err"); 54 }} 55 }.start(); 56 57 }catch(Exception e) { 58 System.out.println("異常-----"); 59 } 60 } 61 } 62 public class Main{ 63 public static void main(String[] args) { 64 new MySocket(); 65 } 66 }