Created
January 4, 2021 16:22
-
-
Save vgvgvvv/39802ff54d9153ff1b84f774ecf8ed9f to your computer and use it in GitHub Desktop.
Simple C# Server/Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using TestServer; | |
namespace TestClient | |
{ | |
public class Client | |
{ | |
public void ConnectToServer() | |
{ | |
var host = "127.0.0.1"; | |
IPAddress ipa = IPAddress.Parse(host); | |
IPEndPoint ipe = new IPEndPoint(ipa, 8000); | |
TcpClient client = new TcpClient(); | |
try | |
{ | |
Console.WriteLine("主机IP=" + ipa.ToString()); | |
Console.WriteLine("连接主机中"); | |
client.Connect(ipe); | |
if (client.Connected) | |
{ | |
Console.WriteLine("连接成功!"); | |
CommunicateBase cb = new CommunicateBase(); | |
cb.SendMsg("这是客户端传给主机的信息", client); | |
Console.WriteLine(cb.ReceiveMsg(client)); | |
} | |
else | |
{ | |
Console.WriteLine("连接失败!"); | |
} | |
Console.Read(); | |
} | |
catch (Exception e) | |
{ | |
client.Close(); | |
Console.WriteLine(e.Message); | |
Console.Read(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Sockets; | |
using System.Text; | |
namespace TestServer | |
{ | |
public class CommunicateBase | |
{ | |
public void SendMsg(string msg, TcpClient client) | |
{ | |
NetworkStream ns = client.GetStream(); | |
if (ns.CanWrite) | |
{ | |
var msgBytes = Encoding.Default.GetBytes(msg); | |
ns.Write(msgBytes, 0, msgBytes.Length); | |
} | |
} | |
public string ReceiveMsg(TcpClient client) | |
{ | |
string result = string.Empty; | |
byte[] receiveBytes = new byte[client.ReceiveBufferSize]; | |
int numberOfBytesRead = 0; | |
NetworkStream ns = client.GetStream(); | |
if (ns.CanRead) | |
{ | |
do | |
{ | |
numberOfBytesRead = ns.Read(receiveBytes, 0, client.ReceiveBufferSize); | |
result = Encoding.Default.GetString(receiveBytes, 0, numberOfBytesRead); | |
} while (ns.DataAvailable); | |
} | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Sockets; | |
namespace TestServer | |
{ | |
public class HandleClient | |
{ | |
private TcpClient _tcpClient; | |
public HandleClient(TcpClient client) | |
{ | |
_tcpClient = client; | |
} | |
public void Communicate() | |
{ | |
try | |
{ | |
CommunicateBase cb = new CommunicateBase(); | |
var msg = cb.ReceiveMsg(_tcpClient); | |
Console.WriteLine(msg + "\n"); | |
cb.SendMsg("主机回传消息", _tcpClient); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("客户端强制关闭连接 " + e); | |
_tcpClient.Close(); | |
Console.Read(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
namespace TestServer | |
{ | |
public class Server | |
{ | |
public void ListenToConnection() | |
{ | |
string hostName = "127.0.0.1"; | |
Console.WriteLine("本機名稱=" + hostName); | |
var ipa = Dns.GetHostAddresses(hostName); | |
Console.WriteLine("本機IP=" + ipa[0].ToString()); | |
var ipe = new IPEndPoint(ipa[0], 8000); | |
TcpListener tcpListener = new TcpListener(ipe); | |
tcpListener.Start(); | |
Console.WriteLine("等待连线中..."); | |
int numberOfClients = 0; | |
while (true) | |
{ | |
try | |
{ | |
TcpClient client = tcpListener.AcceptTcpClient(); | |
if (client.Connected) | |
{ | |
Console.WriteLine("连线成功"); | |
HandleClient handleClient = new HandleClient(client); | |
var thread = new Thread(new ThreadStart(handleClient.Communicate)); | |
numberOfClients += 1; | |
thread.IsBackground = true; | |
thread.Start(); | |
thread.Name = client.Client.RemoteEndPoint.ToString(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
Console.Read(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment