从服务器读取文件内容,然后通过套接字将其发送到客户端 C#
我在这里尝试通过服务器发送文本文件的内容并将其发送到客户端
这是服务器客户
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(localEP);
server.Listen(10);
Console.WriteLine("Waiting for Client...");
Socket client = server.Accept();
IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
Console.WriteLine("Got connection from " + clientAddress);
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Welcome to my test server");
writer.Flush();
string line = null;
while ((line = reader.ReadLine()).Length != 0)
{
Console.WriteLine("loooking for this file:" + line);
System.IO.FileInfo fi = new System.IO.FileInfo(line);
Console.WriteLine("Found");
writer.WriteLine("File Size: " + fi.Length + "\nContent:");
StreamReader tr = new StreamReader(line);
string s = null;
//string b = "";
while((s= tr.ReadLine()).Length != 0)
{
writer.WriteLine(tr.ReadLine());
writer.Flush();
}
tr.Close();
}
client.Close(); server.Close();
端从服务器读取的部分是这样
String line = null;
line = textBox3.Text;
writer.WriteLine(line); // Send line to Server
writer.Flush();
string s = null;
// Read line from server, then echo on the screen
while((s= reader.ReadLine()).Length != 0)
{
textBox4.Text += reader.ReadLine() + "\r\n\r\n";
}
当我运行代码时,根本没有错误,但是客户端卡住了,当我停止服务器时,文件的内容将显示,,,顺便说一句,它是一个 GUI 应用程序
i am trying here to send the content of a text file by the server and send it to the client
this is the server
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(localEP);
server.Listen(10);
Console.WriteLine("Waiting for Client...");
Socket client = server.Accept();
IPAddress clientAddress = ((IPEndPoint)client.RemoteEndPoint).Address;
Console.WriteLine("Got connection from " + clientAddress);
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Welcome to my test server");
writer.Flush();
string line = null;
while ((line = reader.ReadLine()).Length != 0)
{
Console.WriteLine("loooking for this file:" + line);
System.IO.FileInfo fi = new System.IO.FileInfo(line);
Console.WriteLine("Found");
writer.WriteLine("File Size: " + fi.Length + "\nContent:");
StreamReader tr = new StreamReader(line);
string s = null;
//string b = "";
while((s= tr.ReadLine()).Length != 0)
{
writer.WriteLine(tr.ReadLine());
writer.Flush();
}
tr.Close();
}
client.Close(); server.Close();
the part of the client where it reads from the server is this
String line = null;
line = textBox3.Text;
writer.WriteLine(line); // Send line to Server
writer.Flush();
string s = null;
// Read line from server, then echo on the screen
while((s= reader.ReadLine()).Length != 0)
{
textBox4.Text += reader.ReadLine() + "\r\n\r\n";
}
when i run the code, no errors at all, but the client get stuck, and when i stop the server, the content of the file will show,,, BTW, its a GUI application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}
while ((s = reader.ReadLine()) != null) {
textBox4.Text += s;
}
StreamReader 的示例代码 使用以下构造来检测流结束。另外 - 你真的想在该循环中阅读两行吗?
Sample code for StreamReader uses the construct below to detect end of stream. Also - do you really want to read two lines in that loop?
你提到这是一个 GUI 应用程序?如果是这样,您在哪个线程上进行阅读?如果您在主线程上进行读取,则应用程序消息循环将被冻结,并且在您停止另一端并终止连接之前不会显示任何内容。
You mentioned that this is a GUI app? If so, on which thread are you doing the reading? If you are doing the read on the main thread, then the application messageloop will be frozen and nothing will show up until you stop the other side and kill the connection.