我已经为服务器创建了一个 gui,但是每当我启动 gui 时,控制台窗口也会打开,如果我关闭控制台窗口,gui 也会关闭。控制台窗口没有做任何事情,它只是在我启动我的 gui 时打开。我怎样才能避免这种情况?我只想启动 gui 而不是控制台窗口。
namespace ServerUI
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
//default port number
textBox1.Text = "8001";
button1.Click += button1_Click;
}
//Global Variables
public static class Globals
{
public const string IP = "127.0.0.1";
public static int port_number = 0;
public static string selected = "";
public static string selected_1 = "";
}
//IP address of server
static IPAddress ipAddress = IPAddress.Parse(Globals.IP);
//List of active clients connected to server
List<Socket> active_clients = new List<Socket>();
static Socket serverSocket;
static byte[] buffer = new Byte[1024];
public static ManualResetEvent allDone = new ManualResetEvent(false);
private void button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text.Trim()))
{
System.Windows.Forms.MessageBox.Show("Port Number Empty", "Error");
return;
}
else
{
bool check = int.TryParse(textBox1.Text, out Globals.port_number);
if (!check)
{
MessageBox.Show("Port Number not in correct format. Enter Port Number again", "Error");
return;
}
client_pkt_parsing.client_list.Clear();
foreach (DataGridViewRow myRow in dataGridView1.Rows)
{
myRow.Cells[1].Value = null; // assuming you want to clear the first column
}
foreach (DataGridViewRow myRow in dataGridView2.Rows)
{
myRow.Cells[1].Value = null; // assuming you want to clear the first column
}
//Starting Server
StartServer();
}
} //end of button1_Click
public void StartServer()
{
byte[] bytes = new Byte[1024];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Globals.port_number);
try
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(10);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
infoBox.Text = "Server started. Waiting for a connection...";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}//end of StartServer
public void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
try
{
Socket clientSocket = serverSocket.EndAccept(ar);
infoBox.Text = infoBox.Text + "\r\n" + string.Format(DateTime.Now.ToString("HH:mm:ss") + " > Client : " + clientSocket.RemoteEndPoint.ToString() + "connected");
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), clientSocket);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch (Exception ex)
{
}
}//end of AcceptCallback
public void ReceiveCallback(IAsyncResult ar)
{
try
{
int received = 0;
Socket current = (Socket)ar.AsyncState;
received = current.EndReceive(ar);
byte[] data = new byte[received];
if (received == 0)
{
return;
}
Array.Copy(buffer, data, received);
string text = Encoding.ASCII.GetString(data);
testBox.Text = testBox.Text + "\r\n" + text;
//Send(current, "hello");
buffer = null;
Array.Resize(ref buffer, current.ReceiveBufferSize);
current.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
}
catch (Exception ex)
{
}
}//end of RecieveCallback
}
}