我在阅读之前读取服务器的响应时遇到问题。我可以在第一次读取服务器时获得服务器的响应,但是当我将其他内容发送回服务器时,响应将为null
并且我在response = sr.ReadToEnd();
上崩溃。
两个args
会发生什么情况,它会使用第二个arg
更新位置。然后它发送到服务器,然后服务器以 Ok 响应。在以下请求中,将使用新位置而不是第一个位置。
error message抱怨第102行,即:
response = sr.ReadToEnd();
这是args
向服务器的第一次写作以及给出的响应。
// these are the args being sent to the server
sw.WriteLine(args[0]);
// the flush sends this to the server which the response is then read
sw.Flush();
try
{
response = sr.ReadToEnd();
//[some if statement...]
else if (args.Length == 2)
{
//response = sr.ReadLine();
if (args[1] != response)
{
// this is what should be put in 'sw.write()' for the updated database entry
// <name><space><location><CR><LF>
// cssbct + " " + "is in fenner", so "cssbct location has changed"
// args[0] + " " + response
// response is now what the arg is
response = args[1];
// write the new response so that it will be sent to the server
sw.WriteLine(args[0] + " " + response);
// send it to the server
sw.Flush();
// write out the args and that the location has changed
Console.WriteLine(args[0] + " " + " location changed to be" + " " + response);
// just testing what the response should be
Console.WriteLine(args[0] + " " + response);
// read the response from the server
response = sr.ReadToEnd();
// if it equals "OK" do this
if (response == "OK")
{
// drops connection to the server
// probably not what is needed but look at later date
Console.WriteLine(response);
Console.WriteLine("Client connection closed");
client.Close();
}
}
}
sw.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
我正在以正确的方式接收服务器的第二个响应吗?
我没有找到使用streamReader/Writer
来获取服务器的第二个响应的方法。
编辑:连接
string response = "";
TcpClient client = new TcpClient();
client.Connect("whois.net.dcs.hull.ac.uk", 43);
// client.Connect("whois.networksolutions.com", 43);
Stream stream = client.GetStream();
StreamWriter sw = new StreamWriter(client.GetStream(), Encoding.ASCII);
StreamReader sr = new StreamReader(client.GetStream(), Encoding.ASCII);
客户端和服务器响应的规范
名称空间位置CRLF
服务器响应(无权访问服务器代码,只是说它会做这样的事情)
如果没有找到条目
“错误:未找到任何条目 [drop connection]
如果服务器更新database
新位置:
好
[drop connection]
一个想法:响应的原因null
是否已经到达流的末尾?