c#文件服务器客户端在服务器完成发送之前接收文件

时间:2013-02-04 03:47:15

标签: c# file sockets client socketserver

我在使用c#制作的文件服务器时遇到了一些问题。问题是,当我在我的计算机上运行服务器和客户端时,它们工作正常,但是当我在另一台计算机上使用客户端时,客户端会在服务器完成发送之前接收该文件。任何输入都会有帮助。感谢。

服务器:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PortalServer
{
    class Program
    {
        static Stream stream01;
        static long length;
        static int int04;
        static int int03;
        static int int02;
        static int int01;
        static TcpListener tcp01;
        static TcpClient tcp02;
        static FileStream s01;
        static byte[] data01;
        static byte[] data02;
        static byte[] data03;
        static byte[] data04;
        static byte[] data05;
        static string str01;
        static string str02;
        static IPEndPoint ipe01;
        static string port01;
        static void Main(string[] args)
        {
            while (true)
            {
            label1:
                try
                {
                    string version = "V:1.1.1";
                    Console.Title = (" Portal Server " + version);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine();
                    Console.WriteLine("         PORTAL SERVER " + version);
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    data02 = new byte[1];
                    Console.Write(" Enter port for connecting clients : ");
                    port01 = Console.ReadLine();
                    Console.Write(" Enter path of file to send : ");
                    str01 = Console.ReadLine();
                    ipe01 = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port01));
                    tcp01 = new TcpListener(ipe01);
                    Console.Write(" Enter server message : ");
                    str02 = Console.ReadLine();
                    s01 = File.OpenRead(@str01);
                    length = s01.Length;
                    Console.WriteLine(" Computing optimum byte size...");
                    for (int i = 1; i <= 30000; i++)
                    {
                        if (s01.Length % i == 0) { int02 = i; }
                    }
                    if (length < 65000) { int02 = Convert.ToInt32(length); }
                    Console.WriteLine(" Done." + " Optimum byte size is " + int02);
                    tcp01.Start();
                    Console.WriteLine(" Server started. Waiting for clients...");
                    tcp02 = tcp01.AcceptTcpClient();
                    stream01 = tcp02.GetStream();
                    Console.WriteLine(" Client " + tcp02.Client.RemoteEndPoint.ToString() + " connected.");
                    data05 = Encoding.UTF8.GetBytes(str02);
                    stream01.Write(data05, 0, data05.Length);
                    System.Threading.Thread.Sleep(500);
                    data04 = Encoding.UTF8.GetBytes(str01);
                    stream01.Write(data04, 0, data04.Length);
                    System.Threading.Thread.Sleep(500);
                    data03 = Encoding.UTF8.GetBytes(length.ToString());
                    stream01.Write(data03, 0, data03.Length);
                    System.Threading.Thread.Sleep(500);
                    Console.WriteLine(" Waiting for response...");
                    stream01.Read((new byte[1]), 0, 1);
                    Console.WriteLine(" Received response...");
                    Console.WriteLine(" Sending file to " + tcp02.Client.RemoteEndPoint.ToString() + "...");
                    int03 = (Convert.ToInt32(length) / int02);
                    int04 = 0;
                    for (int01 = 0; int01 <= int03; int01++)
                    {

                        System.Threading.Thread.Sleep(1);
                        data01 = new byte[int02];
                        s01.Read(data01, 0, data01.Length);
                        stream01.Write(data01, 0, data01.Length);
                        stream01.Read(new byte[1], 0, 1);
                    }
                    s01.Close();
                    Console.WriteLine(" All data sent.");
                    Console.WriteLine(" Waiting for terminate message...");
                    stream01.Read((new byte[1]), 0, 1);
                    Console.WriteLine(" Received terminate message.");
                    tcp02.Close();
                    Console.WriteLine(" Stopping client and server.");
                    tcp01.Stop();
                    Console.WriteLine(" Done. Restarting.");

                }
                catch (Exception e)
                {
                    Console.WriteLine(" Error! : " + e.Message.ToString());
                    if (!(tcp02 == null)) { tcp02.Close(); }
                    if (!(tcp01 == null)) { tcp01.Stop(); goto label1; }
                }
            }
        }
    }
}

