Здравствуйте, Dark-Shadow, Вы писали:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
private static int localPort = 5005;
private static UdpClient rUdpClient = new UdpClient(localPort);
private static IPEndPoint rIpEnd = null;
private static byte[] rByte;
private static string fType;
private static string fSize;
private static FileStream fs;
static void Main(string[] args)
{
Console.WriteLine("Ждем получения файла");
Program pr = new Program();
pr.GetInfo();
Thread p = new Thread(new ThreadStart(pr.ReceivedFile));
p.Start();
Console.ReadLine();
}
private void GetInfo()
{
GetFileDet();
GetFileSize();
}
private void GetFileDet()
{
try
{
rByte = rUdpClient.Receive(ref rIpEnd);
Console.WriteLine("*****Тип файла*****");
fType = Encoding.ASCII.GetString(rByte);
Console.WriteLine(fType);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void GetFileSize()
{
try
{
rByte = rUdpClient.Receive(ref rIpEnd);
Console.WriteLine("*****Размер файл*****");
fSize = Encoding.ASCII.GetString(rByte);
Console.WriteLine(fSize);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally { rUdpClient.Close(); }
}
private void ReceivedFile()
{
try
{
TcpListener listner = new TcpListener(5002);
listner.Start();
TcpClient client = listner.AcceptTcpClient();
NetworkStream st = client.GetStream();
rByte = new byte[Convert.ToInt32(fSize)];
st.Read(rByte, 0, Convert.ToInt32(fSize));
Console.WriteLine("Сохраняем присланый файл");
fs = new FileStream("temp." + fType, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(rByte, 0, rByte.Length);
Console.WriteLine("Файл сохранен");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
Код лучше оформлять специальными тегами (я твой код оформил тегом [ccode]).
1. Создаешь объект TcpListener. в цикле, при подключении клиентов (TcpClient client = listner.AcceptTcpClient();), для каждого клиента создаешь поток для приема файлов — для каждого клиента свой поток получается.
2. Создаешь поток в котором создаешь TcpListener и в цикле начинаешь принимать соединения (TcpClient client = listner.AcceptTcpClient();) и закачивать файлы — все файлы будут закачиваться в одном потоке, сначала от одного клиента, потом от другого и т.д.