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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:00:52+00:00 2026-06-17T04:00:52+00:00

I’m currently building a multi-threaded server / client in Java with UDP via datagram

  • 0

I’m currently building a multi-threaded server / client in Java with UDP via datagram sockets/packets. I’m having a hard time understanding the proper use of threads and would like some clarification. I’ll first give an example of what I’m doing.

Thread a;
Thread b(a);

a.start
b.start

//simple enough, now inside b imagine this,
Thread c(a);
if (case)
{
    c.start //therefore I can have a lot of thread c's running at once, 
}

//now inside c imagine this
if (case)
{
    a.somefunction();
}

Ultimately my question is very hard to ask, but is the above sudo appropriate use of threads? Even though there is only 1 thread a running at a time, it may be accessed from multiple other places concurrently. Will this cause issues?

Thanks for any responses.

-William

Just adding an edit for further clarification.

Thread a would be my packet sender, it sends packets from the server to the client.
Thread b would be my packet listener, it receives packets from the clients, and sends them off to thread C, the packet parser. (so I can parse multiple packets at the same time).
Thread c, the packet parser, may need to send a response back to the client, so it would invoke a function in a which queues a packet up to be sent off.

Thanks again,

Edit again,

sample thread using functions

package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Vector;

public class ServerSenderThread extends Thread
{
    DatagramSocket serverSocket;
    Vector<DatagramPacket> outGoingPackets = new Vector<DatagramPacket>();

    public ServerSenderThread(DatagramSocket serverSocket)
    {
        this.serverSocket = serverSocket;
    }

    public void run()
    {
        while (true)
        {
            if (outGoingPackets.size() == 0)
            {
                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                try
                {
                    send();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public void addSend(DatagramPacket packet)
    {
        outGoingPackets.addElement(packet);
    }

    public void send() throws IOException
    {
        DatagramPacket packet = outGoingPackets.get(0);
        outGoingPackets.removeElementAt(0);

        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        byte[] buf = new byte[256];
        String dString = "Data Only the Server Knows";
        buf = dString.getBytes();
        packet = new DatagramPacket(buf, buf.length, address, port);

        System.out.println("Sserver sending packet");   
        serverSocket.send(packet);

    }

}
  • 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-17T04:00:53+00:00Added an answer on June 17, 2026 at 4:00 am

    The sending and receiving of packet is generally simple unless you have a high rate e.g. 10+K/second. The processing of these packets could take some time but unless this is really expensive (much more than parsing) I would consider using one thread for all these functions. It will simplify the code and make debugging it easier. i.e. I would make the design as simple as possible unless you know you need to make it more complicated.

    If you compare the single threaded version of what you have above, you can see it’s much simpler which is a clear benefit whereas using multiple threads in this situation is not a clear benefit.

    public class DataPacket {
        final DatagramSocket serverSocket;
    
        public DataPacket(InetAddress address, int port) throws SocketException {
            this.serverSocket = new DatagramSocket(port, address);
        }
    
        public void send(String message) throws IOException {
            byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
            serverSocket.send(new DatagramPacket(bytes, bytes.length));
            System.out.println("Sent " + message);
        }
    }
    
    • 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
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox 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.