C# - Client/Server File Transfer Example 1

This code snippet demonstrates how to transfer files from a client to a server. In this example, both the client and server are console applications and use the TcpClient/TcpListener classes. The client sends the filesize and filename to the server before sending the file. The server uses the filesize to determine how much data to expect from the client and uses the filename to save the file locally.

Client

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net;  
  4. using System.Net.Sockets;  
  5. using System.IO;  
  6. using System.Text;  
  7.  
  8. namespace FileTransferClient  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             try  
  15.             {  
  16.                 Console.WriteLine("Please enter a full file path");  
  17.                 string fileName = Console.ReadLine();  
  18.  
  19.                 TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);  
  20.                 Console.WriteLine("Connected. Sending file.");  
  21.  
  22.                 StreamWriter sWriter = new StreamWriter(tcpClient.GetStream());  
  23.  
  24.                 byte[] bytes = File.ReadAllBytes(fileName);  
  25.  
  26.                 sWriter.WriteLine(bytes.Length.ToString());  
  27.                 sWriter.Flush();  
  28.  
  29.                 sWriter.WriteLine(fileName);  
  30.                 sWriter.Flush();  
  31.  
  32.                 Console.WriteLine("Sending file");  
  33.                 tcpClient.Client.SendFile(fileName);  
  34.  
  35.             }  
  36.             catch (Exception e)  
  37.             {  
  38.                 Console.Write(e.Message);  
  39.             }  
  40.  
  41.             Console.Read();  
  42.         }  
  43.     }  
  44. } 

Server

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net;  
  4. using System.Net.Sockets;  
  5. using System.IO;  
  6. using System.Text;  
  7.  
  8. namespace FileTransfer  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             // Listen on port 1234    
  15.             TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);  
  16.             tcpListener.Start();  
  17.  
  18.             Console.WriteLine("Server started");  
  19.  
  20.             //Infinite loop to connect to new clients    
  21.             while (true)  
  22.             {  
  23.                 // Accept a TcpClient    
  24.                 TcpClient tcpClient = tcpListener.AcceptTcpClient();  
  25.  
  26.                 Console.WriteLine("Connected to client");  
  27.  
  28.                 StreamReader reader = new StreamReader(tcpClient.GetStream());  
  29.  
  30.                 // The first message from the client is the file size    
  31.                 string cmdFileSize = reader.ReadLine();  
  32.  
  33.                 // The first message from the client is the filename    
  34.                 string cmdFileName = reader.ReadLine();  
  35.  
  36.                 int length = Convert.ToInt32(cmdFileSize);  
  37.                 byte[] buffer = new byte[length];  
  38.                 int received = 0;  
  39.                 int read = 0;  
  40.                 int size = 1024;  
  41.                 int remaining = 0;  
  42.  
  43.                 // Read bytes from the client using the length sent from the client    
  44.                 while (received < length)  
  45.                 {  
  46.                     remaining = length - received;  
  47.                     if (remaining < size)  
  48.                     {  
  49.                         size = remaining;  
  50.                     }  
  51.  
  52.                     read = tcpClient.GetStream().Read(buffer, received, size);  
  53.                     received += read;  
  54.                 }  
  55.  
  56.                 // Save the file using the filename sent by the client    
  57.                 using (FileStream fStream = new FileStream(Path.GetFileName(cmdFileName)FileMode.Create))  
  58.                 {  
  59.                     fStream.Write(buffer, 0, buffer.Length);  
  60.                     fStream.Flush();  
  61.                     fStream.Close();  
  62.                 }  
  63.  
  64.                 Console.WriteLine("File received and saved in " + Environment.CurrentDirectory);  
  65.             }  
  66.         }  
  67.     }  
  68. } 

1 comment:

Unknown said...

Братуха,на разные компьютеры не передаётся

Post a Comment