0

我正在使用 C# 为我正在开发的游戏创建一个套接字服务器。我可以连接到服务器,它似乎可以完美地接受连接。我遇到的唯一问题是我创建的表单没有显示出来。我正在使用该表单,因为我将对其进行管理控制,但它没有出现。到目前为止,我已经包含了我的代码。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainServer
{
    public partial class Form1 : Form
    {
        public static string data = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // Dns.GetHostName returns the name of the 
            // host running the application.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 6510);

            Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (true)
                {
                    serverLog.AppendText("Waiting for connection..");
                    // Program is suspended while waiting for an incoming connection.
                    Socket handler = listener.Accept();
                    data = null;
                }

            }
            catch (Exception a)
            {
                serverLog.AppendText(a.ToString());
            }
        }
    }
}
4

0 回答 0