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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:38:56+00:00 2026-06-09T08:38:56+00:00

I am using a Java socket program to write integers and characters from the

  • 0

I am using a Java socket program to write integers and characters from the server back to the client

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

I want to write an int32 and a char of 8B on the wire. For the integer when I write

    out.printf("%04x",1);

it appears on the wireshark as “30 30 30 31”, whereas I want it to appear “01 00 00 00”. Similarly for out.printf(“%d”,1); it appears as “31” whereas I want it “01 00 00 00”. How can I write on the wire an integer of 4B and a character of 8B? Does the representation has to do with encoding?

  • 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-09T08:38:59+00:00Added an answer on June 9, 2026 at 8:38 am

    Using PrintWrite to write text to a socket is not ideal as it will hide any errors.

    In your case you appear to want to write binary in so I would suggest you try.

    DataOutputStream out = new DataOutputStream(
                           new BufferedOutputStream(clientSocket.getOutputStream()));
    
    out.writeInt(1);
    

    However if you want to sue little endian format, I would use blocking NIO.

    SocketChannel sc = 
    ByteBuffer bb= ByteBuffer.allocateDirect(1024).order(ByteOrder.LITTLE_ENDIAN);
    
    bb.putInt(1);
    // put whatever else
    bb.flip();
    while(bb.remaining() > 0) sc.write(bb);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My TCP server is implemented using Netty. My client using vanilla java.net.Socket to connect
I have developed a Java server using Eclipse that accepts TCP socket connection from
I am writing a Java client/server GUI application using sockets and here is the
In my program using java nio, the socketchannel.write() becomes very slow when it tries
I have a Java-program which communicates with a C++ program using a socket on
I have written a simple server - client program with Swing interface using singleton
I'm currently writing a jdbc client-server socket application in java. I've been having a
I'm writing a program using Java non-blocking socket and TCP. I understand that TCP
I am trying to connect to server using a Java socket. I am trying
I'm writing my first java client/server program which just establishes a connection with the

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.