C# code
namespace UDPServer
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
//構建TCP 服務器
//得到本機IP,設置TCP端口號
IPEndPoint ipep = new IPEndPoint(IPAddress.Any , 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp);
//綁定網絡地址
newsock.Bind(ipep);
Console.WriteLine("This is a Server, host name is {0}",Dns.GetHostName());
//等待客戶機連接
Console.WriteLine("Waiting for a client...");
//得到客戶機IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console .WriteLine ("Message received from {0}: ", Remote.ToString ());
Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv ));
//客戶機連接成功後,發送歡迎信息
string welcome = "Welcome ! ";
//字符串與字節數組相互轉換
data = Encoding .ASCII .GetBytes (welcome );
//發送信息
newsock .SendTo (data ,data.Length ,SocketFlags .None ,Remote );
while (true )
{
data =new byte [1024];
//發送接受信息
recv =newsock.ReceiveFrom(data ,ref Remote);
Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv));
newsock .SendTo (data ,recv ,SocketFlags .None ,Remote );
}
}
}
}
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input ,stringData;
//構建TCP 服務器
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
//設置服務IP,設置TCP端口號
IPEndPoint ipep = new IPEndPoint(IPAddress .Parse ("127.0.0.1") , 8001);
//定義網絡類型,數據連接類型和網絡協議UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello! ";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding .ASCII .GetString (data,0,recv));
while (true)
{
input = Console .ReadLine ();
if (input =="exit")
break ;
server .SendTo (Encoding .ASCII .GetBytes (input ),Remote );
data = new byte [1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console .WriteLine ("Stopping Client.");
server .Close ();
}
}
}
C# code
TCPClient
TCPClient 類提供了一種使用 TCP 協議連接到某個端點的簡化方法。它還通過 NetworkStream 對象展現在連接過程中讀取或寫入的數據。請參見下面從 QuickStart 文檔中摘錄的日期/時間客戶機示例。
使用 C# 編寫
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
class Client
{
public static void Main(String[] args)
{
TCPClient tcpc = new TCPClient();
Byte[] read = new Byte[32];
if (args.Length != 1)
{
Console.WriteLine(“請在命令行中指定服務器名稱”);
return;
}
String server = args[0];
// 驗證服務器是否存在
if (DNS.GetHostByName(server) == null)
{
Console.WriteLine(“找不到服務器:” + 服務器);
return;
}
// 嘗試連接到服務器
if (tcpc.Connect(server, 13) == -1)
{
Console.WriteLine(“無法連接到服務器:” + 服務器);
return;
}
// 獲取流
Stream s = tcpc.GetStream();
// 讀取流並將它轉換爲 ASCII 碼形式
int bytes = s.Read(read, 0, read.Length);
String Time = Encoding.ASCII.GetString(read);
// 顯示數據
Console.WriteLine(“已接收到的” + 字節 + “字節”);
Console.WriteLine(“當前日期和時間是:” + 時間);
tcpc.Close();
}
}
TCPListener
TCPListener 類便於在來自某個客戶機的 TCP 連接的特定套接字上進行偵聽的工作。請參見下面包括在 QuickStart 文檔中的日期/時間服務器示例。
使用 C# 編寫
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Server
{
public static void Main()
{
DateTime now;
String strDateLine;
Encoding ASCII = Encoding.ASCII;
// 在端口 13 進行偵聽
TCPListener tcpl = new TCPListener(13);
tcpl.Start();
Console.WriteLine(“正在等待客戶進行連接”);
Console.WriteLine(“請按 Ctrl+c 退出...”);
while (true)
{
// 接收會阻塞,直到有人連接上
Socket s = tcpl.Accept();
// 獲取當前的日期和時間並將它連接成一個字符串
now = DateTime.Now;
strDateLine = now.ToShortDateString() + " " +
now.ToLongTimeString();
// 將該字符串轉換成一個字節數組併發送它
Byte[] byteDateLine =
ASCII.GetBytes(strDateLine.ToCharArray());
s.Send(byteDateLine, byteDateLine.Length, 0);
Console.WriteLine(“發送” + strDateLine);
}
}
}
#region "Download: File transfer FROM ftp server"
/// <summary>
/// Copy a file from FTP server to local
/// </summary>
/// <param name="sourceFilename">Target filename, if required </param>
/// <param name="localFilename">Full path of the local file </param>
/// <returns> </returns>
/// <remarks>Target can be blank (use same filename), or just a filename
/// (assumes current directory) or a full path and filename </remarks>
public bool Download(string sourceFilename, string localFilename, bool PermitOverwrite)
{
//2. determine target file
FileInfo fi = new FileInfo(localFilename);
return this.Download(sourceFilename, fi, PermitOverwrite);
}
//Version taking an FtpFileInfo
public bool Download(FtpFileInfo file, string localFilename, bool permitOverwrite)
{
return this.Download(file.FullName, localFilename, permitOverwrite);
}
//Another version taking FtpFileInfo and FileInfo
public bool Download(FtpFileInfo file, FileInfo localFI, bool permitOverwrite)
{
return this.Download(file.FullName, localFI, permitOverwrite);
}
//Version taking string/FileInfo
public bool Download(string sourceFilename, FileInfo targetFI, bool permitOverwrite)
{
//1. check target
if (targetFI.Exists && !(permitOverwrite))
{
throw (new ApplicationException("Target file already exists"));
}
//2. check source
string target;
if (sourceFilename.Trim() == "")
{
throw (new ApplicationException("File not specified"));
}
else if (sourceFilename.Contains("/"))
{
//treat as a full path
target = AdjustDir(sourceFilename);
}
else
{
//treat as filename only, use current directory
target = CurrentDirectory + sourceFilename;
}
string URI = Hostname + target;
//3. perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);
//Set request to download a file in binary mode
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
//open request and get response stream
using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
//loop to read & write to file
using (FileStream fs = targetFI.OpenWrite())
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
//catch error and delete file only partially downloaded
fs.Close();
//delete target file as it's incomplete
targetFI.Delete();
throw;
}
}
responseStream.Close();
}
response.Close();
}
return true;
}
#endregion
簡單的UDP收發.
發送
C# code
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //向此網段發廣播包
int UDPListenerPort = 8082;
IPAddress broadcast = IPAddress.Parse("192.168.0.255"); //此處根據IP及子網掩碼改爲相應的廣播IP
string ts = "This is UPD string for sending";
byte[] sendbuf = Encoding.ASCII.GetBytes(ts);
IPEndPoint ep = new IPEndPoint(broadcast, UDPListenerPort);
s.SendTo(sendbuf, ep);
}
catch (Exception e)
{}
接收
C# code
UdpClient listener;
int UDPListenerPort = 8082;
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, UDPListenerPort);
try
{
while (true)
{
byte[] bytes = listener.Receive(ref groupEP);
string RecIP = groupEP.ToString().Substring(0, groupEP.ToString().IndexOf(":")); //收到發送UPD端的IP
string RecStr = Encoding.ASCII.GetString(bytes, 0, bytes.Length); //收到的UPD字符串
}
}
catch
{}
C# code
TCPClient
TCPClient 類提供了一種使用 TCP 協議連接到某個端點的簡化方法。它還通過 NetworkStream 對象展現在連接過程中讀取或寫入的數據。請參見下面從 QuickStart 文檔中摘錄的日期/時間客戶機示例。
使用 C# 編寫
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
class Client
{
public static void Main(String[] args)
{
TCPClient tcpc = new T…
來一個Remoting的:
C# code
using System;
namespace Remotable
{
public class RemotableType : MarshalByRefObject
{
private string _internalString = "This is the RemotableType.";
public string StringMethod()
{
return _internalString;
}
}
}
using System;
using System.Runtime.Remoting;
namespace RemotingFirst
{
public class Listener
{
public static void Main()
{
RemotingConfiguration.Configure("Listener.exe.config");
Console.WriteLine("Listening for requests. Press Enter to exit");
Console.ReadLine();
}
}
}
using System;
using System.Runtime.Remoting;
namespace Client
{
public class Client
{
public static void Main()
{
RemotingConfiguration.Configure("Client.exe.config");
Remotable.RemotableType remoteObject = new Remotable.RemotableType();
Console.WriteLine(remoteObject.StringMethod());
}
}
}
Listener.exe.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="Singleton"
type="Remotable.RemotableType, RemotableType"
objectUri="RemotableType.rem"
/>
</service>
<channels>
<channel ref="http" port="8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
實只要用到Socket聯接,基本上就得使用Thread,是交叉使用的。
C#封裝的Socket用法基本上不算很複雜,只是不知道託管之後的Socket有沒有其他性能或者安全上的問題。
在C#裏面能找到的最底層的操作也就是socket了,概念不做解釋。
程序模型如下:
WinForm程序 : 啓動端口偵聽;監視Socket聯接情況;定期關閉不活動的聯接;
Listener:處理Socket的Accept函數,偵聽新鏈接,建立新Thread來處理這些聯接(Connection)。
Connection:處理具體的每一個聯接的會話。
1:WinForm如何啓動一個新的線程來啓動Listener:
//start the server
private void btn_startServer_Click(object sender, EventArgs e)
{
//this.btn_startServer.Enabled = false;
Thread _createServer = new Thread(new ThreadStart(WaitForConnect));
_createServer.Start();
}
//wait all connections
private void WaitForConnect()
{
SocketListener listener = new SocketListener(Convert.ToInt32(this.txt_port.Text));
listener.StartListening();
}
因爲偵聽聯接是一個循環等待的函數,所以不可能在WinForm的線程裏面直接執行,不然Winform也就是無法繼續任何操作了,所以才指定一個新的線程來執行這個函數,啓動偵聽循環。
這一個新的線程是比較簡單的,基本上沒有啓動的參數,直接指定處理函數就可以了。
2:Listener如何啓動循環偵聽,並且啓動新的帶有參數的線程來處理Socket聯接會話。
先看如何建立偵聽:(StartListening函數)
IPEndPoint localEndPoint = new IPEndPoint(_ipAddress, _port);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(20);//20 trucks
// Start listening for connections.
while (true)
{
// here will be suspended while waiting for a new connection.
Socket connection = listener.Accept();
Logger.Log("Connect", connection.RemoteEndPoint.ToString());//log it, new connection
……
}
}……
基本步驟比較簡單:
建立本機的IPEndPoint對象,表示以本機爲服務器,在指定端口偵聽;
然後綁定到一個偵聽Socket上;
進入while循環,等待新的聯接;
如果有新的聯接,那麼建立新的socket來對應這個聯接的會話。
值得注意的就是這一句聯接代碼:listener.Accept()。執行這一句的時候,程序就在這個地方等待,直到有新的聯檢請求的時候程序纔會執行下一句。這是同步執行,當然也可以異步執行。
新的聯接Socket建立了(Accept之後),對於這些新的socket該怎麼辦呢?他們依然是一個循環等待,所以依然需要建立新的Thread給這些Socket去處理會話(接收/發送消息),而這個Thread就要接收參數了。
Thread本身是不能接收參數的,爲了讓它可以接收參數,可以採用定義新類,添加參數作爲屬性的方法來解決。
因爲每一個Socket是一個Connection週期,所以我定義了這麼一個類public class Connection。這個類至少有這樣一個構造函數public Connection(Socket socket); 之所以這麼做,就是爲了把Socket參數傳給這個Connection對象,然後好讓Listener啓動這個Thread的時候,Thread可以知道他正在處理哪一個Socket。
具體處理的方法:(在Listener的StartListening函數,ocket connection = listener.Accept();之後)
Connection gpsCn = new Connection(connection);
//each socket will be wait for data. keep the connection.
Thread thread = new Thread(new ThreadStart(gpsCn.WaitForSendData));
thread.Name = connection.RemoteEndPoint.ToString();
thread.Start();
如此一來,這個新的socket在Accept之後就在新的Thread中運行了。
3:Connection的會話處理
建立了新的Connection(也就是socket),遠程就可以和這個socket進行會話了,無非就是send和receive。
現在先看看怎麼寫的這個線程運行的Connection. WaitForSendData函數
while (true)
{
bytes = new byte[1024];
string data = "";
//systm will be waiting the msg of receive envet. like Accept();
//here will be suspended while waiting for socket income msg.
int bytesRec = this._connection.Receive(bytes);
_lastConnectTime = DateTime.Now;
if (bytesRec == 0)//close envent
{
Logger.Log("Close Connection", _connection.RemoteEndPoint.ToString());
break;
}
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
//…….handle your data.
}
可以看到這個處理的基本步驟如下:
執行Receive函數,接收遠程socket發送的信息;
把信息從字節轉換到string;
處理該信息,然後進入下一個循環,繼續等待socket發送新的信息。
值得注意的有幾個:
1:Receive函數。這個函數和Listener的Accept函數類似。在這個地方等待執行,如果沒有新的消息,這個函數就不會執行下一句,一直等待。
2:接收的是字節流,需要轉化成字符串
3:判斷遠程關閉聯接的方式
4:如果對方的消息非常大,還得循環接收這個data。
4:如何管理這些聯接(thread)
通過上邊的程序,基本上可以建立一個偵聽,並且處理聯接會話。但是如何管理這些thread呢?不然大量產生thread可是一個災難。
管理的方法比較簡單,在Listener裏面我定義了一個靜態的哈希表(static public Hashtable Connections=new Hashtable();),存儲Connection實例和它對應的Thread實例。而connection中也加入了一個最後聯接時間的定義(private DateTime _lastConnectTime;)。在新鏈接建立的時候(Listener的Accept()之後)就把Connection實例和Thread實例存到哈希表中;在Connection的Receive的時候修改最後聯接時間。這樣我們就可以知道該Connection在哪裏,並且會話是否活躍。
然後在Winform程序裏頭可以管理這些會話了,設置設置超時。
在網絡環境下,我們最感興趣的兩個命名空間是System.Net和 System.Net.Sockets。System.Net命名空間通常與較高程的操作有關,例如download或upload,試用HTTP和其他協議進行Web請求等等,而System.Net.Sockets命名空間所包含的類通常與較低程的操作有關。如果要直接使用Sockets或者 TCP/IP之類的協議,這個命名空間的類是非常有用的。
在.Net中,System.Net.Sockets 命名空間爲需要嚴密控制網絡訪問的開發人員提供了 Windows Sockets (Winsock) 接口的託管實現。System.Net 命名空間中的所有其他網絡訪問類都建立在該套接字Socket實現之上,如TCPClient、TCPListener 和 UDPClient 類封裝有關創建到 Internet 的 TCP 和 UDP 連接的詳細信息;NetworkStream類則提供用於網絡訪問的基礎數據流等,常見的許多Internet服務都可以見到Socket的蹤影,如 Telnet、Http、Email、Echo等,這些服務儘管通訊協議Protocol的定義不同,但是其基礎的傳輸都是採用的Socket。
其實,Socket可以象流Stream一樣被視爲一個數據通道,這個通道架設在應用程序端(客戶端)和遠程服務器端之間,而後,數據的讀取(接收)和寫入(發送)均針對這個通道來進行。
可見,在應用程序端或者服務器端創建了Socket對象之後,就可以使用Send/SentTo方法將數據發送到連接的Socket,或者使用Receive/ReceiveFrom方法接收來自連接Socket的數據。
針對Socket編程,.NET 框架的 Socket 類是 Winsock32 API 提供的套接字服務的託管代碼版本。其中爲實現網絡編程提供了大量的方法,大多數情況下,Socket 類方法只是將數據封送到它們的本機 Win32 副本中並處理任何必要的安全檢查。如果你熟悉Winsock API函數,那麼用Socket類編寫網絡程序會非常容易,當然,如果你不曾接觸過,也不會太困難,跟隨下面的解說,你會發覺使用Socket類開發 windows 網絡應用程序原來有規可尋,它們在大多數情況下遵循大致相同的步驟。
在使用之前,你需要首先創建Socket對象的實例,這可以通過Socket類的構造方法來實現:
public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType);
其中,addressFamily 參數指定 Socket 使用的尋址方案,socketType 參數指定 Socket 的類型,protocolType 參數指定 Socket 使用的協議。
下面的示例語句創建一個 Socket,它可用於在基於 TCP/IP 的網絡(如 Internet)上通訊。
Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
若要使用 UDP 而不是 TCP,需要更改協議類型,如下面的示例所示:
Socket temp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
一旦創建 Socket,在客戶端,你將可以通過Connect方法連接到指定的服務器,並通過Send/SendTo方法向遠程服務器發送數據,而後可以通過 Receive/ReceiveFrom從服務端接收數據;而在服務器端,你需要使用Bind方法綁定所指定的接口使Socket與一個本地終結點相聯,並通過Listen方法偵聽該接口上的請求,當偵聽到用戶端的連接時,調用Accept完成連接的操作,創建新的Socket以處理傳入的連接請求。使用完 Socket 後,記住使用 Shutdown 方法禁用 Socket,並使用 Close 方法關閉 Socket。
可以看出,以上許多方法包含EndPoint類型的參數,在Internet中,TCP/IP 使用一個網絡地址和一個服務端口號來唯一標識設備。網絡地址標識網絡上的特定設備;端口號標識要連接到的該設備上的特定服務。網絡地址和服務端口的組合稱爲終結點,在 .NET 框架中正是由 EndPoint 類表示這個終結點,它提供表示網絡資源或服務的抽象,用以標誌網絡地址等信息。.Net同時也爲每個受支持的地址族定義了 EndPoint 的子代;對於 IP 地址族,該類爲 IPEndPoint。IPEndPoint 類包含應用程序連接到主機上的服務所需的主機和端口信息,通過組合服務的主機IP地址和端口號,IPEndPoint 類形成到服務的連接點。
用到IPEndPoint類的時候就不可避免地涉及到計算機IP地址,System.Net命名空間中有兩種類可以得到IP地址實例:
IPAddress類:IPAddress 類包含計算機在 IP 網絡上的地址。其Parse方法可將 IP 地址字符串轉換爲 IPAddress 實例。下面的語句創建一個 IPAddress 實例:
IPAddress myIP = IPAddress.Parse("192.168.0.1");
Dns 類:向使用 TCP/IP Internet 服務的應用程序提供域名服務。其Resolve 方法查詢 DNS 服務器以將用戶友好的域名(如"host.mydomain.com")映射到數字形式的 Internet 地址(如 192.168.0.1)。Resolve方法 返回一個 IPHostEnty 實例,該實例包含所請求名稱的地址和別名的列表。大多數情況下,可以使用 AddressList 數組中返回的第一個地址。下面的代碼獲取一個 IPAddress 實例,該實例包含服務器 host.mydomain.com 的 IP 地址。
IPHostEntry ipHostInfo = Dns.Resolve("host.mydomain.com ");
IPAddress ipAddress = ipHostInfo.AddressList[0];
你也可以使用GetHostName方法得到IPHostEntry實例:
IPHosntEntry hostInfo=Dns.GetHostByName("host.mydomain.com ")
在使用以上方法時,你將可能需要處理以下幾種異常:
SocketException異常:訪問Socket時操作系統發生錯誤引發
ArgumentNullException異常:參數爲空引用引發
ObjectDisposedException異常:Socket已經關閉引發
在掌握上面得知識後,下面的代碼將該服務器主機( host.mydomain.com的 IP 地址與端口號組合,以便爲連接創建遠程終結點:
IPEndPoint ipe = new IPEndPoint(ipAddress,11000);
確定了遠程設備的地址並選擇了用於連接的端口後,應用程序可以嘗試建立與遠程設備的連接。下面的示例使用現有的 IPEndPoint 實例與遠程設備連接,並捕獲可能引發的異常:
try
{
temp.Connect(ipe);//嘗試連接
}
//處理參數爲空引用異常
catch(ArgumentNullException ae)
{
Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
}
//處理操作系統異常
catch(SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
需要知道的是:Socket 類支持兩種基本模式:同步和異步。其區別在於:在同步模式中,對執行網絡操作的函數(如 Send 和 Receive)的調用一直等到操作完成後纔將控制返回給調用程序。在異步模式中,這些調用立即返回。
綜合運用以上闡述的使用Visual C#進行Socket網絡程序開發的知識,下面的程序是一個簡單的Socket通訊實例,client向server發送一段測試字符串,server接收並顯示出來,給予client成功相應。
//client端
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace socketsample
{
class Class1
{
static void Main()
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine(recvStr);
c.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.ReadLine();
}
}
}
//server端
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Project1
{
class Class2
{
static void Main()
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(0);
Socket temp = s.Accept();
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine(recvStr);
string sendStr = "Ok!Sucess!";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
temp.Send(bs, bs.Length, 0);
temp.Shutdown(SocketShutdown.Both);
temp.Close();
s.Shutdown(SocketShutdown.Both);
s.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.ReadLine();
}
}
}
以上程序在VS Express 2005 .Net2.0環境下測試通過。
C#UDP的多路廣播組的發送和接收
下列範例使用 UdpClient,在通訊端口11000傳送UDP 資料包至多點傳送位址羣組 224.268.100.2。它傳送命令列上指定的信息字串。
C# code
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPMulticastSender {
private static IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private static int GroupPort = 11000;
private static void Send( String message) {
UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try {
Console.WriteLine("Sending datagram : {0}", message);
byte[] bytes = Encoding.ASCII.GetBytes(message);
sender.Send(bytes, bytes.Length, groupEP);
sender.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args) {
Send(args[0]);
return 0;
}
}
下列範例使用 UdpClient,在通訊端口 11000 監聽廣播到多點傳送位址羣組 224.168.100.2 的 UDP 資料包。它接收信息字串,並將信息寫入主控臺 (Console)。
C# code
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPMulticastListener {
private static readonly IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private const int GroupPort = 11000;
private static void StartListener() {
bool done = false;
UdpClient listener = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try {
listener.JoinMulticastGroup(GroupAddress);
listener.Connect(groupEP);
while (!done) {
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);
Console.WriteLine("Received broadcast from {0} :/n {1}/n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
listener.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args) {
StartListener();
return 0;
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPMulticastSender {
private static IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private static int GroupPort = 11000;
private static void Send( String message) {
UdpClient sender = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try {
Console.WriteLine("Sending datagram : {0}", message);
byte[] bytes = Encoding.ASCII.GetBytes(message);
sender.Send(bytes, bytes.Length, groupEP);
sender.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args) {
Send(args[0]);
return 0;
}
}
C# code
try
{
UdpClient udp=new UdpClient(new IPEndPoint(ipAddress,startPort+i));
udp.Close();
unUsedPort=startPort+i;
break;
}
catch
{
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace 聊天工具服務器
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
#region 字段定義
/// <summary>
/// 服務器程序使用的端口,默認爲8888
/// </summary>
private int _port = 8888;
/// <summary>
/// 接收數據緩衝區大小2K
/// </summary>
private const int _maxPacket =2 * 1024;
/// <summary>
/// 服務器端的監聽器
/// </summary>
private TcpListener _tcpl = null;
Thread _receiveThread;
/// <summary>
/// 保存所有客戶端會話的哈希表
/// </summary>
private Hashtable _transmit_tb = new Hashtable();
/// <summary>
/// 當前文件路徑
/// </summary>
string MyPath = null;
/// <summary>
/// 用戶基本信息表,包括UserName,UserPwd,UserNich,UserImg,ZX,UserIp
/// </summary>
DataTable TabUser = new DataTable();
/// <summary>
/// 用戶消息表,保存用戶不在線時的消息
/// </summary>
DataTable TabUserMessage = new DataTable();
#endregion
/// <summary>
/// 序列化在線列表,向客戶端返回序列化後的字節數組
/// </summary>
/// <returns>序列化後的字節數組 </returns>
private byte[] SerializeOnlineList()
{
StringCollection onlineList = new StringCollection();
foreach (object o in _transmit_tb.Keys)
{
onlineList.Add(o as string);
}
IFormatter format = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
format.Serialize(stream, onlineList);
byte[] ret = stream.ToArray();
stream.Close();
return ret;
}
/// <summary>
/// 序列化好友列表,向客戶端返回序列化後的datatable
/// </summary>
/// <returns>序列化後的字節數組 </returns>
private bool SerializeFriendList(object obj, Socket clientSkt)
{
DataTable TabmyFriend = new DataTable();
TabmyFriend.TableName = obj as string;
try {
TabmyFriend.ReadXml(MyPath + "//UserFriend//" + TabmyFriend.TableName + ".xml");
TabmyFriend.Columns.Add("UserImg", typeof(String));
TabmyFriend.Columns.Add("UserNich", typeof(String));
TabmyFriend.Columns.Add("ZX", typeof(Boolean));
TabmyFriend.Columns.Add("UserIp", typeof(String));
foreach (DataRow myrow in TabmyFriend.Rows)
{
DataRow[] DataRows = TabUser.Select(" UserName = '" + myrow["UserName"].ToString() + "'");
if (DataRows.Length > 0)
{
myrow["UserImg"] = DataRows[0]["UserImg"].ToString();
myrow["UserNich"] = DataRows[0]["UserNich"].ToString();
try
{
myrow["ZX"] = (bool)DataRows[0]["ZX"];
myrow["UserIp"] = DataRows[0]["UserIp"].ToString();
}
catch
{
myrow["ZX"] = false;
myrow["UserIp"] = "";
}
}
}
}
catch
{
TabmyFriend.Columns.Add("UserName", typeof(String));
TabmyFriend.Columns.Add("UserImg", typeof(String));
TabmyFriend.Columns.Add("ZX", typeof(Boolean));
TabmyFriend.Columns.Add("UserIp", typeof(String));
}
IFormatter format = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
format.Serialize(stream, TabmyFriend);
stream.Position = 0;
byte[] ret = new byte[_maxPacket];
int count = 0;
count = stream.Read(ret, 0, _maxPacket);
//先發送響應信號,用戶客戶機的判斷
clientSkt.Send(Encoding.Unicode.GetBytes("cmd::RequestFriendList"));
while (count >0)
{
clientSkt.Send(ret);
count = stream.Read(ret, 0, _maxPacket);
}
//發送結束信號
clientSkt.Send(Encoding.Unicode.GetBytes("Find::RequestFriendListEnd"));
stream.Close();
return true ;
}
private void FormMain_Load(object sender, EventArgs e)
{
MyPath = Application.StartupPath;
Read_User();
ReadTabUserMessage();
_receiveThread = new Thread(new ThreadStart(StartUp));
_receiveThread.Start();
}
/// <summary>
/// 讀取所有用戶信息
/// </summary>
private void Read_User()
{
try
{
TabUser.ReadXml(MyPath + "//User.xml");
}
catch
{
TabUser.TableName = "User";
TabUser.Columns.Add("UserName", typeof(String));
TabUser.Columns.Add("UserPwd", typeof(String));
TabUser.Columns.Add("UserNich", typeof(String));
TabUser.Columns.Add("UserImg", typeof(String));
}
TabUser.Columns.Add("ZX", typeof(Boolean));
TabUser.Columns.Add("UserIp", typeof(String));
}
/// <summary>
/// 新用戶上/下線後,更新其好友的(好友列表)
/// </summary>
/// <param name="UserName"> </param>
/// <param name="OnLine"> </param>
/// <param name="IpAddress"> </param>
private void UpdateFriendList(string UserName, bool OnLine, string IpAddress)
{
DataTable TabmyFriend = new DataTable();
TabmyFriend.TableName = UserName;
string svrlog = null;
string []UserInformation = new string[2];//UserName + "$" + IpAddress;
UserInformation[0] = UserName;
UserInformation[1] = IpAddress;
IFormatter format = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
format.Serialize(stream, UserInformation);
byte[] ret = stream.ToArray();
stream.Close();
if (OnLine)
{
svrlog = "cmd::RequestAddFriendList";
}
else
{
svrlog = "cmd::RequestRemoveFriendList";
DataRow[] DataRows = TabUser.Select(" UserName = '" + UserName + "'");
if (DataRows.Length > 0)
{
DataRows[0]["ZX"] = false;
DataRows[0]["UserIp"] = "";
}
}
try
{
TabmyFriend.ReadXml(MyPath + "//UserFriend//" + TabmyFriend.TableName + ".xml");
foreach (DataRow myrow in TabmyFriend.Rows)
{
if(_transmit_tb.ContainsKey(myrow["UserName"].ToString()))
{
Socket _clientSkt = _transmit_tb[myrow["UserName"].ToString()] as Socket;
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));
_clientSkt.Send(ret);
}
}
}
catch
{ }
}
[code=C#][/code /// <summary>
/// 線程執行體,轉發消息
/// </summary>
/// <param name="obj">傳遞給線程執行體的用戶名,用以與用戶通信 </param>
private void ThreadFunc(object obj)
{
//通過轉發表得到當前用戶套接字
Socket clientSkt = _transmit_tb[obj] as Socket;
//主循環
while (true)
{
try
{
//接受第一個數據包。
//由於程序邏輯結構簡單,所以在這裏對客戶機發送的第一個包內容作逐一判斷,
//這裏的實現不夠優雅,但不失爲此簡單模型的一個解決之道。
byte[] packetBuff = new byte[_maxPacket];
clientSkt.Receive(packetBuff);
string _str = Encoding.Unicode.GetString(packetBuff).TrimEnd('/0');
//如果是發給不在線好友的信息
if (_str.StartsWith("cmd::FriendMessage"))
{
string UserName = _str.Substring("cmd::FriendMessage".Length, 20).Trim();
string MessageS = _str.Substring("cmd::FriendMessage".Length + 20, _str.Length - "cmd::FriendMessage".Length - 20);
SaveMessage(obj as string, UserName, MessageS);
continue;
}
//如果是離線請求
if (_str.StartsWith("cmd::RequestLogout"))
{
_transmit_tb.Remove(obj);
UpdateFriendList((string)obj, false, "");
// string svrlog = string.Format("[系統消息]用戶 {0} 在 {1} 已斷開... 當前在線人數: {2}/r/n/r/n", obj, DateTime.Now, _transmit_tb.Count);
// Console.WriteLine(svrlog);
//向所有客戶機發送系統消息
//foreach (DictionaryEntry de in _transmit_tb)
//{
// string _clientName = de.Key as string;
// Socket _clientSkt = de.Value as Socket;
// _clientSkt.Send(Encoding.Unicode.GetBytes(svrlog));
//}
Thread.CurrentThread.Abort();
}
//如果是請求好友列表
if (_str.StartsWith("cmd::RequestFriendList"))
{
SerializeFriendList(obj, clientSkt);
// 將該用戶不在線時的信息發送給用戶
DataTable TabMessage = ReadMessage(obj as string);
if (TabMessage != null)
{
foreach (DataRow myrow in TabMessage.Rows)
{
if (myrow["SendUserName"].ToString() == "System::Message")
{
clientSkt.Send(Encoding.Unicode.GetBytes(myrow["Message"].ToString()));
}
else
{
clientSkt.Send(Encoding.Unicode.GetBytes("cmd::FriendMessage" + myrow["SendUserName"].ToString().PadRight(20, ' ') + myrow["Message"].ToString()));
}
}
}
//這裏不需要再繼續接受後繼數據包了,跳出當前循環體。
continue;
}
////如果是請求好友列表
//if (_str.StartsWith("cmd::RequestOnLineList"))
//{
// byte[] onlineBuff = SerializeOnlineList();
// //先發送響應信號,用戶客戶機的判斷
// clientSkt.Send(Encoding.Unicode.GetBytes("cmd::RequestOnLineList"));
// clientSkt.Send(onlineBuff);
// //這裏不需要再繼續接受後繼數據包了,跳出當前循環體。
// continue;
//}
//查找用戶
if (_str.StartsWith("Find::FindFriend"))
{
DataTable TabFind = TabUser.Clone();
DataRow [] FindRow =null ;
string UserName = _str.Substring("Find::FindFriend".Length, _str.Length - "Find::FindFriend".Length);
if (UserName.Equals("Find::WhoOnLine"))
{ //看誰在線
FindRow = TabUser.Select(" ZX = 1");
}
else//精確查找
{
FindRow = TabUser.Select("UserName = '" + UserName + "'");
}
foreach (DataRow myrow in FindRow)
{
TabFind.ImportRow(myrow);
}
clientSkt.Send(Encoding.Unicode.GetBytes("Find::FindFriend"));
IFormatter format = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
format.Serialize(stream, TabFind);
stream.Position = 0;
byte[] ret = new byte[_maxPacket];
int count = 0;
count = stream.Read(ret, 0, _maxPacket);
while (count >0)
{
clientSkt.Send(ret);
count = stream.Read(ret, 0, _maxPacket);
}
clientSkt.Send(Encoding.Unicode.GetBytes("Find::FindFriendEnd"));
stream.Close();
TabFind = null;
FindRow = null;
//這裏不需要再繼續接受後繼數據包了,跳出當前循環體。
continue;
}
//請求添加好友
if (_str.StartsWith("Find::AddFriendAsk"))
{
string UserName = _str.Substring("Find::AddFriendAsk".Length, _str.Length - "Find::AddFriendAsk".Length);
//通過轉發表查找接收方的套接字
if (_transmit_tb.Count != 0 && _transmit_tb.ContainsKey(UserName))
{
Socket receiverSkt = _transmit_tb[UserName] as Socket;
receiverSkt.Send(Encoding.Unicode.GetBytes("Find::AddFriendAsk" + obj as string));
}
//這裏不需要再繼續接受後繼數據包了,跳出當前循環體。
continue;
}
//回覆答應添加好友
if (_str.StartsWith("Find::AddFriendYes"))
{
string UserName = _str.Substring("Find::AddFriendYes".Length, _str.Length - "Find::AddFriendYes".Length);
//// 保存數據
DataTable TabmyFriend = new DataTable() ;
//保存該用戶
TabmyFriend.ReadXml(MyPath + "//UserFriend//" + obj as string + ".xml");
DataRow newRow = TabmyFriend.NewRow();
newRow["UserName"] = UserName;
TabmyFriend.Rows.Add(newRow);
TabmyFriend.WriteXml(MyPath + "//UserFriend//" + obj as string + ".xml", XmlWriteMode.WriteSchema, false);
//保存其好友
TabmyFriend = new DataTable();
TabmyFriend.ReadXml(MyPath + "//UserFriend//" + UserName + ".xml");
DataRow newRow1 = TabmyFriend.NewRow();
newRow1["UserName"] = obj as string;
TabmyFriend.Rows.Add(newRow1);
TabmyFriend.WriteXml(MyPath + "//UserFriend//" + UserName + ".xml", XmlWriteMode.WriteSchema, false);
TabmyFriend = null;
//更新好友列表
SerializeFriendList(obj, clientSkt);
上面發了服務器端,沒發客戶端,現在補上!不知道寫的好不好,見笑了
C# code
public partial class Form1 : Form
{
private TcpClient client;
private bool isExit = false;
private NetworkStream networkStream;
private EventWaitHandle allDone = new EventWaitHandle(false, EventResetMode.ManualReset);
#region 用於一個線程操作另一個線程的控件
private delegate void SetListBoxCallback(string str);
private SetListBoxCallback setListBoxCallBack;
private delegate void SetRichTextBoxCallback(string str);
private SetRichTextBoxCallback setRichTextBoxCallBack;
#endregion
public Form1()
{
InitializeComponent();
listBoxStatus.HorizontalScrollbar = true;
setListBoxCallBack = new SetListBoxCallback(SetListBox);
setRichTextBoxCallBack = new SetRichTextBoxCallback(SetReceiveText);
}
//狀態顯示
private void SetListBox(string str)
{
listBoxStatus.Items.Add(str);
listBoxStatus.SelectedIndex = listBoxStatus.Items.Count - 1;
listBoxStatus.ClearSelected();
}
//接收客戶端信息
private void SetReceiveText(string str)
{
richTextBoxReceive.AppendText(str);
}
//連接服務器....
private void buttonConnet_Click(object sender, EventArgs e)
{
client = new TcpClient(AddressFamily.InterNetwork);
//得到服務器IP
IPAddress ip= IPAddress.Parse("127.0.0.1");
//創建一個委託,並知名在異步操作完成時執行的方法
AsyncCallback callback = new AsyncCallback(RequestCallBack);
allDone.Reset();
client.BeginConnect(ip, 7100, RequestCallBack, client);
}
private void RequestCallBack(IAsyncResult ar)
{
allDone.Set();
try
{
client = (TcpClient)ar.AsyncState;
client.EndConnect(ar);
listBoxStatus.Invoke(setListBoxCallBack, string.Format("與服務器{0}連接成功", client.Client.RemoteEndPoint));
networkStream = client.GetStream();
ReadObject readObject = new ReadObject(networkStream, client.ReceiveBufferSize);
networkStream.BeginRead(readObject.bytes, 0, readObject.bytes.Length, ReadCallBack, readObject);
}
catch (Exception e1)
{
listBoxStatus.Invoke(setListBoxCallBack, e1.Message);
return;
}
}
//異步操作完成時執行的回調調用的方法
private void ReadCallBack(IAsyncResult ar)
{
try
{
ReadObject ro = (ReadObject)ar.AsyncState;
int count = ro.netStream.EndRead(ar);
richTextBoxReceive.Invoke(setRichTextBoxCallBack, System.Text.Encoding.UTF8.GetString(ro.bytes, 0, count));
if (isExit == false)
{
ro = new ReadObject(networkStream, client.ReceiveBufferSize);
networkStream.BeginRead(ro.bytes, 0, ro.bytes.Length, ReadCallBack, ro);
}
}
catch (Exception e2)
{
listBoxStatus.Invoke(setListBoxCallBack, e2.Message);
return;
}
}
//發送數據
private void SendString(string str)
{
try
{
byte[] by = System.Text.Encoding.UTF8.GetBytes(str+"/r/n");
networkStream.BeginWrite(by, 0, by.Length, new AsyncCallback(SendCallBack), networkStream);
networkStream.Flush();
}catch(Exception e3){
listBoxStatus.Invoke(setListBoxCallBack, e3.Message);
return;
}
}
//發送數據回調的方法
private void SendCallBack(IAsyncResult ar)
{
try
{
networkStream.EndWrite(ar);
}
catch (Exception e4)
{
listBoxStatus.Invoke(setListBoxCallBack, e4.Message);
return;
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
SendString(richTextBoxSend.Text);
richTextBoxSend.Clear();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isExit = true;
allDone.Set();
}
}
aliyun活動 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl