我正在尝试将文本写入文本文件:
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(字符串路径,FileMode模式,FileAccess 访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String System.IO.FileStream..ctor中的msgPath,Boolean bFromProxy(String path,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions options)at System.IO.StreamWriter.CreateFile(String path,Boolean append)at System.IO.StreamWriter..ctor(String path,Boolean append,Encoding System.IO.StreamWriter..ctor(String。)中的编码,Int32 bufferSize) 路径)在IntegServer.Main()中 C:\ Users \用户有限公司\桌面\ IntegClient \ IntegServer \ IntegServer \的Program.cs:行 38错误.....在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress(socketAddress)at System.Net.Sockets.Socket.Bind(EndPoint localEP)at System.Net.Sockets.TcpListener.Start(Int32 backlog)at System.Net.Sockets.TcpListener.Start()在IntegServer.Main()中 C:\ Users \用户有限公司\桌面\ IntegClient \ IntegServer \ IntegServer \的Program.cs:行 21错误.....在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress(socketAddress)at System.Net.Sockets.Socket.Bind(EndPoint localEP)at System.Net.Sockets.TcpListener.Start(Int32 backlog)at System.Net.Sockets.TcpListener.Start()在IntegServer.Main()中 C:\ Users \用户有限公司\桌面\ IntegClient \ IntegServer \ IntegServer \的Program.cs:行 21错误.....在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress(socketAddress)at System.Net.Sockets.Socket.Bind(EndPoint localEP)at System.Net.Sockets.TcpListener.Start(Int32 backlog)at 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();
}
答案 0 :(得分:0)
也许这就是你想要做的事情?
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
using (var sw = new StreamWriter("output.txt"))
{
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
sw.Write(Convert.ToChar(b[i]));
}
}
答案 1 :(得分:0)
看起来您的问题是在访问模式下可以更改代码以创建文件流传入访问模式/写入或读写模式。 如果你需要关注
,这是一个例子FileStream fsFileStream = null;
fsFileStream = new FileStream("yourOutputFile", FileMode.Open, FileAccess.ReadWrite);
// then add your code here.. also is there a reason why you are declaring such a small byte to write try 512 or 1024 just a suggestion