前言
在開始介紹socket前先補充補充基礎知識,在此基礎上理解網絡通信才會順理成章,當然有基礎的可以跳過去了。都是廢話,進入正題。
tcp/ip:transmission control protocol/internet protocol,傳輸控制協議/因特網互聯協議,又名網絡通訊協議。簡單來說:tcp控制傳輸數據,負責發現傳輸的問題,一旦有問題就發出信號,要求重新傳輸,直到所有數據安全正確地傳輸到目的地,而ip是負責給因特網中的每一臺電腦定義一個地址,以便傳輸。從協議分層模型方面來講:tcp/ip由:網絡接口層(鏈路層)、網絡層、傳輸層、應用層。它和osi的七層結構以及對于協議族不同,下圖簡單表示:
注:第一張圖:tcp/ip的四層結構對應osi七層結構。
第三張圖:tcp/ip協議族在osi七層中的位置及對應的功能。
第二張圖:tcp/ip協議模塊關系圖。
現階段socket通信使用tcp、udp協議,相對應udp來說,tcp則是比較安全穩定的協議了。
socket是一種通信tcp/ip的通訊接口,也就是http的抽象層,就是socket在http之上,socket也就是發動機。實際上,傳輸層的tcp是基于網絡層的ip協議的,而應用層的http協議又是基于傳輸層的tcp協議的,而socket本身不算是協議,就像上面所說,它只是提供了一個針對tcp或者udp編程的接口。
在c#中可以非常方便的使用socket進行數據傳輸。
socket對象是c#使用它的重要對象在socket的構造函數中,我們可以設置它的地址,socket的類,支持的協議等等,其定義如下:
1
|
public socket(addressfamily addressfamily, sockettype sockettype, protocoltype protocoltype); |
我們想要使用socket,那么就必須創建socket的對象,創建這個對象,就必須需要ipendpoint對象來綁定到套接詞字中,有如下定義
1
2
3
4
5
6
|
// 創建負責監聽的套接字,注意其中的參數; socketwatch = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); // 獲得文本框中的ip對象; ipaddress address = ipaddress.parse(textbox1.text.trim()); // 創建包含ip和端口號的網絡節點對象; ipendpoint endpoint = new ipendpoint(address, int .parse(textbox2.text.trim())); |
然后再通過socket的bind來進行綁定。
1
|
socketwatch.bind(endpoint); |
因為我們時刻會被內網中的其他ip和端口進行連接,那么我們就需要創建線程來進行觀察,有如下定義
1
2
3
4
5
6
7
|
// 設置監聽隊列的長度; socketwatch.listen(10); // 創建負責監聽的線程; threadwatch = new thread(watchconnecting); threadwatch.isbackground = true ; threadwatch.start(); showmsg( "服務器啟動監聽成功!" ); |
其檢測方法如下,其中就是不斷的去檢測客戶端的連接請求,通過accept()方法可以獲取一個套接字,然后通過socket對象的remoteendpoint()可以獲取一個ip。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void watchconnecting() { while ( true ) // 持續不斷的監聽客戶端的連接請求; { // 開始監聽客戶端連接請求,accept方法會阻斷當前的線程; socket sokconnection = socketwatch.accept(); // 一旦監聽到一個客戶端的請求,就返回一個與該客戶端通信的 套接字; // 想列表控件中添加客戶端的ip信息; online.items.add(sokconnection.remoteendpoint.tostring()); // 將與客戶端連接的 套接字 對象添加到集合中; dict.add(sokconnection.remoteendpoint.tostring(), sokconnection); showmsg( "客戶端連接成功!" ); thread thr = new thread(recmsg); thr.isbackground = true ; thr.start(sokconnection); dictthread.add(sokconnection.remoteendpoint.tostring(), thr); // 將新建的線程 添加 到線程的集合中去。 } } |
最后我們開啟一個線程去執行recmsg方法,然后我們不停的去監聽客戶端給我們的數據發送。
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
46
47
48
49
50
51
52
53
54
55
56
57
58
|
void recmsg( object sokconnectionparn) { socket sokclient = sokconnectionparn as socket; while ( true ) { // 定義一個2m的緩存區; byte [] arrmsgrec = new byte [1024 * 1024 * 2]; // 將接受到的數據存入到輸入 arrmsgrec中; int length = -1; try { length = sokclient.receive(arrmsgrec); // 接收數據,并返回數據的長度; } catch (socketexception se) { showmsg( "異常:" + se.message); // 從 通信套接字 集合中刪除被中斷連接的通信套接字; dict.remove(sokclient.remoteendpoint.tostring()); // 從通信線程集合中刪除被中斷連接的通信線程對象; dictthread.remove(sokclient.remoteendpoint.tostring()); // 從列表中移除被中斷的連接ip online.items.remove(sokclient.remoteendpoint.tostring()); break ; } catch (exception e) { showmsg( "異常:" + e.message); // 從 通信套接字 集合中刪除被中斷連接的通信套接字; dict.remove(sokclient.remoteendpoint.tostring()); // 從通信線程集合中刪除被中斷連接的通信線程對象; dictthread.remove(sokclient.remoteendpoint.tostring()); // 從列表中移除被中斷的連接ip online.items.remove(sokclient.remoteendpoint.tostring()); break ; } if (arrmsgrec[0] == 0) // 表示接收到的是數據; { string strmsg = system.text.encoding.utf8.getstring(arrmsgrec, 1, length - 1); // 將接受到的字節數據轉化成字符串; showmsg(strmsg); } if (arrmsgrec[0] == 1) // 表示接收到的是文件; { savefiledialog sfd = new savefiledialog(); if (sfd.showdialog( this ) == system.windows.forms.dialogresult.ok) { // 在上邊的 sfd.showdialog() 的括號里邊一定要加上 this 否則就不會彈出 另存為 的對話框,而彈出的是本類的其他窗口,,這個一定要注意!!!【解釋:加了this的sfd.showdialog(this),“另存為”窗口的指針才能被savefiledialog的對象調用,若不加thissavefiledialog 的對象調用的是本類的其他窗口了,當然不彈出“另存為”窗口。】 string filesavepath = sfd.filename; // 獲得文件保存的路徑; // 創建文件流,然后根據路徑創建文件; using (filestream fs = new filestream(filesavepath, filemode.create)) { fs.write(arrmsgrec, 1, length - 1); showmsg( "文件保存成功:" + filesavepath); } } } } } |
我們在方法中獲得了一個object類型的對象,將這個object對象轉換成了socket,然后我們通過socket的方法receive()方法接收返回的數據,其中里面有它的屬性,可以獲取ip還有一些數據等等。服務器向客戶端發送數據也是非常簡單。通過send方法就可以了,如以下定義:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
string strmsg = "服務器" + "\r\n" + " -->" + richtextbox1.text.trim() + "\r\n" ; byte [] arrmsg = system.text.encoding.utf8.getbytes(strmsg); // 將要發送的字符串轉換成utf-8字節數組; byte [] arrsendmsg = new byte [arrmsg.length + 1]; arrsendmsg[0] = 0; // 表示發送的是消息數據 buffer.blockcopy(arrmsg, 0, arrsendmsg, 1, arrmsg.length); string strkey = "" ; strkey = online.text.trim(); if ( string .isnullorempty(strkey)) // 判斷是不是選擇了發送的對象; { messagebox.show( "請選擇你要發送的好友!!!" ); } else { dict[strkey].send(arrsendmsg); // 解決了 sokconnection是局部變量,不能再本函數中引用的問題; showmsg(strmsg); richtextbox1.clear(); } |
最后需要注意的是,如果你的文件較大,有的時候這個緩沖區達不到你的文件字節那么大,那么就會截斷,所以與的時候,先將文件轉換為byte是正確的做法。只要在客戶端進行逆轉就可以了!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/ZaraNet/p/10361158.html