2016-02-27 54 views
0

我对套接字编程颇为陌生。我希望这个问题是可以理解的。C#异步套接字服务器只接收来自客户端的一条消息

问题是,当我使用客户端的button1_click发送textbox的 内容时 - 服务器仅获取第一条消息。我不知道发生了什么问题。

可能是什么问题?

这里是服务器:

public partial class Form1 : Form 
{ 
    Socket server; 
    byte[] byteData = new byte[1024]; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      server = new Socket(AddressFamily.InterNetwork, 
           SocketType.Stream, 
           ProtocolType.Tcp); 
      IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 20000); 

      server.Bind(endPoint); 
      server.Listen(1); 
      server.BeginAccept(new AsyncCallback(Accept), null); 
      textBox1.Text = "Server started..."; 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    private void Accept(IAsyncResult ar) 
    { 
     try 
     { 
      Socket client = server.EndAccept(ar); 
      server.BeginAccept(new AsyncCallback(Accept), null); 
      client.BeginReceive(byteData, 0, byteData.Length, 
       SocketFlags.None, new AsyncCallback(Receive), client); 

     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    private void Receive(IAsyncResult ar) 
    { 
     Socket client = (Socket)ar.AsyncState; 
     client.EndReceive(ar); 

     textBox1.Invoke(new Action(delegate() 
     { 
      textBox1.AppendText(Environment.NewLine 
         + Encoding.ASCII.GetString(byteData)); 
     })); 
     byteData = null; 
    } 
} 

这里是客户端:

public partial class Form1 : Form 
{ 
    Socket client; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     if (textBox1.Text == null) 
      button1.Enabled = false; 
     else 
      button1.Enabled = true; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      client = new Socket(AddressFamily.InterNetwork, 
           SocketType.Stream, 
           ProtocolType.Tcp); 
      IPAddress ip = IPAddress.Parse("127.0.0.1"); 
      IPEndPoint ipEndPoint = new IPEndPoint(ip, 20000); 
      client.Connect(ipEndPoint); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 

      byte[] byteData = Encoding.ASCII.GetBytes(textBox1.Text); 
      client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, 
          new AsyncCallback(Send), null); 
      textBox1.Text = String.Empty; 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    private void Send(IAsyncResult ar) 
    { 
     try 
     { 
      client.EndSend(ar); 
     } 
     catch (ObjectDisposedException) 
     { } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 

回答

1

Async,你得重新初始化的BeginReceive在服务器端,只要你要听消息。

因此,在您的Receive回调中,您应该在EndReceive之后重新初始化您的BeginReceive。否则,你无法获得下一个消息:

private void Receive(IAsyncResult ar) 
{ 
    Socket client = (Socket)ar.AsyncState; 
    client.EndReceive(ar); 
    client.BeginReceive(byteData, 0, byteData.Length, //add BeginReceive again 
     SocketFlags.None, new AsyncCallback(Receive), client); 

    textBox1.Invoke(new Action(delegate() 
    { 
     textBox1.AppendText(Environment.NewLine 
        + Encoding.ASCII.GetString(byteData)); 
    })); 
    byteData = null; 
} 

更多工作与Async例子,看看这个:Sending a value from server to client with sockets

相关问题