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 + Multiple Clients C#


Rekomendowane odpowiedzi

Opublikowano

Witam.

Mam problem z tym że client łączy się cały czas z serwerem.
SS:
http://screenshot.sh/ofn8VcOQj10Sg

 

Client:

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace xxx
{
    class Client
    {
        public void StartClient(string ip)
        {
            try
            {
                while (true)
                {
                    TcpClient tcpclnt = new TcpClient();
                    Console.WriteLine("Connecting.....");

                    tcpclnt.Connect(ip, 8898);

                    Console.WriteLine("Connected");
                    Console.Write("Enter the string to be transmitted : ");

                    String str = Console.ReadLine();
                    Stream stm = tcpclnt.GetStream();

                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes(str);
                    Console.WriteLine("Transmitting.....");

                    stm.Write(ba, 0, ba.Length);

                    byte[] bb = new byte[100];
                    int k = stm.Read(bb, 0, 100);

                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(bb[i]));

                    tcpclnt.Close();
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}

 

 

 

Serwer:

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace xxx
{
    class Server
    {
        public void StartServer(string ip)
        {
            try
            {
                while (true)
                {
                    IPAddress ipAd = IPAddress.Parse(ip);
                    TcpListener myList = new TcpListener(ipAd, 8898);
                    myList.Start();
                    Console.WriteLine("The server is running at port 8898...");
                    Console.WriteLine("The local End point is  :" +
                    myList.LocalEndpoint);
                    Console.WriteLine("Waiting for a connection.....");
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    byte[] b = new byte[100];
                    int k = s.Receive(;
                    Console.WriteLine("Recieved...");
                    string Command = string.Empty;
                    for (int i = 0; i < k; i++)
                    {
                        Command = Command + Convert.ToChar(b[i]);
                    }
                    Console.WriteLine(Command);
                    ASCIIEncoding asen = new ASCIIEncoding();
                    s.Send(asen.GetBytes("The string was recieved by the server."));
                    Console.WriteLine("\nSent Acknowledgement");
                    s.Close();
                    myList.Stop();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }  
        }
    }
}

 

 

 

+ do tego jak chcę połączyć kolejny client to mam cały czas Transmitting.

http://screenshot.sh/oAe5Bo4OUyDoU

Opublikowano

masz wszystko w pętli while i żadnego ifa, który sprawdza, czy się połączył już z serverem. Weź na końcu pętli daj breake i na teraz możesz to mieć w ten sposób, ale powinieneś pomyśleć o jakimś ifie, który sprawdzi czy się połączył, jeśli tak, to breake.

Opublikowano

Nie wiem czy oto chodziło ale teraz cały czas łączy ... (http://screenshot.sh/m8Us31J953tQ2)

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace xxx
{
    class Client
    {
        public void StartClient(string ip)
        {
            try
            {
                while (true)
                {
                    TcpClient tcpclnt = new TcpClient();
                    if (tcpclnt.Connected)
                    {
                        Console.Write("Enter the string to be transmitted : ");

                        String str = Console.ReadLine();
                        Stream stm = tcpclnt.GetStream();

                        ASCIIEncoding asen = new ASCIIEncoding();
                        byte[] ba = asen.GetBytes(str);
                        Console.WriteLine("Transmitting.....");

                        stm.Write(ba, 0, ba.Length);

                        byte[] bb = new byte[100];
                        int k = stm.Read(bb, 0, 100);

                        for (int i = 0; i < k; i++)
                            Console.Write(Convert.ToChar(bb[i]));
                    }
                    else
                    {
                        Console.WriteLine("Connecting.....");

                        tcpclnt.Connect(ip, 8898);

                        Console.WriteLine("Connected");
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}

 

 

Opublikowano

Masz to w pętli While, która ciągle jest true, musisz ją przerwać komendą break.

Coś w ten deseń.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace xxx
{
    class Client
    {
        public void StartClient(string ip)
        {
            try
            {
                while (true)
                {
                    TcpClient tcpclnt = new TcpClient();
                    Console.WriteLine("Connecting.....");

                    tcpclnt.Connect(ip, 8898);

                    Console.WriteLine("Connected");
                    Console.Write("Enter the string to be transmitted : ");

                    String str = Console.ReadLine();
                    Stream stm = tcpclnt.GetStream();

                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes(str);
                    Console.WriteLine("Transmitting.....");

                    stm.Write(ba, 0, ba.Length);

                    byte[] bb = new byte[100];
                    int k = stm.Read(bb, 0, 100);

                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(bb[i]));

                    tcpclnt.Close();
                    break;
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}
Opublikowano

chyba raczej tak, miałeś problem z tym, że się cały czas łączy, to w ten sposób wykona się całość jeden raz. Jeśli chcesz, żebyś mógł wysyłać więcej wiadomości, to w pętli while zostawiasz jedynie wysyłanie, a połączenie wywalasz z niej i break nie jest potrzebny.

Opublikowano

Mi chodziło że cały czas miałem Connected zamiast tylko wiadomości
tak samo przy odbiorze wiadomości od serwera cały czas łączyło z nim zamiast raz połączyć

i to by wystarczyło bo client byłby cały czas połączony a nie go rozłącza i połącza na nowo ..

Opublikowano

Client

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;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            string ip = "192.168.1.2";
            Console.WriteLine("Client");
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");

                tcpclnt.Connect(ip, 8898);

                Console.WriteLine("Connected");
                while (true)
                {
                    
                   
                        Console.Write("Enter the string to be transmitted : ");

                        String str = Console.ReadLine();
                        Stream stm = tcpclnt.GetStream();

                        ASCIIEncoding asen = new ASCIIEncoding();
                        byte[] ba = asen.GetBytes(str);
                        Console.Write("Transmitting.....");

                        stm.Write(ba, 0, ba.Length);

                        byte[] bb = new byte[100];
                        int k = stm.Read(bb, 0, 100);

                        for (int i = 0; i < k; i++)
                            Console.Write(Convert.ToChar(bb[i]));

                        
                    }
                tcpclnt.Close();
            }
            

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
            Console.ReadKey();
        }
    }
}

Server

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.Diagnostics;
using System.Runtime.InteropServices;

namespace server
{
    class Program
    {
        static void Main(string[] args)
        {
            string ip = "192.168.1.2";
            Console.WriteLine("serwer");
            try
            {
                IPAddress ipAd = IPAddress.Parse(ip);
                TcpListener myList = new TcpListener(ipAd, 8898);
                myList.Start();
                Console.WriteLine("The server is running at port 8898...");
                Console.WriteLine("The local End point is  :" +
                myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");
                Socket s = myList.AcceptSocket();
                while (true)
                {
                    
                    
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    byte[] b = new byte[100];
                    int k = s.Receive(;
                    Console.WriteLine("Recieved...");
                    string Command = string.Empty;
                    for (int i = 0; i < k; i++)
                    {
                        Command = Command + Convert.ToChar(b[i]);
                    }
                    Console.WriteLine(Command);
                    ASCIIEncoding asen = new ASCIIEncoding();
                    s.Send(asen.GetBytes("The string was recieved by the server. \n"));
                    Console.WriteLine("\nSent Acknowledgement");
                    
                }
                s.Close();
                myList.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
            Console.ReadKey();
        }
    }
}

Musisz teraz zrobić jeszcze oddzielny wątek do nasłuchiwania połączeń, bo w ten sposób połączy się tylko jeden klient. Chyba, że ma się tylko jeden klient łączyć, to masz gotowe.

Opublikowano

Nie zrobię za Ciebie całego programu ;) Zrobiłem Ci to z czym miałeś problem, napisałem, że tylko jeden klient w ten sposób się połączy, a za pomoc nie usłyszałem, słowa dziękuję ;)

Szukaj u wuja google informacji o wątkach, poczytaj o tym troszkę i na pewno dasz rade to zrobić ;)

Zarchiwizowany

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

×
×
  • Dodaj nową pozycję...