0

私は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; }
                    }
                }

            }
        }
    }
}
4

1 に答える 1

1

Stream.Read()のドキュメントを確認してください

渡す最後のパラメータは、読み取る「最大」バイト数です。これは、1回の呼び出しで要求するすべてのバイトを読み取ることを保証するものではありません。

Readメソッドは、実際に読み取られたバイト数を返します。これから、さらに多くのことを期待している場合は、それを再度呼び出す必要があるかどうかを判断できます。また、渡す配列内の正しい場所への後続のバイトの読み取りなどを管理する必要があります。

于 2013-02-04T04:38:14.033 に答える