:Console.Writeメソッドは完璧に動作、およびテキスト出力が画面に表示されているが私は、テキストファイルにテキストを書き込むしようとしています
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]));
}
が、 TextWriterメソッドを使用すると、アプリケーションは100のエラーを生成し続け、指定された出力ファイルに何も出力しません。
"Console.Write(Convert.ToChar(b [私]));"テキストファイルに?以下は
はエラーのサンプルです:サーバーはポート8111で実行されている...ローカルエンドポイントは です:172.16.0.37:8111は、接続.....接続待機します受け入れた から172.16.0.37:59307受信した... //エラー..... System.IO .__ Error.WinIOError(Int32 errorCode、String maybeFullPath)
at System.IO.FileStream.Init(String path、 FileModeモード、FileAccessの アクセス、のInt32権、ブールuseRights、ファイル共有の共有、のInt32 がbufferSize、FileOptionsはオプション、SECURITY_ATTRIBUTES secAttrs、セントSystem.IO.FileStream..ctor( パス、FileModeモード、FileAccessアクセス、FileShare共有、Int32 、bufferSize、FileOptionsオプション)ののmsgPath、Boolean bFromProxy)System.IO.StreamWriter.CreateFile(String path、ブール値) でInteger.Main()のSystem.IO.StreamWriter..ctor(String パス)のSystem.IO.StreamWriter..ctor(String path、Boolean append、エンコード エンコーディング、Int32 bufferSize) C :\ Users \ Limited \ Desktop \ IntegClient \ IntegServer \ IntegServer \ Program.cs:行 38エラー..... System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot、SocketAddress socketAddress)at System.Net .Sockets.Socket.Bind(EndPoint localEP): CでIntegServer.Main()で System.Net.Sockets.TcpListener.Start()でSystem.Net.Sockets.TcpListener.Start(のInt32バックログ):\ Users \ユーザー限定\デスクトップ\ IntegClient \ IntegServer \ IntegServer \ Program.csの:ライン 21エラー..... システムで System.Net.Sockets.Socket.BindでSystem.Net.Sockets.Socket.DoBind(エンドポイント endPointSnapshot、のSocketAddressのSocketAddress)(エンドポイントlocalEP)で。 IntegerServer.Main()の のC:¥Users¥Limited¥Desktop¥IntegClient¥IntegServer¥IntegServer¥Program.csのNetSockets.TcpListener.Start(Int32バックログ) System.Net.Sockets.TcpListener.Start() :行 21エラー..... System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot、SocketAddress socketAddress ) System.Net.Sockets.TcpListener.StartでSystem.Net.Sockets.TcpListener.Start(のInt32のバックログ)で System.Net.Sockets.Socket.Bind(エンドポイントlocalEP)()で
完全なコードは、次のとおりです。
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();
}
エラーは何ですか? (たぶんそれらの一部です...) – David
'TextWriter'を使用したときに発生するエラーは何ですか? –
ループ内に新しいライターを作成していますか?それは問題を尋ねている – Rob