如何发送“你好”到服务器并回复“嗨”?
使用我的代码,我可以在服务器上读取消息并从客户端写入消息。但我无法从服务器写入响应并在客户端中读取。
客户端
上的代码
var cli = new TcpClient();
cli.Connect("127.0.0.1", 6800);
string data = String.Empty;
using (var ns = cli.GetStream())
{
using (var sw = new StreamWriter(ns))
{
sw.Write("Hello");
sw.Flush();
//using (var sr = new StreamReader(ns))
//{
// data = sr.ReadToEnd();
//}
}
}
cli.Close();
服务器上的代码
tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
while (run)
{
var client = tcpListener.AcceptTcpClient();
string data = String.Empty;
using (var ns = client.GetStream())
{
using (var sr = new StreamReader(ns))
{
data = sr.ReadToEnd();
//using (var sw = new StreamWriter(ns))
//{
// sw.WriteLine("Hi");
// sw.Flush();
//}
}
}
client.Close();
}
如何让服务器读取数据后回复并让客户端读取此数据?
With my code I can read a message on the server and write from the client. But I am not being able to write a response from the server and read in the client.
The code on the client
var cli = new TcpClient();
cli.Connect("127.0.0.1", 6800);
string data = String.Empty;
using (var ns = cli.GetStream())
{
using (var sw = new StreamWriter(ns))
{
sw.Write("Hello");
sw.Flush();
//using (var sr = new StreamReader(ns))
//{
// data = sr.ReadToEnd();
//}
}
}
cli.Close();
The code on the server
tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
while (run)
{
var client = tcpListener.AcceptTcpClient();
string data = String.Empty;
using (var ns = client.GetStream())
{
using (var sr = new StreamReader(ns))
{
data = sr.ReadToEnd();
//using (var sw = new StreamWriter(ns))
//{
// sw.WriteLine("Hi");
// sw.Flush();
//}
}
}
client.Close();
}
How can I make the server reply after reading the data and make the client read this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是
,因此您可以直接写回客户端,而不需要它进行自我识别。 如果您使用
Stream.Read()
或.ReadLine()
而不是.ReadToEnd(),您拥有的代码将实际工作
。
ReadToEnd()
将在网络流上永远阻塞,直到流关闭。请参阅类似问题的此答案,或来自 MSDN,如果您在一侧使用 ReadLine(),则需要在另一侧使用 WriteLine(),而不是 Write()。另一种方法是使用循环调用 Stream.Read() 直到没有任何内容可供读取。您可以在 MSDN 上的 AcceptTcpClient() 文档。相应的客户端示例位于 TcpClient 文档。
Since you are using
, you can write back to the client directly without needing it to self-identify. The code you have will actually work if you use
Stream.Read()
or.ReadLine()
instead of.ReadToEnd()
.ReadToEnd()
will block forever on a network stream, until the stream is closed. See this answer to a similar question, or from MSDN,If you use ReadLine() at one side, you will need to use WriteLine() - not Write() - at the other side. The alternative is to use a loop that calls Stream.Read() until there is nothing left to read. You can see a full example of this for the server side in the AcceptTcpClient() documentation on MSDN. The corresponding client example is in the TcpClient documentation.
俗气,低效,但对一次性程序来说却很有效:
端口和IP。
依次连接,然后发送回复
溪流。
但是,这是一个很好的起点,请查看 Sockets 类正确的双向通信。
Cheesy, inneficient, but does the trick on a one-time throwaway program:
port and IP.
in turn connect, then send the reply
stream.
However, this is a great place to start, look into Sockets class for proper bi-directional communication.