当前位置: 首页 > 知识库问答 >
问题:

TCP服务器到客户端

云骏奇
2023-03-14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Multi_Client
{
    class Program
    {
        private static Socket _clientSocket = new Socket
            (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        static void Main(string[] args)
        {
            Console.Title = "Client";
            ConnectToServer();
            RequestLoop();
            Exit();
        }

        private static void ConnectToServer()
        {
            int attempts = 0;

            while (!_clientSocket.Connected)
            {
                try
                {
                    attempts++;
                    Console.WriteLine("Connection attempt " + attempts);
                    _clientSocket.Connect(IPAddress.Loopback, 100);
                }
                catch (SocketException)
                {
                    Console.Clear();
                }
            }

            Console.Clear();
            Console.WriteLine("Connected");
            _clientSocket.Send(Encoding.ASCII.GetBytes("C1"));
        }

        private static void RequestLoop()
        {
            Console.WriteLine(@"<Type ""exit"" to properly disconnect client>");

            SendRequest();
            ReceiveResponse();

        }

        /// <summary>
        /// Close socket and exit app
        /// </summary>
        private static void Exit()
        {
            SendString("exit"); // Tell the server we re exiting
            _clientSocket.Shutdown(SocketShutdown.Both);
            _clientSocket.Close();
            Environment.Exit(0);
        }

        private static void SendRequest()
        {
            Console.Write("Send a request: ");
            string request = Console.ReadLine();
            SendString(request);

            if (request.ToLower() == "exit")
            {
                Exit();
                return;
            }
        }

        private static void SendString(string text)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(text);
            _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
        }

        private static void ReceiveResponse()
        {
            byte[] buffer = new byte[2048];
            int received = _clientSocket.Receive(buffer, SocketFlags.None);

            if (received == 0) return;

            byte[] data = new byte[received];
            Array.Copy(buffer, data, received);
            string text = Encoding.ASCII.GetString(data);
            Console.WriteLine(text);
        }
    }
}

[服务器]:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using NetworkCommsDotNet;
using System.Net.Sockets;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private static List<Socket> _clientSockets = new List<Socket>();
        private static readonly int _BUFFER_SIZE = 2048;
        private static byte[] _buffer = new byte[_BUFFER_SIZE];
        string text;
        Socket current;
        Socket test;
        delegate void SetTextCallback(string text);

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.richTextBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.richTextBox1.Text += text + "\n";
            }
        }
        private void SetupServer()
        {
            SetText("Setting up server...");
            _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
            _serverSocket.Listen(5);
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
            SetText("Server Setup");
        }

        /// <summary>
        /// Close all connected client (we do not need to shutdown the server socket as its connections
        /// are already closed with the clients)
        /// </summary>
        private static void CloseAllSockets()
        {
            foreach (Socket socket in _clientSockets)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            _serverSocket.Close();
        }

        private void AcceptCallback(IAsyncResult AR)
        {
            Socket socket = null;

            try
            {
                socket = _serverSocket.EndAccept(AR);
            }
            catch (ObjectDisposedException) // I cannot seem to avoid this (on exit when properly closing sockets)
            {
                return;
            }

            _clientSockets.Add(socket);
            socket.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
            SetText("Client connected, waiting for request...");
            test = (Socket)AR.AsyncState;
            _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
        private void ReceiveCallback(IAsyncResult AR)
        {
            current = (Socket)AR.AsyncState;
            int received = 0;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                SetText("Client forcefully disconnected");
                current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
                _clientSockets.Remove(current);
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(_buffer, recBuf, received);
            text = Encoding.ASCII.GetString(recBuf);
            SetText("Received Text: " + text);
            if (text.ToLower() == "get tim") // Client requested time
            {
                SetText("Text is a get time request");
                byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
                current.Send(data);
                SetText("Time sent to client");
            }
            else if (text.ToString() == "C1") // Client wants to exit gracefully
            {
                SetText("Received :: C1");
                byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
                current.Send(data);
            }

            else
            {
                SetText("Server is sending invalid packet");
                SetText("Warning Sent");
            }
            current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetupServer();
        }
        private void Send_Received(string mess)
        {
            byte[] data = Encoding.ASCII.GetBytes(mess);
            test.Send(data);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            CloseAllSockets();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Send_Received(textBox1.Text);
        }


    }
}

共有1个答案

常茂
2023-03-14

简答:是的,有可能。

长阿斯瓦尔:是的。您当前的代码是这样实现的:

  • 服务器初始化并准备接收连接。
  • 客户端尝试连接。
  • 服务器接受连接,添加到clientSockets集合。
  • 客户端发送“C1”。
  • 服务器接收内容、解释并返回答案。
  • 客户端收到应答。
    null
 类似资料:
  • 创建 TCP 服务器 使用最简单的方法来创建一个 TCP 服务器,使用所有默认选项如下所示: NetServer server = vertx.createNetServer(); 配置 TCP 服务器 如果你不想默认值,可以将服务器配置通过传入一个NetServerOptions实例来创建它: NetServerOptions options = new NetServerOptions().s

  • 创建 TCP 客户端 最简单的方法来创建一个 TCP 客户端,使用默认选项如下所示: NetClient client = vertx.createNetClient(); 配置 TCP 客户端 如果你不想使用默认值,则创建TCP 客户端时,通过传入NetClientOptions实例可以配置: NetClientOptions options = new NetClientOptions().s

  • 在探索和实现Proact设计模式后,遇到了一个问题,即客户端(“C”客户端)连接在限制后不再接受。开始探索netty。这是我试图做的1。C客户端建立连接2。Java服务器接受连接并开始使用TCP向客户端发送8 Mb大小的字节缓冲区。有什么想法吗?netty是一个好的选择吗?我浏览了netty的一个很好的例子,不幸的是不走运。 先谢谢你。 尊敬的Ravi

  • Vert.x 可以轻松地编写非阻塞的 TCP 客户端和服务器。

  • 我需要实现一个TCP服务器,它基本上应该在与客户端握手时打开一个套接字。 套接字打开后服务器需要保持套接字打开,并且能够通过打开的套接字将消息从服务器推送到客户端 我查看了一些spring集成示例,但不确定我所看到的示例是否确实参考了我的需求。 1. Spring集成tcp是否有这种能力来保持打开套接字并将消息从服务器发送到客户端? 服务器还应支持传入请求 客户端实现是作为简单Tcp java客户

  • 我有一个Android Studio应用程序,用于客户端读取加速计的数据,并将其发送到运行TCP C套接字服务器的计算机。 我想等到服务器确认接收到数据后再发送一个。当我不得不从Android系统读取服务器的确认信息时,问题就出现了,这只是简单的崩溃,我不知道为什么。这是我使用的代码。 服务器代码 和客户端代码(Android Studio) 我在交流中遗漏了什么还是我做错了什么?