C#是否可以通过1个套接字发送许多消息? 2019年

时间:2019-12-16 09:35:03

标签: c# sockets tcp ip client

在C-Sharp [C#]中,TCP / IP套接字存在很大的问题。我不明白为什么我只能通过套接字发送1条消息,而彼此之间却不能发送更多消息。 客户端一次应该发送许多消息。 我了解错了什么?

public class SocketClient
{

public static int Main(String[] args)
{


    StartClient();

    Console.ReadKey();

    return 0;
}

public static void StartClient()
{

//buffer
        byte[] bytes = new byte[1024];
//Messages which i want send
        byte[] aw = Encoding.UTF8.GetBytes("-AW-");
        byte[] verb = Encoding.UTF8.GetBytes("-VERB-");

    try
    {

        string HostName = Dns.GetHostName();
        IPHostEntry hostInfo = Dns.GetHostEntry(HostName);
        IPAddress IpAdresse = hostInfo.AddressList[1];
        Console.WriteLine("Waiting for Server Connection");
        // Connect to a Remote server  
        // Get Host IP Address that is used to establish a connection  
        // In this case, we get one IP address of localhost that is IP : 172.XX.XX.XX
        // If a host has multiple addresses, you will get a list of addresses  
        IPAddress ipAddress = IPAddress.Parse("172.xx.xx.xx");
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 62000);
        Socket sender = new Socket(ipAddress.AddressFamily,
    SocketType.Stream, ProtocolType.Tcp);

        // Create a TCP/IP  socket.    

        // Connect the socket to the remote endpoint. Catch any errors.    
        try

        {

            sender.Connect(remoteEP);
            Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
            // Text der geschickt wird
            Console.WriteLine("______________________________________________________");
            Console.WriteLine("{[-Server Online-]}");
            Console.WriteLine("______________________________________________________");


            // Send the data through the socket.  
            int bytesSent = sender.Send(aw);
            int bytesRec = sender.Receive(bytes);




            // Receive the response from the remote device.    


            //sender.Send(Encoding.UTF8.GetBytes(text));
            //Verb
            int bytesverb = sender.Send(verb);

            int byteRecv = sender.Receive(bytes);






            Console.WriteLine("Your answer is = {0}", Encoding.UTF8.GetString(bytes, 0, bytesRec));




            ///////////////////////////////////////
            //Ping for keep-alive

            //PingverzoegerungAsync();



        }
        catch (ArgumentNullException ane)
        {
            Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
        }
        catch (SocketException se)
        {
            Console.WriteLine("SocketException : {0}", se.ToString());
        }
        catch (Exception e)
        {
            Console.WriteLine("Unexpected exception : {0}", e.ToString());
        }

    }

    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        Console.WriteLine("Fehlgeschlagen");
    }

}

private static void SetKeepAliveValues(Socket socket, bool v1, int v2, int v3)
{
    throw new NotImplementedException();
}

private static async Task PingverzoegerungAsync()
{

    Stopwatch timer = Stopwatch.StartNew();

    try
    {


        while (true)
        {

            Ping myping = new Ping();
            await Task.Delay(5000);

            try
            {
                PingReply reply = myping.Send("172.xxx.xxx.xx", 10000);
                if (reply.Status == IPStatus.Success)
                {
                    Console.WriteLine("Zusammenfassung");
                    Console.WriteLine("Status :  " + reply.Status + " \n Time : " + reply.RoundtripTime.ToString() + " \n Address : " + reply.Address);
                }
                else
                {
                    Console.WriteLine("Connection Fail, Server Adresse nicht erreichbar");
                }
            }
            catch
            {
                throw new Exception();
            }
        }
    }
    catch (PingException ex)
    {
        Console.WriteLine("Error : Timeout Probleme ");
        Console.WriteLine(ex);
    }
    Console.ReadKey();
}





}

这是我的客户代码。任何建议如何解决我的问题?还是我可能会误解tcp / ip套接字的问题?

0 个答案:

没有答案
相关问题