c#的網(wǎng)絡(luò)編程比較簡單,有相關(guān)的類來實現(xiàn)相關(guān)的功能。
本小程序是<<c#網(wǎng)絡(luò)編程>>里面的。


基本的類介紹
tcpClient
TcpClient 類提供了一些簡單的方法,用于在同步阻止模式下通過網(wǎng)絡(luò)來連接、發(fā)送和接收流數(shù)據(jù)。
為使 TcpClient 連接并交換數(shù)據(jù),使用 TCP ProtocolType 創(chuàng)建的 TcpListener 或 Socket 必須偵聽是否有傳入的連接請求。 可以使用下面兩種方法之一連接到該偵聽器:
創(chuàng)建一個 TcpClient,并調(diào)用三個可用的 Connect 方法之一。
使用遠程主機的主機名和端口號創(chuàng)建 TcpClient。 此構(gòu)造函數(shù)將自動嘗試一個連接。
tcpListener
TcpListener 類提供一些簡單方法,用于在阻止同步模式下偵聽和接受傳入連接請求。 可使用 TcpClient 或 Socket 來連接 TcpListener。 可使用 IPEndPoint、本地 IP 地址及端口號或者僅使用端口號,來創(chuàng)建 TcpListener。 可以將本地 IP 地址指定為 Any,將本地端口號指定為 0(如果希望基礎(chǔ)服務(wù)提供程序為您分配這些值)。 如果您選擇這樣做,可在連接套接字后使用 LocalEndpoint 屬性來標識已指定的信息。
使用 Start 方法,可開始偵聽傳入的連接請求。 Start 將對傳入連接進行排隊,直至您調(diào)用 Stop 方法或它已經(jīng)完成 MaxConnections 排隊為止。 可使用 AcceptSocket 或 AcceptTcpClient 從傳入連接請求隊列提取連接。 這兩種方法將阻止。 如果要避免阻止,可首先使用 Pending 方法來確定隊列中是否有可用的連接請求。
調(diào)用 Stop 方法來關(guān)閉 TcpListener。
還需要理解線程,委托和事件。
服務(wù)器端
有一個listener類來監(jiān)聽,Sender類來發(fā)送消息。
listener類關(guān)鍵代碼
private void Listen()
{
try
{
IPAddress addr = IPAddress.Parse(Form1.ipaddress);
IPEndPoint ipLocalEndPoint = new IPEndPoint(addr, 5656);//服務(wù)器端需要使用endpoint
tcp1 = new TcpListener(ipLocalEndPoint);
tcp1.Start();
while(listenerRun)//開始進行監(jiān)聽
{
Socket s = tcp1.AcceptSocket();
string remote = s.RemoteEndPoint.ToString();
Byte[] stream = new Byte[1024];
int i = s.Receive(stream);
string msg;
string str = System.Text.Encoding.UTF8.GetString(stream);
//MessageBox.Show(str);
if(str.Substring(0,1) == "1")
{
string str_ = "歡迎登陸!";
TcpClient tcpc = new TcpClient(((IPEndPoint)s.RemoteEndPoint).Address.ToString(), 5657);
NetworkStream tcpStream = tcpc.GetStream();
Byte[] data = System.Text.Encoding.UTF8.GetBytes(str_);//將字符串轉(zhuǎn)為byte
tcpStream.Write(data, 0, data.Length);
tcpc.Close();
msg = "<" + remote + ">" + "上線";
AddMessageEventArgs arg = new AddMessageEventArgs();
arg.mess = msg;
OnAddMessage(this, arg);
}
else if(str.Substring(0,1)=="0")
{
msg = "<" + remote + ">" + "斷開";
AddMessageEventArgs argRe = new AddMessageEventArgs();
argRe.mess = remote.ToString();
OnIpRemod(this, argRe);
AddMessageEventArgs arg = new AddMessageEventArgs();
arg.mess = msg;
OnAddMessage(this, arg);
}
else
{
msg = "<" + remote + ">" + System.Text.UTF8Encoding.UTF8.GetString(stream);
AddMessageEventArgs arg = new AddMessageEventArgs();
arg.mess = msg;
OnAddMessage(this, arg);
}
}
}
sender關(guān)鍵代碼
public void Send(string str)//需要發(fā)送的字符串
{
try
{
TcpClient tcpc = new TcpClient(obj, 5657);
NetworkStream tcpStream = tcpc.GetStream();
Byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(str);
tcpStream.Write(data, 0, data.Length);
tcpStream.Close();
}
catch(Exception)
{
MessageBox.Show("連接被目標主機拒絕");
}
}
客戶端
listener類關(guān)鍵代碼
private void Listen()
{
try
{
IPAddress addr = IPAddress.Parse(Form1.ipaddress);
IPEndPoint ipLocalEndPoint = new IPEndPoint(addr, 5657);
tcp1 = new TcpListener(ipLocalEndPoint);
tcp1.Start();
while(listenerRun)
{
Socket s = tcp1.AcceptSocket();
string remote = s.RemoteEndPoint.ToString();
Byte[] stream = new Byte[1024];
int i = s.Receive(stream);
string msg = "<" + remote + ">" + System.Text.UTF8Encoding.UTF8.GetString(stream);
AddMessageEventArgs arg = new AddMessageEventArgs();
arg.mess = msg;
OnAddMessage(this, arg);
// MessageBox.Show(msg);
}
}
catch(System.Security.SecurityException)
{
MessageBox.Show("防火墻禁止連接");
}
catch(Exception)
{
MessageBox.Show("監(jiān)聽已經(jīng)停止");
}
}
}
提供文件下載
鏈接: http://pan.baidu.com/s/1skYeq6L 密碼: 93yg