如何修改本地主机C#服务器以在专用服务器上工作?

时间:2019-04-29 00:50:27

标签: c# networking server

我目前处于游戏困境中,我们必须构建一个多用户地牢。在遵循一个不错的示例https://www.geeksforgeeks.org/socket-programming-in-c-sharp/之后,我已经拥有了本地主机服务器和客户端。多亏了游戏果酱的拥有者,我还获得了Digital Ocean上几个月的免费托管。所以我想知道如何修改代码以在Digital Ocean的服务器上工作。

这是代码,这又不是我的代码,我从上面的链接开始关注,我将其集成到我将要构建的ascii游戏中

服务器代码:

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

namespace Network_Test_server
{
class Program
{
    static void Main(string[] args)
    {
        ExecuteServer();
    }

    public static void ExecuteServer()
    {
        IPHostEntry iPHost = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress iPAddress = iPHost.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(iPAddress, 11111);

        Socket listener = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        try { 
        listener.Bind(localEndPoint);
        listener.Listen(10);

        while (true)
        {
            Console.WriteLine("Waiting connection...");
            Socket clientSocket = listener.Accept();

            //data buffer
            byte[] bytes = new Byte[1024];
            string data = null;

            while (true)
            {
                int numByte = clientSocket.Receive(bytes);
                data += Encoding.ASCII.GetString(bytes, 0, numByte);
                if (data.IndexOf("<EOF>") > -1) break;
            }
            Console.WriteLine("Text received -> {0} ", data);

            //sends data back to client  
            byte[] message = Encoding.ASCII.GetBytes("Test Server");
            clientSocket.Send(message);

            clientSocket.Shutdown(SocketShutdown.Both);
            clientSocket.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}
}

这是客户代码

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

namespace Network_Test_Client
{
class Program
{
    static void Main(string[] args)
    {
        ExecuteClient();
    }

    static void ExecuteClient()
    {
        try
        {
            IPHostEntry iPHost = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress iPAddress = iPHost.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(iPAddress, 
         11111);

            Socket sender = new Socket(iPAddress.AddressFamily, 
         SocketType.Stream, ProtocolType.Tcp);

            try
            {
                sender.Connect(localEndPoint);

                Console.WriteLine("Socket connected to -> {0}", sender.RemoteEndPoint.ToString());

                //send to server
                byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
                int byteSent = sender.Send(messageSent);

                //data buffer
                byte[] messageReceived = new byte[1024];

                int byteRecv = sender.Receive(messageReceived);
                Console.WriteLine("Message from Server -> {0}", Encoding.ASCII.GetString(messageReceived, 0, byteRecv));

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (AggregateException 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());
        }

    }

}


}

0 个答案:

没有答案