2012-04-23 79 views
0

我做聊天與插座的應用程序: 服務器創建一個套接字連接,並從任何客戶端 等待消息服務器:通過插座和C#聊天

  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.Xml; 
      using System.Net.Sockets; 
      using System.Net; 
      using System.Threading; 
      using System.IO; 
      namespace server 
     { 
     public partial class Form1 : Form 
{ 
    public static byte[] data; 
    public static byte[] data1; 
    public static Socket sock; 
    public delegate void operation(string s); 
    public delegate void operation2(); 
    public delegate bool verifier(); 
    public Form1() 
    { 
     InitializeComponent(); 
     sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 

     IPAddress adress = IPAddress.Parse("127.0.0.1"); 
     IPEndPoint iep = new IPEndPoint(adress, 4000); 
     EndPoint ep = (EndPoint)iep; 
     sock.Bind(iep); 
     sock.Listen(10); 
     sock = sock.Accept(); 
     data1 = new byte[1024]; 
     data = new byte[1024]; 
     Thread.Sleep(2000); 
     this.Show(); 
     if (sock.Receive(data) > 0) 
     { 

      Thread t = new Thread(new ThreadStart(aller)); 
      t.Start(); 

     } 




    } 

    private void effectuer(String s) 
    { 
     textBox1.Text += "serveur: " + s + "\r\n"; 

     message.Text = ""; 
    } 
    private void effectuer4(String s) 
    { 
     textBox1.Text += "Client: " + s + "\r\n"; 

     message.Text = ""; 
    } 

    private void aller() { 


     String s = ASCIIEncoding.ASCII.GetString(data); 

     if (this.InvokeRequired) Invoke((operation)effectuer4, s); 
     else effectuer4(s); 
     //Thread.Sleep(2000); 
     byte[] data2 = new byte[1024]; 

      if (sock.Receive(data2) > 0 && data2 != data) 
      { 
       data = data2; 
       Thread t = new Thread(new ThreadStart(aller)); 
       t.Start(); 
      } 

     } 


    private void buttonDisconnect_Click(object sender, EventArgs e) 
    { 
     sock.Close(); 
     Application.Exit(); 
    } 

    private void buttonSend_Click(object sender, EventArgs e) 
    { 

     String s = message.Text ; 
     data1 = System.Text.Encoding.ASCII.GetBytes(s); 

     sock.Send(data1); 

     Invoke((operation)effectuer, s); 


    } 
    } 

} 

對於客戶端:他發送一個空消息服務器並等待服務器

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.Net.Sockets; 
    using System.IO; 
    using System.Diagnostics; 
    using System.Threading; 
    using System.Net; 
    using System.Xml; 


    namespace client 
    { 
     public partial class Form1 : Form 
    { 
    public static TcpClient SocketPourClient = null; 
    public static string ClientMessage; 
    public static string ServerMessage; 
    Socket sock; 
    public static byte[] data; 
    public static byte[] data1; 
    public delegate void operation(String s); 
    public delegate void lancer(); 
    public delegate bool verifier(); 

    public Form1(string ip, int port) 
    { 
     InitializeComponent(); 
       IPAddress adress = IPAddress.Parse("127.0.0.1"); 
       IPEndPoint ipEnd = new IPEndPoint(adress, 4000); 
       sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       try 
       { 
        sock.Connect(ipEnd); 
       } 
       catch (SocketException e) 
       { 
        MessageBox.Show(e.ToString()); 
        sock.Close(); 

       } 
       Thread t1 = new Thread(envoi); 
        t1.Start(); 
        data = new byte[1024]; 
        if (sock.Receive(data) > 0) 
       { 

        Thread t=new Thread(new ThreadStart(aller)); 
        t.Start(); 
       } 

    } 

    private void aller() 
    { 

     String s = ASCIIEncoding.ASCII.GetString(data); 

      if(this.InvokeRequired) Invoke((operation)effectuer4, s); 
     else effectuer4(s); 
      // Thread.Sleep(2000); 
      byte[] data2 = new byte[1024]; 

       if (sock.Receive(data2) > 0 && data2 != data) 
       { 
        data = data2; 
        Thread t = new Thread(new ThreadStart(aller)); 
        t.Start(); 
       } 
     } 

    private void envoi() 
    { 
     String s = message.Text ; 
     data1 = System.Text.Encoding.ASCII.GetBytes(s); 
     sock.Send(data1); 
     effectuer(s); 


    } 



private void effectuer(String s) 
{ 
    textBox1.Text += "client: " + s + "\r\n"; 

    message.Text = ""; 
} 

private void effectuer4(String s) 
{ 
    textBox1.Text += "Server: " + s + "\r\n"; 

    message.Text = ""; 

} 

private void buttonDisconnect_Click(object sender, EventArgs e) 
{ 
    sock.Close(); 
    Application.Exit(); 
} 

private void buttonSend_Click_1(object sender, EventArgs e) 
{ 
    String s = message.Text ; 
    data1 = System.Text.Encoding.ASCII.GetBytes(s); 
    sock.Send(data1); 
    Invoke((operation)effectuer, s); 


    } 



} 

}的響應

我的問題是: 我不會說服務器或客戶端將沒有義務初始化應用程序,但兩次可以做到這一點+當我做服務器作爲控制檯應用程序它的工作原理,但是當我更改服務器winforms像應用程序被阻止!!!!!!所以我需要幫助

回答

0

不要在Form1 ctor中運行listen/accept循環!

在單獨的偵聽線程中運行listen/accept。

+0

我添加此代碼:Thread lis = new Thread(listenning); lis.Start();並且我在這個線程中監聽/接受,但是當套接字斷開連接時發送不可用的異常出現 – 2012-04-23 22:44:47

+0

謝謝馬丁我解決了這個線程問題。我現在需要如何使我的應用程序interactif:就像你看到客戶端必須初始化聊天,我不會說服務器和客戶端可以初始化應用程序像一個正常的聊天 – 2012-04-23 22:55:04

+0

任何建議????? – 2012-04-23 23:30:53