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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:06:49+00:00 2026-06-16T17:06:49+00:00

I’m trying to send a couple of data via Sockets, so it’s converted to

  • 0

I’m trying to send a couple of data via Sockets, so it’s converted to bytes and then back to String on the Server. But I can only do one apparently.

Server code:

static void Read(IAsyncResult ar)
{
    int fileNameLen = 1;
    //int userNameLen = 9;

    State newState = (State)ar.AsyncState; //gets state of Socket
    Socket handler = newState.Socket_w; //passes Socket to handler

    int bytesRead = handler.EndReceive(ar); //terminates Data Receive from Socket.

    if (bytesRead > 0)
    {
        if (flag == 0)
        {
            fileNameLen = BitConverter.ToInt32(newState.buffer, 0); //gets filename length
            fileName = Encoding.UTF8.GetString(newState.buffer, 4, fileNameLen); //gets filename
            //userNameLen = BitConverter.ToInt32(newState.buffer, 8);
            //getUsername = Encoding.UTF8.GetString(newState.buffer, 8, fileNameLen);

            flag++;
        }
    }    
}

Client code:

internal static void uploadFile(string host, string username, string getGame, string filename, string filepath)
{
    byte[] m_clientData;
    Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    byte[] fileName = Encoding.UTF8.GetBytes(username + "_" + filename);
    byte[] fileData = File.ReadAllBytes(filepath);
    byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
    //byte[] sendUsername = Encoding.UTF8.GetBytes(username);
    //byte[] sendUsernameLen = BitConverter.GetBytes(sendUsername.Length);
    //byte[] sendGame = Encoding.UTF8.GetBytes(getGame);
    //byte[] sendGameLen = BitConverter.GetBytes(sendGame.Length);

    m_clientData = new byte[4 + fileName.Length + fileData.Length];

    fileNameLen.CopyTo(m_clientData, 0);
    fileName.CopyTo(m_clientData, 4);
    fileData.CopyTo(m_clientData, 4 + fileName.Length);

    //sendUsernameLen.CopyTo(m_clientData, 0);
    //sendUsername.CopyTo(m_clientData, 4);

    //sendGameLen.CopyTo(m_clientData, 0);
    //sendGame.CopyTo(m_clientData, 4);

    clientSock.Connect(host, 8889);
    clientSock.Send(m_clientData); //tofix exception
    clientSock.Close();
}

I can’t seem to decrypt it properly over on Server. Can anyone help me with the buffersizes and whatnot?

  • 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-16T17:06:51+00:00Added an answer on June 16, 2026 at 5:06 pm

    Read does not know anything about what was sent; TCP is basically just a stream – so there is absolutely nothing to say that you have all the data in one call to Read; you could have:

    • exactly the amount of data you sent
    • part of one message
    • 17 messages
    • the end of one message and the start of the next
    • 1 solitary byte from a message

    You need to devise some kind of framing protocol that lets the receiver know when they have an entire message. That could be as simple as a length prefix, or can be more complex. You should then buffer the data in memory (or process it gradually) until you have the entire message. One call to Read is very unlikely to represent a single and complete message. Indeed, this is guaranteed not to be the case if a message is larger than your newstate.buffer, but you can get the same result even for small messages and a large buffer.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.