using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 網絡編程.SockteHandel
{
/// <summary>
/// 服務端處理類
/// </summary>
publicclassServerHandle
{
/// <summary>
/// 端口號
/// </summary>
publicstaticstringPoint{ get;set;}
/// <summary>
/// IP地址
/// </summary>
publicstaticstringIpSite{ get;set;}
/// <summary>
/// 服務端信息
/// </summary>
publicstaticTextBoxServerMsg{ get;set;}
/// <summary>
/// 接收到客戶端的消息
/// </summary>
publicstaticTextBoxReceiveMsg{ get;set;}
/// <summary>
/// 發送消息
/// </summary>
publicstaticTextBoxSendMessage{ get;set;}
/// <summary>
/// 負責通訊的Socket
/// </summary>
privateSocketConnectionSocket;
/// <summary>
/// 監聽端口
/// </summary>
publicvoidWatchPort()
{
//在服務器端建立一個負責監聽ID跟端口號 的Socket
Socket socketWatch =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//IPAddress ip = IPAddress.Any; //監聽
IPAddress ip =IPAddress.Parse(IpSite);
//建立端口號對象
IPEndPoint pointObj =newIPEndPoint(ip,Convert.ToInt32(Point));
//監聽
socketWatch.Bind(pointObj);
ServerMsg.Text+="監聽成功······\r\n ";
//設定最多十個排隊鏈接請求
socketWatch.Listen(10);
//開啓一個新線程
Thread thread =newThread(Listen);
thread.IsBackground=true;
thread.Start(socketWatch);
}
/// <summary>
/// 等待客戶端鏈接,而且建立與之通訊的Socket
/// </summary>
/// <param name="obj"></param>
voidListen(object obj)
{
Socket soketlisten = obj asSocket;
while(true)
{
//等待客戶端鏈接,並建立一個負責通訊的Socket
Socket socketSend = soketlisten.Accept();
ServerMsg.Text+="鏈接成功·······"+ socketSend.RemoteEndPoint.ToString()+"\r\n";
//開啓一個新線程不停接收客戶端發來的消息
Thread reciveThread =newThread(Recive);
reciveThread.IsBackground=true;
reciveThread.Start(socketSend);
}
}
/// <summary>
/// 服務器端不停接收客戶端發來的消息
/// </summary>
/// <param name="obj"></param>
voidRecive(object obj)
{
//接收消息
ConnectionSocket= obj asSocket;
if(ConnectionSocket==null)
{
return;
}
while(true)
{
//接收客戶端發來信息
byte[] buffer =newbyte[1024*1024*2];
//實際接收的有效字節數
int receiveIndex =ConnectionSocket.Receive(buffer);
//以實際接收有效字節數來判斷客戶端是否下線了
if(receiveIndex ==0)
{
break;
}
string str =Encoding.UTF8.GetString(buffer,0, buffer.Length);
ReceiveMsg.Text+= str +"\r\n";
}
}
/// <summary>
/// 服務端發送消息
/// </summary>
publicvoidServerSendMessage()
{
byte[] buffer =Encoding.UTF8.GetBytes(SendMessage.Text);
int connectionIndex =ConnectionSocket.Send(buffer);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 網絡編程.SockteHandle
{
/// <summary>
/// 客戶端
/// </summary>
publicclassClientHandle
{
/// <summary>
/// IP地址
/// </summary>
publicstaticstringConnectionIp{ get;set;}
/// <summary>
/// 端口號
/// </summary>
publicstaticstringPoint{ get;set;}
//發送消息
publicTextBoxSendMsg{ get;set;}
/// <summary>
/// 接收消息
/// </summary>
publicTextBoxReciveMsg{ get;set;}
/// <summary>
/// 客戶端Socket對象
/// </summary>
publicSocket socketSend =null;
/// <summary>
/// 建立負責通訊的Socket
/// </summary>
publicvoidCreateClientSocket()
{
socketSend =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ip =IPAddress.Parse(ConnectionIp);
IPEndPoint endPoint =newIPEndPoint(ip,Convert.ToInt32(Point));
socketSend.Connect(endPoint);
ReciveMsg.Text+="鏈接到服務器";
//建立一個線程來接收服務器端數據
Thread reciveThread =newThread(ReciveServerMessage);
reciveThread.IsBackground=true;
reciveThread.Start(socketSend);
}
/// <summary>
/// 接收服務端信息
/// </summary>
voidReciveServerMessage(object obj)
{
Socket reciveSocket = obj asSocket;
while(true)
{
byte[] buffer =newbyte[1024*1024*2];
int reciveIndex = reciveSocket.Receive(buffer);
if(reciveIndex ==0)
{
break;
}
ReciveMsg.Text+="\r\n"+Encoding.UTF8.GetString(buffer)+"\r\n";
}
}
/// <summary>
/// 發送消息
/// </summary>
publicvoidSendMessage()
{
byte[] buffer =Encoding.UTF8.GetBytes(SendMsg.Text);
int sendIndex = socketSend.Send(buffer);
}
}
}