Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:20:28+00:00 2026-06-18T06:20:28+00:00

im having some trouble with a file server that i made in c#. The

  • 0

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

            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:20:29+00:00Added an answer on June 18, 2026 at 6:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble parsing a *.CSV file in windows server 2008 64bit version.
I am following a tutorial to download file from server. But having some trouble.
I've been having some trouble trying to send a file from a server to
Im having some trouble with my host file.. If I change the IP for
I've been having some trouble with my .htaccess file lately (my entire site goes
I'm having some trouble reading data from a file into a vector of Orders.
I am having some trouble trying to print from a file. Any ideas? Thanks
I'm having some trouble with replacing a portion of a file in binary mode.
I'm having some trouble with some struct typedef declarations in a header file not
I'm having some trouble with Mongodb and Python (Flask). I have this api.py file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.