2012-01-06 5 views
0

私のアプリは特定のポートでソケットメッセージを待ち受けます。私はそれがコマンドラインで "netstat -a"を介して聴いているのを見ることができます。私のアプリがシャットダウンしてもポートがまだ聞こえているのは悪いジュジュですか?

私はアプリをシャットダウンし、私は再実行マシンがまだそのポートで待機している「netstatコマンド-a」

は、これは問題ですか?

私はその後、再びアプリケーションを起動すると、それは恐ろしくクラッシュします。

聞き取りを停止させるにはどうすればよいですか?

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

namespace testSocketSendAndReceive_Nutshell 
{ 
    public partial class Form1 : Form 
    { 
     string sJerrysIPAddr = "10.24.93.110"; 
     string sMyIPAddr = "10.24.93.128"; 
     string sThisAppFileName = string.Empty; 
     bool bThisInstanceFunctionsAsServer = false; 

     internal static Form1 MainSocketPairForm = null; 

     public Form1() 
     { 
      InitializeComponent(); 
      MainSocketPairForm = this; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      sThisAppFileName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; // This provides just the app name, appending ".vshost" but NOT ".exe" (testSocketSendAndReceive_Nutshell.vshost) 
      lblFileName.Text = sThisAppFileName; 

      // Client and Server code are here combined in one app; however, we want each instance to run as 
      // just one or the other, so (the .exe functioning as a Server should be renamed with the subString 
      // "Server" somewhere in the filename): 
      bThisInstanceFunctionsAsServer = sThisAppFileName.Contains("Server"); 
      if (bThisInstanceFunctionsAsServer) 
      { 
       new Thread(Server).Start();  // Run server method concurrently. 
       Thread.Sleep(500);    // Give server time to start. 
      } 
      btnSendMsg.Visible = !bThisInstanceFunctionsAsServer; 
      textBox1.Visible = !bThisInstanceFunctionsAsServer; 
     } 

     static void Client() 
     { 
      using (TcpClient client = new TcpClient(Form1.MainSocketPairForm.sJerrysIPAddr, 51111)) // err here second time around 
      using (NetworkStream n = client.GetStream()) 
      { 
       BinaryWriter w = new BinaryWriter(n); 
       w.Write(Form1.MainSocketPairForm.textBox1.Text.ToString()); 
       w.Flush(); 
       Form1.MainSocketPairForm.label1.Text = new BinaryReader(n).ReadString(); 
      } 
     } 

     static void Server()  
     { 
      TcpListener listener = new TcpListener(IPAddress.Any, 51111); 
      listener.Start(); 
      var shouldExit = false; 
      while (!shouldExit) 
       using (TcpClient c = listener.AcceptTcpClient()) 
       { 
        using (NetworkStream n = c.GetStream()) 
        { 
         string msg = new BinaryReader(n).ReadString(); 
         if (msg == "exit") 
          // Client told us to exit... 
          shouldExit = true; 
         BinaryWriter w = new BinaryWriter(n); 
         w.Write(msg + " back atcha!"); 
         w.Flush(); // Must call Flush because we're not disposing the writer. 
        } 
       } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Client(); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 
    } 
} 
+0

あなたは一例on..for聴いているかのポート..そして、それはあなたがそれがポートのbecasueだそう確信していますどのようにクラッシュしたとき..あなたはそれのコードの例を貼り付けることができますのForm_Loadでこの男は試してみてください時々クラッシュ.. – MethodMan

+0

はい、それはあなたが言及した理由で問題です。 2回目にアプリケーションを起動すると、そのip/portはすでに使用中です。あなたはDispose()、Close()、およびあなたがすべきその他のクリーンアップメソッドを呼び出していますか? – user1231231412

+0

いくつかのコードを掲載して原因を特定する手助けをすることができますか? –

答えて

3

あなたのアプリケーションは実際には終了していない可能性があります(あなたの.exeのタスクマネージャの「プロセス」タブを確認してください)。

おそらく、コマンドウィンドウを閉じるだけでアプリケーションを終了しようとしている可能性があります。あなたのサーバスレッドはbackground threadではないので、実行し続けるだけです。

if (bThisInstanceFunctionsAsServer) 
     { 
      var serverThread = new Thread(Server); 
      serverThread.IsBackground = true; // Make sure the server thread doesn't keep the app running in the background 
      serverThread.Start();  // Run server method concurrently. 
      Thread.Sleep(500);    // Give server time to start. 
     } 
+0

あなたの前提に常に疑問を呈しています。それは私がこの答えについて最も気に入っていることです。あなたが想定していることが起こっているか正しく設定されているか、さもなければそうでなければならないものがそうではありません。 –

関連する問題