本次主要就是寫一寫服務端與客服端相互發一些簡單資訊的**。增加心跳包,傳送複雜檔案等之後慢慢擴充套件吧。
效果展示:
首先建立winform專案,建立兩個cs
伺服器**
public
partial
class server : form
///
/// 開啟監聽
///
///
///
private
void
bnt_listen_click
(object sender,
eventargs e)
socket socket_listen;
//將所有的連線socket儲存,以便伺服器給指定的客戶端發訊息
dictionary<
string
, socket> socketall =
newdictionary
<
string
,socket
>()
;///
/// 監聽客戶端的連線請求
///
///
void
listen
(object o)
}///
/// 接受訊息
///
///
void
receivemsg
(object o)
}catch
}void
showmessage
(string s)
private
void
form1_load
(object sender,
eventargs e)
///
/// 服務端傳送訊息
///
///
///
private
void
bnt_send_click
(object sender,
eventargs e)
else
}///
/// 給所有的客戶端通知
///
///
void
sendall
(string s)
byte
buffer = encoding.utf8.
getbytes
(s);
item.value.
send
(buffer)
;string str = item.value.remoteendpoint +
":"+ s;
showmessage
(str);}
}}
客戶端**
public
partial
class client : form
socket socketsend;
///
/// 與伺服器建立連線
///
///
///
private
void
connect_click
(object sender,
eventargs e)
void
showmessage
(string s)
private
void
form1_load
(object sender,
eventargs e)
///
/// 接受伺服器的資訊
///
///
void
listen
(object o)
}catch
}///
/// 傳送訊息
///
///
///
private
void
send_click
(object sender,
eventargs e)
}
初學c#,歡迎指出不足之處 Socket 連線簡易程式設計
客戶端 include include using namespace std pragma comment lib,ws2 32.lib define port 4000 define ip address 127.0.0.1 int main int agrc,char agrv create ...
C 的Socket程式設計(1)
開始學tcp ip才發現以前c和c 學的有多膚淺 接下來就準備返璞歸真啦 哈哈 轉戰c ing.1.了解基本socket函式 socket s socket family,type,protocal int nret connect s,sockaddr sin,sizeof s sockaddr ...
Socket程式設計 簡易測試socket TCP程式
簡易的利用socket 實現tcp連線的程式,旨在簡化程式結構,給初學者乙個啟示的作用.伺服器端程式 include stdafx.h include include include using namespace std pragma comment lib,ws2 32.lib 新增ws2 32...