• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > Send File By TcpClient generate SocketException
Ask a questionAsk a question
Search Forums:
  • Search .NET Framework Networking and Communication Forum Search .NET Framework Networking and Communication Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

QuestionSend File By TcpClient generate SocketException

  • Sunday, June 10, 2007 12:24 PMyouTe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

     

    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?

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Monday, June 11, 2007 10:05 AMAlan J. McFarlane Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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...?

    • ReplyReply
    • QuoteQuote
     
  • Monday, June 11, 2007 11:09 AMyouTe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Thanks for your reply,I'v been edit the thread,please help me,and I can't speak English,but I'm learning.
    • ReplyReply
    • QuoteQuote
     
  • Tuesday, June 12, 2007 2:18 PMAlan J. McFarlane Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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?

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement