本文主要介紹了如何實(shí)現(xiàn)socket網(wǎng)絡(luò)編程的多線程,分享給大家
TCP 、UDP、IP地址的基本介紹:
- TCP
是可靠的連接。這個(gè)可靠的意思就是得有明確的連接對(duì)象才行,就像是打電話,撥打的號(hào)碼必須得在服務(wù)中有人接,這個(gè)網(wǎng)絡(luò)會(huì)話才算是建立了。
- UDP:
不可靠的連接。不可靠的意思就是不太確定這個(gè)會(huì)話最后是不是真的送達(dá)你要連接的對(duì)象那里去了,就像是寄快遞,把快遞地址填上了,但是說不好半路會(huì)出點(diǎn)啥幺蛾子,能不能安全送達(dá)不一定。
- IP地址:
就是計(jì)算機(jī)的身份證。身份證號(hào)前六位數(shù)字是地址碼(可以知道是哪個(gè)省哪個(gè)城市哪個(gè)縣城),接著八位數(shù)字是出生日期碼,IP地址也是這樣的,它是由網(wǎng)絡(luò)地址(確定是哪個(gè)網(wǎng)絡(luò))和主機(jī)地址(網(wǎng)絡(luò)中的哪個(gè)主機(jī))組成的。
本機(jī)地址: localhost等價(jià)于 127.0.0.1
下面的例子是基于TCP協(xié)議進(jìn)行的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public class Client { public static void main(String[] args) throws Exception{ //打開一個(gè)套接字,準(zhǔn)備發(fā)送請求 Socket socket = new Socket( "localhost" , 9996); //得先建立連接,就是所謂的TCP是建立可靠的連接 System.out.println( "尊敬的VIP客戶,請求數(shù)據(jù)小分隊(duì)已準(zhǔn)備好,請輸入您的請求:" ); //向服務(wù)端發(fā)送帶著客戶端請求的數(shù)據(jù),這個(gè)請求數(shù)據(jù)是從鍵盤讀入的,發(fā)送給服務(wù)端是以PrintWriter的形式包裝 PrintWriter out = new PrintWriter(socket.getOutputStream(), true ); //輸入流getInputStream(),單獨(dú)開一個(gè)線程在Receive類中完成接收服務(wù)器發(fā)來的數(shù)據(jù) Thread t = new Thread( new Receive(socket)); t.start(); Scanner scanner = new Scanner(System.in); //鍵盤輸入 while (scanner.hasNextLine()){ out.println(scanner.nextLine()); //將從鍵盤輸入的數(shù)據(jù)發(fā)送出去 } } } public class Receive implements Runnable { private Socket socket; public Receive(Socket socket) { this .socket=socket; } @Override public void run() { try { Scanner scanner = new Scanner(socket.getInputStream()); //接收數(shù)據(jù) String str =null; while ( true ){ str=scanner.nextLine(); System.out.println( "服務(wù)器說:" +str); //打印接收的數(shù)據(jù) } } catch (IOException e) { e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class Server { public static void main(String[] args) throws Exception{ ServerSocket server = new ServerSocket(9996); //在端口9996開一個(gè)服務(wù),監(jiān)聽客戶端發(fā)來的請求,準(zhǔn)備接收客戶端發(fā)來的請求數(shù)據(jù) System.out.println( "服務(wù)端準(zhǔn)備完畢,隨時(shí)待命接收請求!" ); Socket socket =server.accept(); //只有當(dāng)有客戶端請求并連接時(shí),才回返回一個(gè)Socket對(duì)象,這個(gè)對(duì)象就是帶著客戶端請求的那個(gè)Socket對(duì)象 //創(chuàng)建一個(gè)PrintWriter的實(shí)例對(duì)象out來完成服務(wù)端向客戶端的輸出數(shù)據(jù)的任務(wù), PrintWriter out = new PrintWriter(socket.getOutputStream(), true ); //單獨(dú)開一個(gè)線程接收從客戶端過來的請求,在Receive1類中完成數(shù)據(jù)的接收 Thread t = new Thread( new Receive1(socket)); t.start(); /*一定要理解Socket是一個(gè)用于機(jī)器之間通信的類*/ //發(fā)送數(shù)據(jù)給客戶端 Scanner scanner = new Scanner(System.in); //鍵盤輸入服務(wù)端要發(fā)給客戶端的數(shù)據(jù) while (scanner.hasNextLine()){ out.println(scanner.nextLine()); //將鍵盤輸入的服務(wù)端要發(fā)給客戶端的數(shù)據(jù)封裝在PrintWriter類的對(duì)象中 } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Receive1 implements Runnable { private Socket socket; public Receive1(Socket socket) { this .socket=socket; //在服務(wù)端接收客戶端發(fā)來的數(shù)據(jù)需要調(diào)用Receive1()這個(gè)方法,就會(huì)得到一個(gè)Socket對(duì)象,這個(gè)對(duì)象就是攜帶著客戶端請求的socket } @Override public void run() { try { Scanner scanner= new Scanner(socket.getInputStream()); //獲得客戶端從鍵盤接收的輸入流 String str = null; while ( true ){ str=scanner.nextLine(); System.out.println( "客戶端發(fā)來的數(shù)據(jù):" +str); } } catch (IOException e) { e.printStackTrace(); } //接收數(shù)據(jù) } } |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家
原文鏈接:https://blog.csdn.net/u012369153/article/details/52876841