发送消息从服务器到客户端的问题
问题描述:
即时通讯使用套接字编程在C#中制作窗口应用程序。我开发了一个服务器客户端。这两个工作正常,但即时通讯的问题是,当我从CLIENT发送任何消息,它发送完美,并在服务器上接收,但每当我尝试发送任何消息从服务器它不会发送到客户端,因为在一开始当建立连接时,服务器将消息发送到客户端,即“连接已建立”并完美地接收到客户端,但稍后服务器不会向客户端发送任何消息!任何人都可以请帮助我吗??????? 问候 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();
}
答
我不知道如何最好根据您所提供的信息来帮助,但也许你可以看看像this example of C# socket programming,并与你的比较自己的应用。
+0
我之前看到过这个例子,但我在我的应用程序中使用了线程,因此如果我将同一个方法同时使用SEND&RECEIVE,它会挂起我的应用程序。我只想在按下按钮时发送消息,并将写入文本框的所有文本发送到客户端。 – Umair 2009-09-09 09:27:00
你可以发布一些示例代码? – 2009-09-09 09:30:26
在您提供的示例代码中,“客户端用于接收的代码”根本没有收到。它实际上是将数据写入NetworkStream,而不是从NetworkStream读取数据。你能提供一个更准确的代码示例吗? – 2009-09-09 13:32:34