P2P


michadaniel

Grünschnabel
Hallo,
Ich setze mich jetzt schon seit einiger Zeit mit dem Thema P2P auseinander. Und ich habe auch versucht, es umzusetzen.... hat aber nur Teilweise geklappt. Ich habe ein Projekt erstellt mit 1 Server und 1 Client.
Der Server läuft auf meinem Rechner, den ich mit NoIp mit einem Dns Namen ausgestattet habe. Außerdem werden alle Anfragen vom Router auf dem Port 50493 zu meinem Rechner weitergeleitet. Ich würd gerne wissen, ob ich das einigermaßen richtig gemacht habe. Oder zumindest einen Ansatz habe... Das ist das erste Mal, dass ich soetwas mache.

Server:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using P2P_Tools;

namespace P2P_Server
{
    class Program
    {
        private const int Port = 50493;

        private static IPEndPoint LocalEndPoint;

        private static TcpListener Server;

        private static Dictionary<IPEndPoint, NetworkStream> Dictionary;

        static void Main(string[] args)
        {
            try
            {
                Dictionary = new Dictionary<IPEndPoint, NetworkStream>();
                LocalEndPoint = new IPEndPoint(IPAddress.Parse(Tools.GetLocalIPAddress()), Port);
                Server = new TcpListener(LocalEndPoint);
                Server.Start();
                Console.WriteLine("Server started...");
                while (true)
                {
                    System.Threading.Thread.Sleep(10);
                    TcpClient client = Server.AcceptTcpClient();
                    Console.WriteLine("Client accepted!");
                    NetworkStream stream = client.GetStream();
                    IFormatter formatter = new BinaryFormatter();
                    RequestMessage clientRequestMessage = formatter.Deserialize(stream) as RequestMessage;
                    IPEndPoint remoteClientEndpoint = client.Client.RemoteEndPoint as IPEndPoint;
                    Dictionary.Add(remoteClientEndpoint, stream);
                    Console.WriteLine("Client registered");
                    foreach (KeyValuePair<IPEndPoint, NetworkStream> pair in Dictionary)
                    {
                        if (pair.Key.Address.Equals(clientRequestMessage.TargetAddress))
                        {
                            RequestAnswerMessage requestAnswerMessage = new RequestAnswerMessage(remoteClientEndpoint);
                            formatter.Serialize(pair.Value, requestAnswerMessage);
                            Console.WriteLine("Endpoint {0} sent to {1}", remoteClientEndpoint, pair.Key);

                            RequestAnswerMessage requestAnswerMessage2 = new RequestAnswerMessage(pair.Key);
                            formatter.Serialize(stream, requestAnswerMessage2);
                            Console.WriteLine("Endpoint {0} sent to {1}", pair.Key, remoteClientEndpoint);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.ReadKey();
            }
        }
    }
}
Client:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using P2P_Tools;

namespace P2P_Client
{
    class Program
    {
        private const int Port = 50493;
        private const string ServerDnsName = "michadaniel4.ddns.net";

        private static IPEndPoint ServerEndPoint;
        private static IPEndPoint TargetRemoteEndPoint;
        private static IPEndPoint LocalEndPoint;

        private static TcpClient Client;
        private static TcpListener Server;

        static void Main(string[] args)
        {
            try
            {
                Client = new TcpClient();
                ServerEndPoint = new IPEndPoint(Dns.GetHostAddresses(ServerDnsName)[0], Port);
                Console.WriteLine("Client started...");
                Client.Connect(ServerEndPoint);
                Console.WriteLine("Successfully connected to the server!");
                NetworkStream stream = Client.GetStream();
                Console.Write("Please enter your Target Address: ");
                RequestMessage requestMessage = new RequestMessage(IPAddress.Parse(Console.ReadLine()));
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, requestMessage);
                RequestAnswerMessage requestAnswerMessage = formatter.Deserialize(stream) as RequestAnswerMessage;
                TargetRemoteEndPoint = requestAnswerMessage.TargetRemoteEndPoint;
                Console.WriteLine("Your Target's Remote Endpoint: {0}", TargetRemoteEndPoint);
                LocalEndPoint = new IPEndPoint(IPAddress.Parse(Tools.GetLocalIPAddress()), Port);
                //stream.Close();
                Console.Write("Are you the Server? (Y/N): ");
                if (Console.ReadLine().Equals("Y"))
                {
                    Client.Client.Shutdown(SocketShutdown.Both);
                    Client.Client.Close();
                    Server = new TcpListener(LocalEndPoint);
                    Server.Start();
                    Console.WriteLine("Server started...");
                    while (true)
                    {
                        System.Threading.Thread.Sleep(10);
                        TcpClient client = Server.AcceptTcpClient();
                        Console.WriteLine(client.Client.RemoteEndPoint);
                    }
                }
                else if (Console.ReadLine().Equals("N"))
                {
                    Client.Connect(TargetRemoteEndPoint);
                    Console.WriteLine("Connect with your Target...");
                }
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.ReadKey();
            }
        }
    }
}
Tools:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace P2P_Tools
{
    public class Tools
    {
        public static string GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            throw new Exception("Local IP Address Not Found!");
        }
    }

    [Serializable]
    public class RequestMessage
    {
        public IPAddress TargetAddress;

        public RequestMessage(IPAddress TargetAddress)
        {
            this.TargetAddress = TargetAddress;
        }
    }

    [Serializable]
    public class RequestAnswerMessage
    {
        public IPEndPoint TargetRemoteEndPoint;

        public RequestAnswerMessage(IPEndPoint TargetRemoteEndPoint)
        {
            this.TargetRemoteEndPoint = TargetRemoteEndPoint;
        }
    }
}