连接超时背后无法解决的原因

时间:2017-09-03 17:21:40

标签: c# windows networking

服务器代码

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using Global;
using System.IO;

namespace Server
{
    class Program
    {
        static List<string> BlackList = new List<string>();
        static List<TcpClient> Clients = new List<TcpClient>();
        static object _lock = new object();
        static BinaryFormatter Formatter = new BinaryFormatter();

        static TcpListener Server;
        static void Main(string[] args)
        {
            Init();
            Console.ReadLine();
        }

        /*
         * Initiate the server and start listening
         */
        static void Init()
        {
            Server = new TcpListener(IPAddress.Any, 7777);
            Server.Start();
            Task.Factory.StartNew(() => Listen());
        }

        /*
         * Listens to new incoming connections
         * Assigns the new client a worker who will listen to it's requests
         * Add the client to the list of active clients
         */
        static void Listen()
        {
            for (;;)
            {
                TcpClient client = Server.AcceptTcpClient();
                lock(_lock)
                    Clients.Add(client);
                Task.Factory.StartNew(() => Work(client));
                Thread.Sleep(100);
            }
        }

        /*
         * Listens to incoming requests from a client and sends a response back if necessary
         * On disconnect remove client from active clients list
         */
        static void Work(TcpClient client)
        {
            while (client.Connected)
            {
                NetworkStream stream = client.GetStream();
                try
                {
                    dynamic request = Formatter.Deserialize(stream);
                    Response response = HandleRequest(request);
                    if(response != null)
                        Formatter.Serialize(stream, response);
                }
                catch(Exception ex)
                {
                    Console.Write(ex.Message);
                }
            }
            lock (_lock)
                Clients.Remove(client);
        }

        /*
         * Handles File Transfer Requests
         */
        static Response HandleRequest(FileTransferRequest request)
        {
            string file = request.File;

            FileTransferResponse response;
            if (File.Exists($"{Environment.CurrentDirectory}\\{file}"))
                response = new FileTransferResponse(File.ReadAllBytes(file));
            else
                response = new FileTransferResponse(null);

            return response;
        }
    }
}

客户代码

using System;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Global;
using System.IO;

namespace Client
{
    class Program
    {
        static TcpClient Server;
        static void Main(string[] args)
        {
            string file = args[0];
            string output = args[1];
            Init();

            NetworkStream stream = Server.GetStream();
            BinaryFormatter formatter = new BinaryFormatter();

            //Request
            FileTransferRequest request = new FileTransferRequest(file);
            formatter.Serialize(stream, request);

            //Get Response
            FileTransferResponse response = (FileTransferResponse) formatter.Deserialize(stream);

            if (response.File == null)
                Console.Write("File not found");
            else
            {
                File.WriteAllBytes(output, response.File);
                Console.Write("Success");
            }
            Console.ReadLine();
        }
        static void Init()
        {
            try
            {
                Server = new TcpClient("lordfrostbyte.webhop.me", 7777);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                Environment.Exit(-1);
            }
        }
    }
}

全球图书馆代码

namespace Global
{
    [Serializable]
    public abstract class Request
    {

    }
    [Serializable]
    public class FileTransferRequest : Request
    {
        public string File;
        public FileTransferRequest(string File)
        {
            this.File = File;
        }
    }
    [Serializable]
    public abstract class Response
    {

    }
    [Serializable]
    public class FileTransferResponse : Response
    {
        public byte[] File;
        public FileTransferResponse(byte[] File)
        {
            this.File = File;
        }
    }
}

这是我的代码......它不起作用! enter image description here

但我配置了端口转发功能 enter image description here

我也禁用了我的防火墙 enter image description here

我的猜测是Windows出了问题。但是我不想格式化我的电脑来解决这个问题。我非常感谢能够解决这个问题的任何意见。我有一段时间没有这个问题,我想知道是否有其他人有类似的问题,如果有人曾经解决过这个问题。目前我不知道如何解决这个问题。请注意,如果我在LAN上托管服务器并连接到LAN,它可以很好地工作。

1 个答案:

答案 0 :(得分:0)

我不得不重新安装pcap驱动程序。

相关问题