客户:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace PortalClient
{
    class Program
    {
        static Stream stream01;
        static byte[] data03;
        static int int04;
        static int int03;
        static int int02;
        static string str01;
        static int length;
        static IPEndPoint ipe01;
        static TcpClient tcp01;
        static FileStream s01;
        static string ip01;
        static string port01;
        static string path01;
        static byte[] data01;
        static byte[] data02;
        static byte[] data04;
        static byte[] data05;
        static void Main(string[] args)
        {

            while (true)
            {
            label1:
                {
                    try
                    {
                        string version = "V:1.1.1";
                        Console.Title = (" Portal Client " + version);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine();
                        Console.WriteLine("         PORTAL CLIENT " + version);
                        Console.WriteLine();
                        Console.ForegroundColor = ConsoleColor.Gray;
                        data01 = new byte[20];
                        data03 = new byte[100];
                        data04 = new byte[100];
                        Console.Write(" Enter IPv4 address of server : ");
                        ip01 = Console.ReadLine();
                        Console.Write(" Enter port to connect on : ");
                        port01 = Console.ReadLine();
                        ipe01 = new IPEndPoint(IPAddress.Parse(ip01), Convert.ToInt32(port01));
                        tcp01 = new TcpClient();
                        Console.WriteLine(" Connecting...");
                        tcp01.Connect(ipe01);
                        Console.WriteLine(" Done.");
                        stream01 = tcp01.GetStream();
                        Console.WriteLine(" Stream aquired.");
                        stream01.Read(data04, 0, data04.Length);
                        System.Threading.Thread.Sleep(100);
                        Console.WriteLine(" Server message : " + Encoding.UTF8.GetString(data04));
                        stream01.Read(data03, 0, data03.Length);
                        System.Threading.Thread.Sleep(100);
                        Console.WriteLine(" File on server : " + Encoding.UTF8.GetString(data03));
                        stream01.Read(data01, 0, data01.Length);
                        System.Threading.Thread.Sleep(100);
                        str01 = Encoding.UTF8.GetString(data01);
                        Console.WriteLine();
                        Console.WriteLine(" file size : " + str01);
                        Console.WriteLine();
                        Console.Write(" Enter the number you see above : ");
                        length = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine(" Computing optimum byte size...");
                        for (int i = 1; i <= 30000; i++)
                        {
                            if (length % i == 0) { int02 = i; }
                        }
                        if (length < 65000) { int02 = Convert.ToInt32(length); }
                        Console.WriteLine(" Done. Optimum byte size is " + int02);
                        int03 = (length / int02);
                        int04 = 0;
                        Console.Write(" Save file as : ");
                        path01 = Console.ReadLine();
                        s01 = File.OpenWrite(@path01);
                        Console.WriteLine(" Receiving file from " + tcp01.Client.RemoteEndPoint.ToString() + "...");
                        stream01.Write((new byte[1]), 0, 1);
                        System.Threading.Thread.Sleep(1000);
                        for (int i = 0; i <= int03; i++)
                        {

                            data02 = new byte[65000];
                            data05 = new byte[int02]; 
                            stream01.Read(data02, 0, data02.Length);
                            Buffer.BlockCopy(data02, 0, data05, 0, int02);
                            System.Threading.Thread.Sleep(1);
                            s01.Write(data05, 0, data05.Length);
                            stream01.Write(new byte[1], 0, 1);
                        }

                        Console.WriteLine(" Received all data.");
                        Console.WriteLine(" Press enter to disconnect...");
                        Console.ReadLine();
                        stream01.Write((new byte[1]), 0, 1);
                        Console.WriteLine(" Disconnecting...");
                        s01.Close();
                        stream01.Close();
                        tcp01.Close();
                        Console.WriteLine(" Done.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(" Error! : " + e.Message.ToString()); if (!(tcp01 == null)) { tcp01.Close(); } if (!(s01 == null)) { s01.Close(); goto label1; }
                    }
                }

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

检查Stream.Read()

上的documentation

您传递的最后一个参数是要读取的“最大”字节数,它并不能保证它会读取您在一次调用中要求的所有字节。

Read方法返回读取的实际字节数,然后您可以决定是否需要再次调用它以获得更多,如果您期望更多。您还需要管理后续字节的读取到您传递的数组中的正确位置等。