我为服务器创建了一个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
}
}
答案 0 :(得分:0)
当您使用错误的项目模板开始时,通常会发生这种情况。它是可以修复的。使用Project + Properties,Application选项卡。将“输出类型”设置从控制台应用程序更改为Windows应用程序。没有更多的控制台。