从服务器发送消息到客户端的问题

时间:2009-09-09 09:13:17

标签: c# sockets sendmessage

使用Socket编程在C#中创建一个Window应用程序。我开发了一个Server&一个客户。两者都工作正常,但是我遇到的问题是,当我从CLIENT发送任何消息时,它发送完美并在SERVER上接收但是每当我尝试从SERVER发送任何消息时它都不会发送给客户端,因为在开始时当建立连接时,服务器将消息发送到“连接已建立”并在客户端完全接收的客户端,但稍后在服务器上不向客户端发送任何消息!有谁能请帮帮我??????? 问候 Umair

修改

  //Code at SERVER for SENDING...
  private void button_send(object sender, EventArgs e)
     { 
        string input = textBoxWrite.Text;
        byte[] SendData = new byte[1024];
        ASCIIEncoding encoding = new ASCIIEncoding();
        SendData = encoding.GetBytes(input);
        client.Send(SendData,SendData.Length,SocketFlags.None);
        textBoxShow.Text = "Server: " + input;
     }
   //Code at CLIENT for receiving
            NetworkStream networkStream = new NetworkStream(server);
            string input = textBoxUser.Text + ": " + textBoxWrite.Text;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] inputByte = encoding.GetBytes(input);
            if (networkStream.CanWrite)
            {
                networkStream.Write(inputByte, 0, inputByte.Length);
                textBoxShow.Text = textBoxShow.Text + Environment.NewLine + input;
                textBoxWrite.Text = "";
                networkStream.Flush();
            }

1 个答案:

答案 0 :(得分:1)

我不确定如何根据您提供的信息提供最佳帮助,但也许您可以查看this example of C# socket programming之类的内容并与自己的应用进行比较。

相关问题