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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:15:37+00:00 2026-06-12T11:15:37+00:00

I’m starting to write my first Java networking program, and long story short I’m

  • 0

I’m starting to write my first Java networking program, and long story short I’m having difficulty making sure that I’m taking the right approach. Our professor has given us a server program to test against this UDP client, but I’m getting some errors I can’t seem to squash. Specifically, I get IO exceptions, either “Connection Refused” or “No route to host” exceptions.

public class Lab2Client {
/**
 * @param args[1] == server name, args[2] == server port, args[3] == myport
 */
public static void main(String[] args) {
    //Serverport is set to 10085, our client is 10086
    try {
        Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
        System.out.println("Server connection Completed\n");
        DataOutputStream output = new DataOutputStream(echoSocket.getOutputStream());
        byte[] toSend = new byte[5];
        toSend[0] = 12; toSend[1] = 34;//Code Number
        toSend[2] = 15;//GroupId
        toSend[3] = 86;toSend[4] = 100;//Port number in Little Endian Order
        output.write(toSend);

        System.out.println("Sent Request. Waiting for reply...\n");

        DataInputStream input = new DataInputStream(echoSocket.getInputStream());

        byte[] toRecieve = new byte[]{0,0,0,0,0,0,0,0}; 
        input.read(toRecieve);
        checkMessage(toRecieve);            
    }
    catch (UnknownHostException e) {
        System.err.println("Servername Incorrect!");
        System.exit(1);
    }
    catch (IOException e){
        System.err.println("IO Exception. Exiting...");
        System.err.println(e);
        System.exit(1);
    }
}

I also have some questions about my implementation regarding receiving messages in Java. I’ll be getting a datagram that contains either:

a) 3 formatting bytes (unimportant to the question) along with an IP and port number

or

b) 3 formatting bytes and a port.

Is using a DataInputStream the correct way to do this? I know using an array with 9 elements is lazy instead of dynamically allocating one that’s either 5 or 9, but right now I’m just trying to get this working. That being said, is there a different approach anyone would suggest for this?

  • 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-12T11:15:38+00:00Added an answer on June 12, 2026 at 11:15 am

    thought I’d leave this up for posterity. The problem is simple, and I’m a fool for not noticing it sooner.

    The correct programs I was testing this against used the UDP protocol, and this program is written in TCP. The corrected code is:

    public class Lab2Client {
    /**
     * @param args[0] == server name, args[1] == server port, args[2] == myport
     */
    public static void main(String[] args) {
        //Serverport is 10085, our client is 10086
        try {
            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getByName(args[0]);
            int portToSend = Integer.parseInt(args[2]);
            System.out.println("Clent Socket Created");
            byte[] toSend = new byte[5];
            toSend[0] = 0x12; toSend[1] = 0x34;//Code Number
            toSend[2] = 15;//GroupId, f in hex
            toSend[3] = 0x27;toSend[4] = 0x66;
            System.out.println("Byte Array Constructed");
    
            DatagramPacket sendPacket = new DatagramPacket(toSend, toSend.length, IPAddress, Integer.parseInt(args[1]));
            clientSocket.send(sendPacket);          
            System.out.println("Sent Request. Waiting for reply...\n");
    
            DataInputStream input = new DataInputStream(echoSocket.getInputStream());
            toRecieve can either be an error message, a return of what we sent,
            or a byte stream full of IP info and port numbers.
            the "heavy" byte stream is either 4 for IPv4 of 16 for IPv6, 2 bytes for port,
            and the magic number (2 bytes) for a total of 9-20 bytes*/
    
            byte[] toRecieve = new byte[9];
            DatagramPacket receivePacket = new DatagramPacket(toRecieve, toRecieve.length);
            clientSocket.receive(receivePacket);            
            checkMessage(toRecieve);            
        } //and so on and so forth...
    

    Thanks to @Serge for the help, though nobody could have answered my question correctly with how I asked it. The byte shifting you suggested was important too.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.