2011-12-20 9 views
2

: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(); 
} 
+0

エラーは何ですか? (たぶんそれらの一部です...) – David

+0

'TextWriter'を使用したときに発生するエラーは何ですか? –

+2

ループ内に新しいライターを作成していますか?それは問題を尋ねている – Rob

答えて

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])); 
    } 
} 
+0

こんにちはデビッド、私はあなたのコードを使用するときにこのエラーが表示されます:エラー 'System.IO.FileStream'は 'WriteLine' 'System.IO.FileStream'型の最初の引数を受け入れる拡張メソッド 'WriteLine'が見つかりませんでした(usingディレクティブまたはアセンブリ参照がありませんか?) – Mike

+0

@Mike:私の間違い。 (コードを自由に渡すことについての私のせいです。)代わりに 'StreamWriter'に書き込むように更新しました。さまざまな条件の下でファイルを安全に開くことができれば、私の頭の上を思い出すことはできません。ファイルが存在し、最初に書き込み可能かどうかを確認するためにいくつかの検証を行いたい場合があります。 – David

+0

これは、テキストファイルが非常にstrangley(垂直方向)に来るが、これは、これを参照してください:http://i44.tinypic.com/30s83so.jpg – Mike

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 
関連する問題