简体   繁体   English

从服务器向客户端发送消息时出现问题

[英]Problem in Sending messages from Server to Client

im making a Window Application in C# using Socket Programming. 即时通讯使用套接字编程在C#中制作Window应用程序。 I have developed a Server & a Client. 我已经开发了服务器和客户端。 Both are working fine but the problem which im gettin is that when ever i send any message from CLIENT, its send perfectly and receives on SERVER but whenever i try to send any message from SERVER it doesn't send to Client, since at the beginning when a connection is built, server sends the message to client that "Connection Established" and received at Client perfectly,but later on server does not send any message to client!!! 两者都工作正常,但即时出现的问题是,每当我从CLIENT发送任何消息时,它就完美发送并在SERVER上接收,但是每当我尝试从SERVER发送任何消息时,它都不会发送给客户端,因为在开始时建立连接时,服务器会向客户端发送“已建立连接”并在客户端完美接收的消息,但稍后服务器上不会向客户端发送任何消息!!! Could anyone please help me out ??????? 有人可以帮我吗??????? Regards Umair 问候Umair

EDIT: 编辑:

  //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();
            }

我不确定根据您提供的信息如何最好地提供帮助,但是也许您可以看一下类似于C#套接字编程示例的示例,并与您自己的应用程序进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM