【发布时间】:2011-12-20 15:38:04
【问题描述】:
我正在尝试将文本写入文本文件:
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
//TextWriter tw = new StreamWriter("output.txt");
//tw.WriteLine(Convert.ToChar(b[i]));
}
虽然 Console.Write 方法运行良好,并且文本输出显示在屏幕上,但是当我使用 TextWriter 方法时,应用程序会产生 100 个错误(继续)并且不会向指定的输出文件输出任何内容
有没有其他方法可以获得“Console.Write(Convert.ToChar(b[i]));”的输出到文本文件?
以下是错误示例:
服务器在 8111 端口运行...本地端点是 :172.16.0.37:8111 等待连接.....连接已接受 从 172.16.0.37:59307 收到... //错误.....在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess 访问、Int32 权限、布尔用户权限、FileShare 共享、Int32 bufferSize,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs,字符串 msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize,FileOptions 选项)在 System.IO.StreamWriter.CreateFile(字符串路径,布尔附加)在 System.IO.StreamWriter..ctor(字符串路径,布尔附加,编码 编码,Int32 bufferSize) 在 System.IO.StreamWriter..ctor(String 路径)在 IntegServer.Main() 中 C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 38 错误.....在 System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 在 System.Net.Sockets.TcpListener.Start(Int32 backlog) 在 System.Net.Sockets.TcpListener.Start() 在 IntegServer.Main() 中 C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21 错误.....在 System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 在 System.Net.Sockets.TcpListener.Start(Int32 backlog) 在 System.Net.Sockets.TcpListener.Start() 在 IntegServer.Main() 中 C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21 错误.....在 System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 在 System.Net.Sockets.TcpListener.Start(Int32 backlog) 在 System.Net.Sockets.TcpListener.Start()
完整代码如下:
try
{
IPAddress ipAd = IPAddress.Parse("172.16.0.37"); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8111);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8111...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
using (var txt = File.OpenWrite("output.txt"))
{
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
txt.WriteLine(Convert.ToChar(b[i]));
}
}
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement to Client");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
// Loop the process
loop();
}
【问题讨论】:
-
有什么错误? (也许只是其中的一部分......)
-
使用
TextWriter时遇到哪些错误? -
你是在循环中创建一个新的作家吗?这是自找麻烦
-
Sergey B 说得对,但他删除了他的答案。他的答案是 add: File.WriteAllBytes("output.txt", b);
-
仍在查看错误,它似乎也是一个权利问题..