MSDN > フォーラム ホーム > .NET Framework Networking and Communication > Send File By TcpClient generate SocketException
質問する質問する
 

質問Send File By TcpClient generate SocketException

  • 2007年6月10日 12:24youTe ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

     

    Code Snippet

    private void SendFile(string address)
            {
                TcpClient tClient = new TcpClient(new IPEndPoint(IPAddress.Loopback , TcpPort));

                    //  Generate SocketException

                    //  No connection could be made because the target machine actively refused it

                    tClient.Connect(IPAddress.Loopback , TcpPort);  // ErrorCode=10061

                    NetworkStream sendNetStm = tClient.GetStream();

                    FileStream sendFileStm = new FileStream(address, FileMode.Open);

                    byte[] buffer = new byte[256];
                      int count;

                    while ((count = sendFileStm.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        sendNetStm.Write(buffer, 0, count);
                    }

                    sendFileStm.Close();
                       tClient.Close();
             }

     

    Why?How to solve?

すべての返信

  • 2007年6月11日 10:05Alan J. McFarlane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

    What's the exception?!  We're not psychic... :-,)  Where does it occur (what's its stack trace)?

     

    However, you should probably change the first line to be TcpClient tClient = new TcpClient();

    I presume you have another application listening on port TcpPort, maybe a TcpListener of your own in another program...?

  • 2007年6月11日 11:09youTe ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Thanks for your reply,I'v been edit the thread,please help me,and I can't speak English,but I'm learning.
  • 2007年6月12日 14:18Alan J. McFarlane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

    Err...  Did you change

       TcpClient tClient = new TcpClient(new IPEndPoint(IPAddress.Loopback , TcpPort));

    to

        TcpClient tClient = new TcpClient();

     

    Do you still get an exception?  Which exception?  SocketException, ArgumentException?  What line?