Question

im having some trouble with a file server that i made in c#. The problem is that when i run both the server and the client in my computer, they work fine, but when i use the client on another computer, the client receives the file before the server can finish sending it. any input would help. thanks.

server:

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

client :

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

            }
        }
    }
}
Était-ce utile?

La solution

Check the documentation on Stream.Read()

The last parameter you pass is the 'maximum' number of bytes to read, it doesnt guarantee it'll read all the bytes you ask for in a single call.

The Read method returns the actual number of bytes read, and from that you can decide if you need to call it again to get any more if you were expecting more. You'll also need to manage the reading of the subsequent bytes to the correct place in the array you pass, etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top