Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

TCP Server - Rozlaczanie clienta - jak naprawic?


Rekomendowane odpowiedzi

Opublikowano
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.IO;
using ServerData;
using System.Threading;

namespace Server
{
    class Server
    {
        static Socket listenerSocket;
        static List<ClientData> _clients;

        static void Main(string[] args)
        {
            Console.WriteLine("Starting server on " + Packet.GetIP4Address());
            listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _clients = new List<ClientData>();

            IPEndPoint IP = new IPEndPoint(IPAddress.Parse(Packet.GetIP4Address()), 1337);
            listenerSocket.Bind(IP);

            Thread listenThread = new Thread(ListenThread);

            listenThread.Start();
        }

        static void ListenThread()
        {
            for (; ; )
            {
                listenerSocket.Listen(0);
                _clients.Add(new ClientData(listenerSocket.Accept()));
            }

        }

        public static void Data_IN(object cSocket)
        {
            Socket clientSocket = (Socket)cSocket;
            byte[] Buffer;
            int readBytes;
            for (; ; )
            {
                try
                {
                    Buffer = new byte[clientSocket.SendBufferSize];

                    readBytes = clientSocket.Receive(Buffer);

                    if (readBytes > 0)
                    {
                        Packet packet = new Packet(Buffer);
                        DataManager(packet);
                    }
                }
                catch(SocketException)
                {
                    Console.WriteLine("Client disconnected!");
                }
            }
        }

        public static void DataManager(Packet p)
        {
            switch (p.packetType)
            {
                case PacketType.Chat:
                    foreach (ClientData c in _clients)
                        c.clientSocket.Send(p.ToBytes());
                    
                    break;
            }
        }
        // Listener - Listens for clients trying to connect
        // ClientData Thread - receives data from each client individually
        // Data Manager 

    }

    class ClientData
    {
        public Socket clientSocket;
        public Thread clientThread;
        public string id;


        public ClientData()
        {
            id = Guid.NewGuid().ToString();
            clientThread = new Thread(Server.Data_IN);
            clientThread.Start(clientSocket);
            SendRegistrationPacket();
        }

        public ClientData(Socket clientSocket)
        {
            this.clientSocket = clientSocket;
            id = Guid.NewGuid().ToString();
            clientThread = new Thread(Server.Data_IN);
            clientThread.Start(clientSocket);
            SendRegistrationPacket();
        }

        public void SendRegistrationPacket()
        {
            Packet p = new Packet(PacketType.Registration, "server");
            p.gData.Add(id);
            clientSocket.Send(p.ToBytes());
        }
    }
}

a problem tkwi tutaj:

                catch(SocketException)
                {
                    Console.WriteLine("Client disconnected!");
                }

przy lapaniu tego, spamuje w nieskoncozosc Client disconnected i nie odbiera pakietow od nowych / reszty clientow.

Hello there.

Opublikowano

 

   catch(SocketException)
                {
                    Console.WriteLine("Client disconnected!");
                    break;
                }

3053080006.png

Opublikowano
   catch(SocketException)
                {
                    Console.WriteLine("Client disconnected!");
                    break;
                }

to nie dziala, na sam rozum tez to zrobilem.

Hello there.

Opublikowano

podaj tez kod clienta

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerData;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace Client
{
    class Client
    {
        public static Socket master;
        public static string name;
        public static string id;

        static void Main(string[] args)
        {
            Console.WriteLine("Name: ");
            name = Console.ReadLine();
        A:  Console.Clear();
            Console.WriteLine("IP: ");
            string IP = Console.ReadLine();
            Console.WriteLine("Port: ");
            int Port = Convert.ToInt32(Console.ReadLine());
            master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint IPe = new IPEndPoint(IPAddress.Parse(IP), Port);
            try
            {
                master.Connect(IPe);
            }
            catch
            {
                Console.WriteLine("Could not connect to host!");
                Thread.Sleep(2000);
                goto A;
            }

            Thread t = new Thread(Data_IN);
            t.Start();

            for (; ; )
            {
                string input = Console.ReadLine();
                if (input == "dc")
                {
                    master.Disconnect(false);
                }
                Packet p = new Packet(PacketType.Chat, id);
                p.gData.Add(name);
                p.gData.Add(input);
                master.Send(p.ToBytes());
            }

        }

        static void Data_IN()
        {
            byte[] Buffer;
            int readBytes;

            for (; ; )
            {
                try
                {
                    Buffer = new byte[master.SendBufferSize];
                    readBytes = master.Receive(Buffer);

                    if (readBytes > 0)
                    {
                        DataManager(new Packet(Buffer));
                    }
                }
                catch (SocketException ex)
                {
                    Console.WriteLine("Server Lost!");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
            }
        }

        static void DataManager(Packet p)
        {
            switch (p.packetType)
            {
                case PacketType.Registration:
                    Console.WriteLine("Connected!");
                    id = p.gData[0];
                    break;
                case PacketType.Chat:
                    ConsoleColor c = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(p.gData[0] + ": " + p.gData[1]);
                    Console.ForegroundColor = c;
                    break;
            }
        }
    }
}

Hello there.

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...