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

The Archive Base Latest Questions

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

I’ve a server in Java which listens the request on the UDP socket port

  • 0

I’ve a server in Java which listens the request on the UDP socket port 100 and puts it’s response on the UDP socket port 200.

Now I need to write a client in PHP which should put it’s request on the port 100 and should receive the response on 200.

My program is able to send the request to the server on the port 100 and the server is also putting it’s response on the port 200. But my PHP program is not receiving the response. It is going into a waiting state.

Interesting part is, if I send response on to the same port on which the request came, my PHP code is able to receive. But the server design is to use two ports. One incoming and one outgoing.

Here is my PHP code

<?php
class SocketHandle {
    public function requestService($message) {
        //  Choose proper domain
        $domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
        $socketHandle = socket_create($domain, SOCK_DGRAM, SOL_UDP);
        $serverIP = "127.0.0.1";
        $serverSendPort = 100;
        $serverRecvPort = 200;
        socket_sendto($socketHandle, $message, strlen($message), 0, $serverIP, $serverSendPort);
        $response = "";
        socket_recvfrom($socketHandle, $response, strlen($message), 0, $serverIP, $serverRecvPort);
        return $response;
    }
}
?>

and my Java server code is

class UDPServer { 
    public static void main(java.lang.String args[]) throws java.lang.Exception{ 
        try{ 
            java.net.DatagramSocket serverSocket = new java.net.DatagramSocket(100); 
            java.net.DatagramSocket serverSocket200 = new java.net.DatagramSocket(200); 
            byte[] receiveData = new byte[1024]; 
            byte[] sendData  = new byte[1024]; 

            while(true){ 

                receiveData = new byte[1024]; 

                java.net.DatagramPacket receivePacket = new java.net.DatagramPacket(receiveData, receiveData.length); 

                java.lang.System.out.println ("Waiting for datagram packet");

                serverSocket.receive(receivePacket); 

                java.lang.String sentence = new java.lang.String(receivePacket.getData()).trim(); 

                java.net.InetAddress IPAddress = receivePacket.getAddress(); 

                int port = receivePacket.getPort(); 

                java.lang.System.out.println ("From: " + IPAddress + ":" + port);
                java.lang.System.out.println ("Message: " + sentence);

                java.lang.String capitalizedSentence = sentence.toUpperCase(); 
                sendData = capitalizedSentence.getBytes(); 

                //java.net.DatagramPacket sendPacket = new java.net.DatagramPacket(sendData, sendData.length, IPAddress,  port); 
                java.net.DatagramPacket sendPacket = new java.net.DatagramPacket(sendData, sendData.length, IPAddress, 200);

                java.lang.System.out.println("Sending : " + capitalizedSentence);

                //serverSocket.send(sendPacket); 
                serverSocket200.send(sendPacket);
            } 

        }catch (java.net.SocketException ex) {
            java.lang.System.out.println("UDP Port 100 / 200 is occupied.");
            java.lang.System.exit(1);
        }
    } 
}

Here is the output of my Java server

>java UDPServer

Waiting for datagram packet
From: /127.0.0.1:54491
Message: Hello
Sending : HELLO
Waiting for datagram 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-05-28T18:08:24+00:00Added an answer on May 28, 2026 at 6:08 pm

    In Linux, it is not listening on ports below 1024. If you use a port above 1024, it will work.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.