c sharp 中的客户端网络和服务器网络
Client network and server network in c sharp
我和我的朋友目前正尝试在 visual studio 中使用 c sharp 构建一个聊天应用程序。
在这样做的时候,我们遇到了 "client network" 和 "server network" 这两个词。
我被告知这是两个 dll 文件,它们提供了我的客户端、服务器和数据库之间的连接。任何人都可以向我解释这些 dll 文件应该包含什么以及它们如何为我们的聊天应用程序做出贡献(我还是个初学者)
非常感谢!
根据你的描述,你想解决客户端与服务端的通信问题。
推荐大家使用socket实现server和client的连接,
创建两个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);
}
}
}
运算结果:
服务器:
客户:
我和我的朋友目前正尝试在 visual studio 中使用 c sharp 构建一个聊天应用程序。 在这样做的时候,我们遇到了 "client network" 和 "server network" 这两个词。 我被告知这是两个 dll 文件,它们提供了我的客户端、服务器和数据库之间的连接。任何人都可以向我解释这些 dll 文件应该包含什么以及它们如何为我们的聊天应用程序做出贡献(我还是个初学者) 非常感谢!
根据你的描述,你想解决客户端与服务端的通信问题。 推荐大家使用socket实现server和client的连接,
创建两个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);
}
}
}
运算结果:
服务器:
客户: