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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:34:25+00:00 2026-05-25T19:34:25+00:00

I am trying to send packets from one host to another peer host using

  • 0

I am trying to send packets from one host to another peer host using java UDP protocol.

The one host sends data, while the other reads it. The corresponding read however keeps blocking, thus receiving no data. I can see the sender packets going to the right destination using wireshark, but the receiver just wont pick it up. The read operation keeps blocking indefinitely.

Please help.
Code for cient:

//CLIENT CLASS
//Sections ommited....
DatagramSocket so = new DatagramSocket(port);
    protected void send(byte[] buffer,int packetsize){  
        DatagramPacket p;
        try {
            myClient.notifyInform("Sending data to "+inetaddress+" on"+port+"\n");
            p=new DatagramPacket(buffer,buffer.length,InetAddress.getByName(inetaddress),port);
            writeLock.lock();
            try{
                so.send(p);
            }finally{
                writeLock.unlock();
            }
        } catch (UnknownHostException e) {
            myClient.perror("Could not connect to peer:"+e.getMessage()+"\n");
            e.printStackTrace();
        } catch (IOException e) {
            myClient.perror("IOException while sending to peer.\n");
            e.printStackTrace();
        }
    }

    protected DatagramPacket read(){
        byte[] buf=new byte[bufsize];
        DatagramPacket p=new DatagramPacket(buf,buf.length);//TODO check these values, what should buffer be? just made it psize*10 for now
        readLock.lock();
        try{                
            myClient.notifyInform("receiving data\n");
            so.receive(p);
            this.myclient.notifyInform(String.valueOf(p.getData().length)+"\n");
        } catch (IOException e) {
            myClient.perror("IOException while reading from peer.\n");
            e.printStackTrace();
        }finally{
            readLock.unlock();
        }
        return p;
    }

    protected void beginRead() {
        while(active) {

            System.out.println("########################");
            byte[] data=this.read().getData();
            myClient.notifyInform("Receiving data\n");
        }

    }
    protected void beginSend(){
        forkWork(new Runnable(){

            @Override
            public void run() {

                byte[] sendBuffer=new byte[bufsize];
                int cnt;
                while(callActive){
                    try{
                        sourceLock.lock();
                        cnt=dataSource.read(sendBuffer, 0, bufsize);
                    }finally{
                        sourceLock.unlock();
                    }
                    if (cnt >0) {
                        send(sendBuffer, packetsize);
                    }
                }
            }

        });

    }

UPDATE:I made a mistake that I finally tracked down. After binding the port, and fixing that error, it now works.

  • 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-25T19:34:25+00:00Added an answer on May 25, 2026 at 7:34 pm

    You need to specify the port that the datagram socket is listening on like this:

     this.so = new DatagramSocket(SOME_NUMBER_HERE);
    

    and make sure you send it to the same port number in the send() method

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

Sidebar

Related Questions

I am trying to send packets from one machine to another using tcpreplay and
Since I am trying to send some packets within my home from one PC
For a project, I'm trying to send UDP packets from Linux kernel-space. I'm currently
Im trying to continuously send small UDP-packets (8 byte) as fast as possible from
I am trying to send audio file from one computer to other using socket
I am trying to send some data from a LINQ query in C# to
I'm trying to send a UDP datagram (containing a protocol buffers message) and getting
I've got a problem trying to send a request using QNetworkAccessManager from a QObject
I'm trying to push packets of data from my HTTP server to a browser,
I'm trying to create a DHCP Client using Java. The client will send Discover,

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.