c # - Uopløselig årsag bag Connection Timeout

Indlæg af Hanne Mølgaard Plasc

Problem



Server kode


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;
        }
    }
}


Klientkode


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);
            }
        }
    }
}


Global Library Code


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;
        }
    }
}


Dette er min kode ... Det virker ikke!
Indtast billedbeskrivelse her [4]


Men jeg har konfigureret port videresendelse
Indtast billedbeskrivelse her [5]


Jeg har også deaktiveret min firewall
Indtast billedbeskrivelse her [6]


Mit gæt er, at der er noget galt med windows. Men jeg vil ikke formatere min computer for at løse dette. Jeg vil meget sætte pris på noget input, der ville føre til løsning af dette problem. Jeg har haft dette problem et stykke tid, og jeg spekulerede på, om nogen andre har haft et lignende problem, og hvis nogen nogensinde har løst dette. I øjeblikket har jeg ingen anelse om, hvordan du endda fejlfinding dette problem. Bemærk, at hvis jeg er vært for serveren på LAN og opretter forbindelse til LAN, fungerer det perfekt.

Bedste reference


Jeg var nødt til at geninstallere pcap drivere.