c# - cSharp中的客户端网络和服务器网络
问题描述
我和我的朋友们目前正在尝试在 Visual Studio 中使用 c sharp 构建一个聊天应用程序。在这样做的时候,我们碰到了“客户端网络”和“服务器网络”这两个词。我被告知这是两个 dll 文件,它们提供了我的客户端、服务器和数据库之间的连接。谁能向我解释这些 dll 文件应该包含什么以及它们如何为我们的聊天应用程序做出贡献(我还是个初学者)非常感谢!
解决方案
根据您的描述,您想解决客户端和服务器之间的通信问题。我推荐你使用socket来实现服务端和客户端的连接,并且
创建两个DLL文件以方便程序引用。
下面将详细说明。
(1) ClientCommunication.dll
1:建立一个Socket对象;
2:使用socket对象的Connect()方法,以上面创建的EndPoint对象为参数,向服务器发送连接请求;
3:如果连接成功,使用socket对象的Send()方法向服务器发送信息;
4:使用socket对象的Receive()方法接收服务器发送的信息;
5:一定要在通讯结束后关闭socket
(2) ServerCommunication.dll
1:建立一个Socket对象;
2:用socket对象的Bind()方法绑定EndPoint;
3:使用socket对象的Listen()方法开始监听;
4:接受与客户端的连接,使用socket对象的Accept()方法创建一个新的socket对象,用于与请求客户端进行通信;
5:使用新的socket对象接收和发送消息。
ServerCommunication.dll 包含 ServerSocket.cs ServerSocket.cs 代码:
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;
namespace ServerCommunication
{
public class ServerSocket
{
static Socket serverSocket;
public static void StartListening(IPAddress ip, int port)
{
serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(ip, port);
serverSocket.Bind(point);
Console.WriteLine("{0}Listen Success", serverSocket.LocalEndPoint.ToString());
serverSocket.Listen(10);
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
Console.ReadLine();
}
static void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
}
static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
//clientSocket accept
byte[] result = new byte[1024];
int receiveNumber = myClientSocket.Receive(result);
Console.WriteLine("Receive client{0}news{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
}
}
}
ClientCommunication.dll 包含 ClientSocket.cs ClientSocket.cs 代码:
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;
namespace ClientCommunication
{
public class ClientSocket
{
static byte[] result = new byte[1024];
public static void StartClient(IPAddress ip, int port)
{
Socket socketClient = new Socket(SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(ip, port);
try
{
socketClient.Connect(point);
Console.WriteLine("Successfully connected to server!");
}
catch
{
Console.WriteLine("Failed to connect to the server, please press enter to exit!");
return;
}
//clientSocket accept
int receiveLength = socketClient.Receive(result);
Console.WriteLine("Receive server message:{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
// clientSocket send
try
{
Thread.Sleep(1000);
string sendMessage = "client send Message Hellp" + DateTime.Now;
socketClient.Send(Encoding.ASCII.GetBytes(sendMessage));
Console.WriteLine("Send message to server:{0}" + sendMessage);
}
catch
{
socketClient.Shutdown(SocketShutdown.Both);
socketClient.Close();
}
Console.WriteLine("After sending, press enter to exit");
Console.ReadLine();
}
}
}
具体使用如下:
服务器,cs代码:
using ServerCommunication;
using System.Net;
namespace Server
{
class Server
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8885;
ServerSocket.StartListening(ip,port);
}
}
}
Client.cs 代码:
using ClientCommunication;
using System.Net;
namespace Client
{
class Client
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8885;
ClientSocket.StartClient(ip,port);
}
}
}
运行结果:
服务器:
客户:
推荐阅读
- python - pytorch 不同版本与“pip3 show torch”VS“torch.__version__”
- html - HTML:“onclick”每次刷新都会执行操作
- python - 如何获取运行脚本所需的参数数量?
- javascript - Jasmine , Karma - 在使用 toEqual 进行比较时忽略结果对象中的 id
- javascript - fs.writeFileSync 方法向文本文件写入错误
- thingsboard - 是否有用于在 Thingsboard 中创建用户的 HTTP API?
- facebook - 赛普拉斯,我想停止由按钮等待回调引起的函数
- c++ - C++ Libpq Postgresql - 函数主 CLibpq 中引用的未解析的外部符号 PQconnectdb
- javascript - 带有全选复选框和淘汰赛的 JQuery 数据表
- ios - 如何在windows中为ios创建安装文件