首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使服务器始终侦听(简单服务器)

使服务器始终侦听(简单服务器)
EN

Stack Overflow用户
提问于 2017-12-11 16:52:13
回答 1查看 223关注 0票数 0

我有一个服务器类,它从客户端类接收.txt文件。

My:我的服务器只接收第一个.txt文件,但在此之后客户端无法再发送,如何转换服务器类以使服务器始终侦听新文件?

以下是服务器:

代码语言:javascript
复制
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string rd;
        byte[] b1;
        string v;
        int m=20;//number of byts
        TcpListener list;
        TcpClient client;
        int port = 8100;//5050
        int port1 = 8100;//5055
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        private void button1_Click(object sender, EventArgs e)//browse button
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
               // while (true)

                    //try
                   // {
                        list = new TcpListener(localAddr, port1);
                        list.Start();


                        Thread incoming_connection = new Thread(ic);
                        incoming_connection.Start();
                        /*
                    }catch(Exception exc)
                    {
                        Console.Write(exc);
                        break;
                    }*/

            }
        }

        private void ic()
        {

            client = list.AcceptTcpClient();
            Stream s = client.GetStream();
            b1 = new byte[m];
            s.Read(b1,0, b1.Length);
            MessageBox.Show("pathh "+textBox1.Text);
            File.WriteAllBytes(textBox1.Text+"\\flag.txt", b1);// the left side us the name of the written file
            //list.Stop();
            //client.Close();
          //  label1.Text = "File Received......";
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            list = new TcpListener(localAddr, port);
           // TcpListener list = new TcpListener(port);
            list.Start();
            TcpClient client = list.AcceptTcpClient();
            MessageBox.Show("Client trying to connect");
            StreamReader sr = new StreamReader(client.GetStream());
            rd = sr.ReadLine();
            v = rd.Substring(rd.LastIndexOf('.') + 1);
            m = int.Parse(v);
           // list.Stop();
           // client.Close();
        }
    }

Related-1 Realted-2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-11 17:51:11

首先,你需要有人发短信,有人接收短信。

让我们从Sender开始,我们称之为Server

代码语言:javascript
复制
public class Server
{
    //This is where the receiver connection will be stored
    public TcpClient ClientConnection { get; set; }
    public int Port { get; set; }
    public string Host { get; set; }
    public TcpListener Listener;
    public Server(int port,string host)
    {
         this.Port = port;
         this.Host = host;
    }
    public void Start()
    {
         this.Listener = new TcpListener(IPAddress.Parse(Host), Port);
         TryConnect();
    }
    public void TryConnect()
    {
         //You can use thread
         Task.Factory.StartNew(AcceptTheClientConnection, TaskCreationOptions.LongRunning);
    }
    public void AcceptTheClientConnection()
    {
        ClientConnection = this.Listener.AcceptTcpClient();
    }
    public void SendText(string text)
    {
         if (ClientConnection != null)
         {
             try
             {
                 var buffer = System.Text.Encoding.Default.GetBytes(text);
                 ClientConnection.Client.Send(buffer, SocketFlags.None);
             }
             catch(Exception e)
             {
                 ClientConnection = null;
                 TryConnect();
             }
         }
            else throw new InvalidOperationException("You must connect to client first");
    }
}

现在Receiver是一个TcpClient,这个Receiver将通过为它提供Sender IP地址和端口来打开连接。Receiver在成功连接到Sender后将运行两个线程。第一个线程将继续侦听新文件,将接收到的文件缓冲区添加到队列中。第二个将处理这些缓冲区,您可以在这里放置自己的逻辑。

代码语言:javascript
复制
public class Client
{
    public int SenderPort { get; set; }
    public byte[] Buffer { get; set; }
    public string SenderHost { get; set; }
    public TcpClient SenderConnection;
    public Queue<byte[]> ReceivedTextFiles;
    public Client(int senderPort, string senderHost)
    {
        this.SenderPort = senderPort;
        this.SenderHost = senderHost;
        ReceivedTextFiles = new Queue<byte[]>();
    }
    public Task Connect()
    {
        return Task.Factory.StartNew(() =>
        {
            SenderConnection = new TcpClient();
            SenderConnection.Connect(SenderHost, SenderPort);
            Thread t = new Thread(Recieve);
            Thread t2 = new Thread(ProcessTextFiles);
            t.Start();
            t2.Start();
        });
    }
    public void Recieve()
    {
        while (true)
        {
            Thread.Sleep(500);
            if (SenderConnection.Available > 0)
            {
                lock (Buffer)
                {
                    Buffer = new byte[SenderConnection.Available];
                    int receivedBytes = SenderConnection.Client.Receive(Buffer);
                    if (receivedBytes > 0)
                    {
                        lock (ReceivedTextFiles)
                        {
                            ReceivedTextFiles.Enqueue(Buffer);
                            Buffer = null;
                        }
                    }
                }
            }
        }
    }
    public void ProcessTextFiles()
    {
        while (true)
        {
            byte[] textFile = null;
            lock (ReceivedTextFiles)
            {
                //We have buffers to process, get one, and remove it from the queue
                if (ReceivedTextFiles.Count > 0)
                {
                    textFile = ReceivedTextFiles.Dequeue();
                }
            }
            //Process the buffer
            var textFileContent = System.Text.Encoding.Default.GetString(textFile);
            //Do whatever you want
            Thread.Sleep(1500);
        }
    }
}

这是套接字背后的一般想法,以及如何在网络中发送和接收数据。请记住,这个解决方案是为您的目的量身定做的,它只接受一个连接。这不是最好的解决方案,但由于您的要求,我试图使它尽可能简单。另外,请注意,您可以使用Threads,或者Tasks,它取决于上下文。我将让您根据需要选择使用这两个类的人。当然,您可以编辑这两个类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47757628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档