2015-06-28 57 views
1

我已經與Windows Forms Application進行了簡單的聊天。我使用套接字,當我嘗試連接本地IP時,一切正常,我可以在本地發送消息。C#。通過套接字和外部IP連接到好友

但是當我試圖連接到我的朋友機器與外部IP沒有任何反應。我在IP字段中輸入他的外部IP,按下連接並且程序已停止工作。

問題是:我在IP領域寫什麼?我需要更多信息來連接我的朋友的機器嗎?我是網絡編程的初學者,請幫助我。

如果可以的話,請給我一個關於C#網絡編程的好書。

這裏是我的windorm圖片: ChatPicture

這裏是我的代碼:

namespace Client 
{ 
    public partial class Client : Form 
    { 
     public Socket ServerSocket, ClientSocket, ClientSocket2; 
     byte[] Buffer; 

     public Client() 
     { 
      InitializeComponent(); 
      StartConnect(); 
     } 

     private void StartConnect() 
     { 
      ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      ServerSocket.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(textBox2.Text))); 
      ServerSocket.Listen(0); 

      ServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); 
     } 

     private void AcceptCallback(IAsyncResult ar) 
     { 
      ClientSocket2 = ServerSocket.EndAccept(ar); 
      Buffer = new byte[ClientSocket.SendBufferSize]; 
      ClientSocket2.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); 
     } 

     private void ReceiveCallback(IAsyncResult ar) 
     { 
      ClientSocket2.EndReceive(ar); 
      string Text = Encoding.ASCII.GetString(Buffer); 
      AppendRichTextBox(Text); 

      ClientSocket2.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); 
     } 

     private void AppendRichTextBox(string Text) 
     { 
      MethodInvoker Invoker = new MethodInvoker(delegate 
      { 
       richTextBox2.Text += "Client says: " + Text; 
      }); 

      this.Invoke(Invoker); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

      ClientSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(textBox2.Text)), new AsyncCallback(ConnectCallback), null); 
     } 

     private void ConnectCallback(IAsyncResult ar) 
     { 
      button2.Enabled = true; 
      ClientSocket.EndConnect(ar); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Buffer = Encoding.ASCII.GetBytes(richTextBox1.Text); 
      ClientSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null); 
      richTextBox1.Clear(); 
     } 

     private void SendCallback(IAsyncResult ar) 
     { 
      ClientSocket.EndSend(ar); 
     } 

     private void Client_KeyPress(object sender, KeyPressEventArgs e) 
     { 
     } 

     private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if(e.KeyChar==(char)13) 
      { 
       button2_Click(sender, (EventArgs)e); 
      } 
     } 

     private void Client_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 
+1

您是否檢查過遠程端口是否打開? –

回答

1

你需要打開你的朋友的電腦防火牆的遠程端口。

+0

好的,你能說我如何打開遠程端口? –

+0

按照這個步驟[http://www.sevenforums.com/tutorials/542-windows-firewall-add-remove-exception.html](http://www.sevenforums.com/tutorials/542-windows-firewall-附加刪除-exception.html)。 – Carlos

+0

謝謝你的幫助 –