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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:21:28+00:00 2026-05-25T18:21:28+00:00

I had problem to transfer data over TCP. So i was writing a UDP

  • 0

I had problem to transfer data over TCP. So i was writing a UDP server, but its not working, shows this following error, how can i fix it?

My error:

run:
UDP Server started
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:657)
    at socket.UDPHandler.start(UDPHandler.java:25)
    at socket.UDPServer.waitForConnections(UDPServer.java:27)
    at socket.UDPServer.main(UDPServer.java:46)
BUILD STOPPED (total time: 14 seconds)

UDPServer.java

package socket;
import java.net.*;
import java.io.*;

public class UDPServer 
{
    private int serverPort = 0;
    private DatagramSocket serverSock = null;
    //private Socket sock = null;

    public UDPServer(int serverPort) throws IOException 
    {
        this.serverPort = serverPort;
        serverSock = new DatagramSocket(serverPort);
        System.out.println("UDP Server started");
    }

    public void waitForConnections() 
    {
        while (true) 
        {
            try {
                //sock = serverSock.accept();
                //System.err.println("Accepted new socket");
                UDPHandler handler = new UDPHandler(serverSock);
                handler.start();
            }
            catch (IOException e){
                e.printStackTrace(System.err);
            }
        }
    }

    public static void main(String argv[]) 
    {
        int port = 8889;

        UDPServer server = null;
        try {
            server = new UDPServer(port);
        }
        catch (IOException e){
            e.printStackTrace(System.err);
        }
        server.waitForConnections();
    }

}

UDPHandler.java

package socket;
import java.io.*;
import java.net.*;
import main.*;
public class UDPHandler implements Runnable 
{
    private DatagramSocket sock = null;
    private DatagramPacket sockInput = null;
    private DatagramPacket sockOutput = null;
    private Thread myThread = null;

    public UDPHandler(DatagramSocket sock) throws IOException 
    {
        this.sock = sock;
        //sockInput = new DatagramPacket();
        //sockOutput = sock.getOutputStream();
        this.myThread = new Thread(this);
    }

    public void start() 
    {
        myThread.start();
    }

    public void run() 
    {
        while(true) 
        {
            byte[] buf=new byte[1024];
            int bytes_read = 0;
            try {                                
                // Incoming - Test                
                sockInput = new DatagramPacket(buf, buf.length);
                sock.receive(sockInput);
                bytes_read = sockInput.getLength();                
                String data = new String(sockInput.getData());
                System.err.println("DATA: " +  bytes_read + " bytes, data=" +data);

                // IP - Test
                InetAddress IPAddress = sockInput.getAddress();
                int port = sockInput.getPort();

                // Sending - Test
                sockOutput = new DatagramPacket(data.getBytes(), data.length(), IPAddress, port);
                sock.send(sockOutput);

            } catch (Exception e) {
                e.printStackTrace(System.err);
                break;
            }
        }

        try {
            System.err.println("Closing socket.");
            sock.close();
        } catch (Exception e) {
            System.err.println("Exception while closing socket, e="+e);
            e.printStackTrace(System.err);
        }

    }

}
  • 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-25T18:21:29+00:00Added an answer on May 25, 2026 at 6:21 pm

    You are creating infinite number of threads in while(true) loop in waitForConnection() method. You should call DatagramSocket receive() method within your server (it is blocking operation) and if datagram is received, then delegate its processing to some handler (for example retrieved from thread pool).

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

Sidebar

Related Questions

I had a problem with set not working in a batch file; it took
I had this problem some time ago and I gave up but lately it
I had this problem before and can't for life of me remember how to
I had no problem at all running the following code on a local server,
I had a problem working with the image classes in java. I am creating
I had another question about this issue, but I didn't ask properly, so here
i'm using jquery in report section in my asp.net mvc project.But i had problem
EDIT I had a problem a while ago writing a program in C which
I was working on project that had problem when encoding UFT-8 text into JSON
I had this problem Cucumber scenarios for extremely long work flow And now I've

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.