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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:35:41+00:00 2026-05-27T23:35:41+00:00

I am building an experimental game server in Java . Client sends: length: 22\r\n

  • 0

I am building an experimental game server in Java.

Client sends:

length: 22\r\n
playerId: 0\r\n
\r\n\r\n
this is a test message

Java code to receive message:

String message = "";

byte[] buf = new byte[20];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socketServer.receive(packet);
message += new String(buf, "UTF8");

buf = new byte[20];
packet = new DatagramPacket(buf, buf.length);
socketServer.receive(packet);
message += new String(buf, "UTF8");

This doesn’t receive 40 bytes of the message sent by the client, it receives 20 bytes and on the second socketServer.receive(packet) it hangs for a new message.

Reason by def:

This method blocks until a datagram is received. The length field of
the datagram packet object contains the length of the received
message. If the message is longer than the packet’s length, the
message is truncated.

I need to be able to read 20 bytes of the message and another 20 bytes of the message. How can this be achieved?

I need this because I’m not sure how long the message will be. I want something like this:

String message = "";
while (!message.contains("\r\n\r\n")) { //"\r\n\r\n" marks end of message
    byte[] buf = new byte[20];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socketServer.receive(packet);
    message += new String(buf, "UTF8");
}

This gets full message assuming message ends with \r\n\r\n.

It needs to be done over the UDP protocol. I am new to sockets in Java but I assume there is something for this, just cannot find it. Thanks in advance.

  • 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-05-27T23:35:42+00:00Added an answer on May 27, 2026 at 11:35 pm

    UDP isn’t designed for variable length reads – each read() [or recv()] will correspond to a single write() [or send()] on the server.

    If you only read 20 bytes of a 40 byte packet the remaining bytes will be discarded.

    You need to create a buffer large enough to receive the entire packet (max 65536 bytes if your network handles fragmentation properly) and then read the entire packet in one go.

    The API will tell you how many bytes were actually received – you should consider checking that against the value encoded within the packet to ensure that you did receive the entire packet.

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

Sidebar

Related Questions

Below is partial code to an experimental http server app I'm building from scratch
Building a client-side swing application what should be notified on a bus (application-wide message
I'm building a small experimental application using BlazeDS AMF, Java and Hibernate, and I
Building on what has been written in SO question Best Singleton Implementation In Java
Basically, I'm building a small tracker for experimental purposes. I've gotten quite far, and
I'm trying to experiment with cocos2d by building a simple card game. I'm not
Im building this Digital Signage prototype for a master thesis. Im using WPF and
Building a website. When I order my tags like this, LightCycle works but Lightbox
Building a GTK+ widget dynamically from code allows for easy access to the child
Building a lottery scratch-card app in Java. Have allot of it done, just need

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.