CodeXchange Friday, April 19, 2024
Home
What's it?
Download
Register
My Snippets
Add Snippet
Search
Faq
Contact Us
Sponsors

Website hosted by


Code Snippet Preview

Review the code snippet before inserting it on your project.

Snippet Metadata
Summary: P2P Transfer class TCPListener and the TCPClient classes
Language: C#
Author: Robert Wagner
Author snippets RSS:
Culture: en-US
Bytes: 2964
Visual Studio 2005 Snippet:

Snippet Stats
Downloads: 10
Overall rating : 0
Average rating : Snippet rating

Snippet Preview
Code:
 public MyTcpListener(int port) : base(port)
  {
   
  }
  public void StopMe() 
  {
   if ( this.Server != null )
   {
    this.Server.Close();
   }
  }
 }
 /// 
 ///    Summary description for Transfer.
 /// 
 public class Transfer
 {
  MyTcpListener tcpl;
  public Transfer()
  {
   OptionsLoader ol = new OptionsLoader(); 
   int port = 8081;
   if (ol.Port > 0)
   {
    port = ol.Port;
   }
   else
   {
    // The default in case nothing else is set
    port = 8081;
   } 
   this.tcpl = new MyTcpListener(port);   
  }
 
  public void TransferShutdown()
  {
   tcpl.StopMe();
  }
  public void ListenForPeers() 
  {           
   try
   {  
    
    Encoding ASCII = Encoding.ASCII;     
        
tcpl.Start(); 

    while (true)
    {  
     // Accept will block until someone connects     
     Socket s = tcpl.AcceptSocket();      
     NetworkStream DataStream = new NetworkStream(s);
     String filename;
     Byte[] Buffer = new Byte[256];
     DataStream.Read(Buffer, 0, 256);
     filename = Encoding.ASCII.GetString(Buffer);
     StringBuilder sbFileName = new StringBuilder(filename);
     StringBuilder sbFileName2 = sbFileName.Replace("\", "\\");
     FileStream fs = new FileStream(sbFileName2.ToString(), FileMode.Open, FileAccess.Read);     
     BinaryReader reader = new BinaryReader(fs);
     byte[] bytes = new byte[1024];
     int read;
     while((read = reader.Read(bytes, 0, bytes.Length)) != 0) 
     {
      DataStream.Write(bytes, 0, read);
     }
     reader.Close(); 
     DataStream.Flush();
     DataStream.Close();
    }
   }
   catch(SocketException ex)
   {    
    MessageBox.Show(ex.ToString());
   }
  }
  public void DownloadToClient(String server, string remotefilename, string localfilename) 
  {
   try
   {
    TcpClient tcpc = new TcpClient();                
    Byte[] read = new Byte[1024];                    
    OptionsLoader ol = new OptionsLoader(); 
    int port = 0;
    if (ol.Port > 0)
    {
     port = ol.Port;
    }
    else
    {
     // The default in case nothing else is set
     port = 8081;
    } 

    // Try to connect to the server 
    IPHostEntry IPHost = Dns.Resolve(server); 
    string []aliases = IPHost.Aliases; 
    IPAddress[] addr = IPHost.AddressList; 
    IPEndPoint ep = new IPEndPoint(addr[0], port);
    tcpc.Connect(ep);
    
    // Get the stream
    Stream s = tcpc.GetStream();    
    Byte[] b = Encoding.ASCII.GetBytes(remotefilename.ToCharArray());
    s.Write( b, 0,  b.Length );
    int bytes;
    FileStream fs = new FileStream(localfilename, FileMode.OpenOrCreate);
    BinaryWriter w = new BinaryWriter(fs);
    // Read the stream and convert it to ASII   
    while( (bytes = s.Read(read, 0, read.Length)) != 0) 
    {    
     w.Write(read, 0, bytes);
     read = new Byte[1024];    
    }               
    tcpc.Close();
    w.Close();
    fs.Close();
   }
   catch(Exception ex)
   {
    throw new Exception(ex.ToString());     
   }
  } 
 }
}

Snippet Comments
Comments:
No comments for this snippet

Other snippets that may interest you..
Related Snippets
TDT - GridPage - Page_Init (C#)
CustomValitaor (C#)
(C#)
Get the file extension from a file path or file name (VB.NET)
grab record from db and subtracts it from variable (VB.NET)



Copyright ©2009-2024 CodeXchange. Server version 1.0.3720.32855 Client Version 0.9.0.0

With from Barcelona

Most Helpful members
These are the members who have received the most points for their helpful samples
Zepho Zep
Robert Wagner
Galen Taylor

All time 'Hall of fame'
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
INI File Access (VB.NET)
Read XML from string into DataSet (C#)
Create Manifest File for your Application (VB.NET)
Round function to avoid banker's rounding (VB.NET)
Get Short and Long Path Names (VB.NET)
Sending Mail through authenticated SMTP server (C#)
One Way Hash for strings (C#)
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
How do I load an image from a URI address? (VB.NET)
Use our easy to use Visual Studio.NET addin client and start sharing code snippets with the CodeXchange community!
Refreshed: Friday, April 19, 2024