/*
* Socket(套接字)編程
* 設備之間的識別依靠IP地址
* 設備中進程之間的是別的靠端口號
* TCP/IP協議 用於距離較遠的,對精準度要求高的信息傳輸
* UDP協議用於距離較近,對信息精準度要求較低但要求速度快的信息傳輸
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace A18_SocketProgram
{
class Server
{
private Socket _SockServer; //服務器監聽套接字
private bool _IsListenContecion = true; //是否在監聽(目的是方便退出)
public Server()
{
Console.WriteLine("---------------服務端啓動-----------------");
//定義網絡終結點(封裝IP與端口)
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),1000);
//實例化套接字
_SockServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//服務器端綁定地址
_SockServer.Bind(endPoint);
//開始監聽
//.......
_SockServer.Listen(10); //10表示服務器監聽隊列最大長度
try
{
while (_IsListenContecion)
{
//Accept()接收客戶端的鏈接,這個方法會阻斷當前線程的運行
Socket sockMsgServer = _SockServer.Accept();
Console.WriteLine("有一個客戶端連接");
//開啓後臺線程進行與客戶端的會話
Thread thClientMsg = new Thread(ClientMsg);
thClientMsg.IsBackground = true; // 定義會話線程爲後臺線程。
thClientMsg.Name = "thClientMsg";
thClientMsg.Start(sockMsgServer);
}
}
catch (Exception)
{
}
}
//服務器與客戶端的通訊後臺線程
[Obsolete("這個方法有瑕疵")]
private void ClientMsgOld(object sockMsg)
{
Socket socketMsg = sockMsg as Socket; //通訊Socket
//準備一個“數據緩存”
while (true)
{
byte[] msgArray = new byte[1024 * 1024]; //1M空間
//接收客戶端發來的數據,返回真實的長度
int trueClinetMsgLength = socketMsg.Receive(msgArray);
//byte數組轉String
string strMsg = Encoding.UTF8.GetString(msgArray, 0, trueClinetMsgLength);
//顯示客戶端發來的數據
Console.WriteLine("客戶端發來的數據:" + strMsg);
}
}
//服務器與客戶端的通訊後臺線程
private void ClientMsg(object sockMsg)
{
Socket socketMsg = sockMsg as Socket; //通訊Socket
//準備一個“數據緩存”
while (true)
{
byte[] msgArray = new byte[1024 * 1024]; //1M空間
//接收客戶端發來的數據,返回真實的長度
int trueClinetMsgLength = socketMsg.Receive(msgArray);
//byte數組轉String
string strMsg = Encoding.UTF8.GetString(msgArray,0,trueClinetMsgLength);
//接到斷開連接的信息
if (strMsg == "gm_exit")
{
Console.WriteLine("客戶端斷開連接");
break;
}
//顯示客戶端發來的數據
Console.WriteLine("客戶端發來的數據:" + strMsg);
}
}
static void Main(string[] args)
{
Server obj = new Server();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace A19_SocketdProgram_Client
{
class Client
{
private Socket _SockClient; //客戶端通訊套接字
private IPEndPoint serverEndPoint; //連接到的服務器IP與端口信息
public Client()
{
//通訊地址端口號
serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000);
//建立客戶端的Sockte
_SockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
_SockClient.Connect(serverEndPoint);
}
catch (Exception)
{
}
Console.WriteLine("連接服務器成功!!");
}
//向服務器發送信息
[Obsolete("這個方法有瑕疵")]
public void SendMsgOld()
{
while (true)
{
//輸入信息
string strMsg = Console.ReadLine();
//退出
if (strMsg == "gm_exit")
{
break;
}
//字節轉換
Byte[] byteArray = Encoding.UTF8.GetBytes(strMsg);
//發送數據
_SockClient.Send(byteArray);
Console.WriteLine("我: " + strMsg);
}
//關閉連接
_SockClient.Shutdown(SocketShutdown.Both);
//清理連接的資源
_SockClient.Close();
}
//向服務器發送信息
public void SendMsg()
{
while (true)
{
//輸入信息
string strMsg = Console.ReadLine();
//字節轉換
Byte[] byteArray = Encoding.UTF8.GetBytes(strMsg);
//發送數據
_SockClient.Send(byteArray);
//退出
if (strMsg == "gm_exit")
{
break;
}
Console.WriteLine("我: " + strMsg);
}
//關閉連接
_SockClient.Shutdown(SocketShutdown.Both);
//清理連接的資源
_SockClient.Close();
}
static void Main(string[] args)
{
Client obj = new Client();
obj.SendMsg();
//obj.SendMsg();
}
}
}
Server類和Client類分別在不同的啓動項中