客户端网络和服务器网络的C锐

问题描述 投票:0回答:1

我和我的朋友们目前正试图用c sharp在visual studio中建立一个聊天应用程序.当我们这样做的时候,我们碰到了 "客户端网络 "和 "服务器网络 "这两个词.我已经被告知,这是两个dll文件,提供了一个连接我的客户端,服务器和数据库之间。有谁能向我解释一下这些dll文件应该包含什么,以及它们如何有助于我们的聊天应用程序(我还是个初学者),非常感谢你!我和我的朋友们目前正试图将这些dll文件连接到我的客户端,服务器和数据库。

c# visual-studio chat
1个回答
0
投票

根据你的描述,你想解决客户端和服务器之间的通信,我建议你使用socket来实现服务器和客户端之间的连接,而

创建两个DLL文件,方便程序参考。

下面将对其进行详细说明。

(1) ClientCommunication.dll文件

1:建立一个Socket对象。

2:使用socket对象的Connect()方法向服务器发送连接请求,参数为上面创建的EndPoint对象。

3:如果连接成功,使用socket对象的Send()方法向服务器发送信息。

4:使用套接字对象的Receive()方法接收服务器发送的信息。

5:通信结束后,一定要关闭套接字。

(2) ServerCommunication.dll。

1:建立一个Socket对象。

2:用socket对象的Bind()方法绑定EndPoint。

3:使用socket对象的Listen()方法开始监听。

4:接受与客户端的连接,使用socket对象的Accept()方法创建一个新的socket对象,用于与请求客户端进行通信。

5:使用新的套接字对象来接收和发送消息。

ServerCommunication.dll包含ServerSocket.csServerSocket.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.csClientSocket.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();   

        }
}
}

具体用法如下。

Server,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);

        }
    }
}

操作结果:

Server.cs代码:Client.cs代码:操作结果:Server:

enter image description here

客户端:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.