C#服务器和客户端无法正常工作

时间:2016-02-03 04:35:56

标签: c# server client

我一直在制作游戏的C#服务器和客户端。我已经使用了很多资源来尝试创造成功的东西。到目前为止,客户端可以将内容发送到服务器,服务器可以接收它们,但我希望能够将内容发送到客户端。此外,我不知道发生了什么,但我的服务器代码突然停止打印到控制台或其他东西。我是新手,需要任何帮助。我认为我对客户端和服务器进行了编程,以便能够发送和接收,但有些事情是对的...

注意:我正在使用运行旧版本.NET的cmd编译器(但代码仍然编译),任何有关使Visual Studio工作以构建/编译代码的帮助也会很好:)

客户端:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class Program
{

    static Boolean done = false;

    static void Main(string[] args)
    {

        Boolean exception_thrown = false;
        Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
        ProtocolType.Udp);

        IPAddress send_to_address = IPAddress.Parse("ip-goes-here");
        IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 27020);

        Console.WriteLine("Enter text to broadcast via UDP.");
        Console.WriteLine("Enter a blank line to exit the program.");

        Thread listenerT = new Thread(new ThreadStart(Program.listener));
        listenerT.Start();

        while (!done)
        {
            Console.WriteLine("Enter text to send, blank line to quit");
            string text_to_send = Console.ReadLine();

            if (text_to_send.Length == 0)
            {
                done = true;
                listenerT.Abort();
            }
            else
            {
                byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);

                Console.WriteLine("sending to address: {0} port: {1}",
                        sending_end_point.Address,
                        sending_end_point.Port);

                try
                {
                    sending_socket.SendTo(send_buffer, sending_end_point);
                }
                catch (Exception send_exception)
                {
                    exception_thrown = true;
                    Console.WriteLine(" Exception {0}", send_exception.Message);
                }

                if (exception_thrown == false)
                {
                    Console.WriteLine("Message has been sent to the broadcast address");
                }
                else
                {
                    exception_thrown = false;
                    Console.WriteLine("The exception indicates the message was not sent.");
                }
            }
        }
    }

    static void listener()
    {

        int listenPort = 27020;
        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
        string received_data;
        byte[] receive_byte_array;

        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                receive_byte_array = listener.Receive(ref groupEP);
                Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
                received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
                Console.WriteLine("data follows \n{0}\n\n", received_data);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}

服务器端:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class UDPListener
{

    private const int listenPort = 27020;
    //static Boolean done = false;
    static IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
    static UdpClient udpServer = new UdpClient(27020);
    static IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 27020); 
    static byte[] data = udpServer.Receive(ref remoteEP); // listen on port 11000
    static IPAddress send_to_address = IPAddress.Parse("10.240.0.1");
    static IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 27020);

    public static void Main()
    {

        Console.WriteLine("Begin listening sequence");

        while (true)
        {

            data = udpServer.Receive(ref groupEP);
            Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
            send_to_address = IPAddress.Parse(groupEP.ToString());
            Thread listenerT = new Thread(new ThreadStart(UDPListener.sender));
            listenerT.Start();
            String received_data = Encoding.ASCII.GetString(data, 0, data.Length);
            Console.WriteLine(received_data);
        }
    }

    static void sender()
    {

        while (true)
        {
            Console.WriteLine("Enter text to send, blank line to quit");
            string text_to_send = Console.ReadLine();
            if (text_to_send.Length == 0)
            {
                //done = true;
            }
            else
            {
                byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
                Console.WriteLine("sending to address: {0} port: {1}",
                sending_end_point.Address,
                sending_end_point.Port);
                udpServer.Send(send_buffer, 0, remoteEP); // reply back
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没关系,我的一个朋友想出来了。你可以在一个名为flaming leaf server

之类的github repo中找到最终代码