2013-08-31 63 views
0

我爲服務器創建了一個gui,但是每當我啓動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

這些被稱爲主機終端,你是否在通常在終端運行的應用程序中創建GUI?顯示一些代碼,也許?還有一些細節也請。 –

+0

這是什麼類型的應用程序?在設置中的控制檯或GUI? –

+0

其gui不是控制檯 – prattom

回答

0

這通常發生在您開始使用錯誤的項目模板時。它是可以修復的。使用項目+屬性,應用程序選項卡。將「輸出類型」設置從控制檯應用程序更改爲Windows應用程序。沒有更多的主機。

+0

感謝您的解決方案 – prattom