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 9242975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:42:16+00:00 2026-06-18T08:42:16+00:00

Using C# in .NET 4.5 in Visual Studio 2012 Ult on Win7 x64. I’m

  • 0

Using C# in .NET 4.5 in Visual Studio 2012 Ult on Win7 x64.

I’m trying to make a server transmit files via the System.Net.Sockets.Socket.SendFile(string filename) method and a client to receive it. The client appends received bytes to the end of the file it creates to reconstruct the sent file on its side. This all works – at least for basic text files.

When I try sending an *.exe file, the received *.exe doesn’t work. Opening both the source *.exe and client-side copy in Notepad++, it appears that the file is correct except that some of the weird-looking characters aren’t correct. My question is how do I fix this?

Since the server code is really just sending the file using Socket.SendFile(string filename) with no other relevant options available, I’m assuming that the client is the problem.

On the client side:

private void ListenAndReport(Socket socket)
{
    EndPoint remoteEndPoint = socket.RemoteEndPoint;
    ReceiveObject obj = new ReceiveObject(socket);
    socket.BeginReceive(obj.buffer, 0, packetSize, SocketFlags.None, new AsyncCallback(ReceiveFromListen), obj);
}
private void ReceiveFromListen(IAsyncResult ar)
{
    ReceiveObject obj = (ReceiveObject)ar.AsyncState;  //ReceiveObject just holds a byte[packetSize] buffer and a Socket

    string received = Encoding.UTF8.GetString(obj.buffer);
    WriteToFile(received);

    obj.socket.EndReceive(ar); //!!! necessary?  Doesn't seem to make a difference.
    ListenAndReport(obj.socket);
}
private void WriteToFile(string text)
{
    fileStream.Write(text);
    fileStream.Flush();
}

My guess is that the encoding in string received = Encoding.UTF8.GetString(obj.buffer); is incorrect, but I’ve tried all of the Encoding.something options, and none of them have correctly transmitted the file.

Alternatively, maybe the file writer, System.IO.TextWriter fileStream = File.AppendText(string filename);, is the problem?

  • 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-18T08:42:17+00:00Added an answer on June 18, 2026 at 8:42 am

    This is the problem:

    string received = Encoding.UTF8.GetString(obj.buffer);
    

    Why are you doing this? It’s not text. You should be writing a byte array to the file, not a string.

    Additionally, you should be using the return value of EndReceive to know how much data you’ve read:

    int bytesRead = obj.socket.EndReceive(ar);
    fileStream.Write(obj.buffer, 0, bytesRead);
    

    (fileStream should be an actual stream, not a TextWriter.)

    This will work for text files as well – if you treat text as binary, you end up with the same content at the other end, which you can still read as text. If you treat binary data as text, you end up with a mess of corrupted data…

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

Sidebar

Related Questions

Am trying to post json data to the server. am using visual studio 2012
Using: Visual Studio 2012, .NET Framework 4, Windows 7 x64 Sometimes, upon starting my
I'm using ASP.Net with Visual Studio 2012/.NET 4.5 and here's the situation: I have
I am debugging my ASP.Net app using Visual Studio 2012 (IE10) and looks like
I'm working on Asp.net MVC4 project using Visual Studio 2012. When there's an error
How to implement the OData in ASP .Net Web API using Visual Studio 2012?
I've developing a Windows 8 app using .NET 4.5 in visual studio 2012 ultimate.
I've been using Microsoft.Web.Optimization package for some time via the Visual Studio 2012 trial
I am using Visual Studio 2012 and .NET 4.5 and the new WebAPI project
Using Visual Studio 2012, I created a new ASP.NET MVC 4 web project, using

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.