2

この質問は何度も聞かれる可能性があります..しかし、私が取り組んでいるシナリオは少し違いますが、非常に単純です..エラーを超えており、これまでのところ解決策がありません..これは、エラーが発生するクライアントコードです.太字で強調表示されている「読む」..助けてください..thnx 事前に.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net.Security;
using System.Timers;

namespace ClientRequest
{
class Program
{
    static void Main(string[] args)
    {
        try
        {

            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            IPAddress[] objLocalAddr = null;
            String strHostName = "";
            int port = 2425;

            strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            objLocalAddr = ipEntry.AddressList;

            tcpclnt.Connect(objLocalAddr[0], port);


            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            **int k = stm.Read(bb, 0, 100);**                
            for (int i = 0; i < k; i++)
            Console.Write(Convert.ToChar(bb[i]));               

               tcpclnt.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
         }
      }
  }
}
4

1 に答える 1

0

Read 関数を実行する前に、次の操作を行います。

if( stm.CanRead && stm.DataAvailable )
{
    int k = stm.Read(bb, 0, 100);
    for (int i = 0; i < k; i++)
        Console.Write(Convert.ToChar(bb[i]));               
}

tcpclnt.Close();
于 2012-10-16T12:53:58.423 に答える