/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
private static void Listen()
{
try
{
TcpListen = new TcpListener(port);
TcpListen.Start();
stRead = TcpListen.AcceptSocket();
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
string sHostName = host.HostName;
while (bListener)
{
stRead.Send(Encoding.ASCII.GetBytes("Hello"));
string sTime = DateTime.Now.ToShortTimeString();
Byte[] byRead = new Byte[1024];
int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
Byte[] byText = new Byte[iRead];
Array.Copy(byRead, 0, byText, 0, iRead);
string line = System.Text.Encoding.Default.GetString(byRead);
}
}
catch (System.Security.SecurityException)
{
//監聽失敗
}
}
thThreadRead = new Thread(new ThreadStart(Listen));
thThreadRead.Start();
2.簡單客戶端
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
*/
private static IPEndPoint dateTimeHost;
string hostIPString=%%1;
string hostPortString=%%2;
IPAddress hostIP=IPAddress.Parse(hostIPString);
dateTimeHost=new IPEndPoint(hostIP,Int32.Parse(hostPortString));
Socket conn=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
conn.Connect(dateTimeHost);
int bytes=0;
Byte[] RecvBytes=new Byte[256];
bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);
string RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(RecvString);
conn.Shutdown(SocketShutdown.Both);
conn.Close();
3.獲得本機IP
//using System.Net;
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
string %%1=null;
for (int i = 0; i < addressList.Length; i++)
{
%%1 += addressList[i].ToString();
}
4.端對端通信
/*
using System.Net;
using System.Net.Sockets;
*/
UdpClient client=new UdpClient(%%2);
IPAddress a=IPAddress.Parse("127001");
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress HostIP=null;
byte[] sendData=Encoding.UTF8.GetBytes(%%3);
byte[] recData;
try{
HostIP=IPAddress.Parse(%%1);
}
catch {
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Send(sendData,sendData.Length,%%4,%%2);
client.Close();
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
recData=client.Receive(ref receivePoint);
%%3=Encoding.UTF8.GetString(recData);
client.Close();
5.點對點通信
/*
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Threading;
*/
Thread th;
TcpListener tpListen1;
bool listenerRun=true;
NetworkStream tcpStream;
StreamWriter reqStreamW;
TcpClient tcpc;
Socket skSocket;
protected void Listen()
{
try{
tpListen1=new TcpListener(Int32.Parse(%%2));
tpListen1.Start();
skSocket=tpListen1.AcceptSocket();
EndPoint tempRemoteEP=skSocket.RemoteEndPoint;
IPEndPoint tempRemoteIP=(IPEndPoint)tempRemoteEP;
IPHostEntry host=Dns.GetHostByAddress(tempRemoteIP.Address);
string HostName=host.HostName;
while(listenerRun)
{
Byte[] stream=new Byte[1024];
string time=DateTime.Now.ToString();
int i=skSocket.ReceiveFrom(stream,ref tempRemoteEP);
string %%5=Encoding.UTF8.GetString(stream);
//指定編碼,從緩沖區中解析出內容
//time+" "+HostName+":"
}
}
catch(Security.SecurityException)
{
//防火墻安全錯誤!
}
try{
string sMsg=%%4;
string MyName=Dns.GetHostName();
reqStreamW=new StreamWriter(tcpStream);
reqStreamW.Write(sMsg);
reqStreamW.Flush();
string time=DateTime.Now.ToString();
//顯示傳送的數據和時間
//time+" "+MyName+":"
//sMsg
}
catch(Exception)
{
//無法發送信息到目標計算機!
}
protected override void Dispose(bool disposing)
{
try{
listenerRun=false;
th.Abort();
th=null;
tpListen1.Stop();
skSocket.Close();
tcpc.Close();
}
catch{}
if(disposing && component!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
C/S(Client/Server)結構,即大家熟知的客戶機和服務器結構。它是軟件系統體系結構,通過它可以充分利用兩端硬件環境的優勢,將任務合理分配到Client端和Server端來實現,降低了系統的通訊開銷。目前大多數應用軟件系統都是Client/Server形式的兩層結構,由於現在的軟件應用系統正在向分布式的Web應用發展,Web和Client/Server應用都可以進行同樣的業務處理,應用不同的模塊***享邏輯組件;因此,內部的和外部的用戶都可以訪問新的和現有的應用系統,通過現有應用系統中的邏輯可以擴展出新的應用系統。這也就是目前應用系統的發展方向。 傳統的C/S體系結構雖然采用的是開放模式,但這只是系統開發壹級的開放性,在特定的應用中無論是Client端還是Server端都還需要特定的軟件支持。由於沒能提供用戶真正期望的開放環境,C/S結構的軟件需要針對不同的操作系統系統開發不同版本的軟件,加之產品的更新換代十分快,已經很難適應百臺電腦以上局域網用戶同時使用。而且代價高,效率低。 C/S的優點是能充分發揮客戶端PC的處理能力,很多工作可以在客戶端處理後再提交給服務器。對應的優點就是客戶端響應速度快。缺點主要有以下幾個: 1、只適用於局域網。而隨著互聯網的飛速發展,移動辦公和分布式辦公越來越普及,這需要我們的系統具有擴展性。這種方式遠程訪問需要專門的技術,同時要對系統進行專門的設計來處理分布式的數據。 2、客戶端需要安裝專用的客戶端軟件。首先涉及到安裝的工作量,其次任何壹臺電腦出問題,如病毒、硬件損壞,都需要進行安裝或維護。特別是有很多分部或專賣店的情況,不是工作量的問題,而是路程的問題。還有,系統軟件升級時,每壹臺客戶機需要重新安裝,其維護和升級成本非常高。 3、 對客戶端的操作系統壹般也會有限制。可能適應於Win98, 但不能用於Win2000或Windows XP。或者不適用於微軟新的操作系統等等,更不用說Linux、Unix等。
遊戲協議/service/license.html