Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

UDP: Using NetBeans: Client not Receiving

Zulfi KhanOct 14 2016

Hi,

I  have created a simple UDP client server application using socket programming. The server takes the input and when character ‘a’ is pressed followed by “Enter” key, data is transferred to the client which displays it. This is working fine in the DOS environment by opening two command windows. However when I am trying to run it in NetBeans, it does not show me any output on the client window. In the output section, it takes input from server but does not transfer it to the client portion shown adjacent to server portion in the output window. My server code is:

import java.net.*;

/**

*

* @author HP

*/

public class Server1main {

        public static int serverPort = 666;

                public static int clientPort = 999;

                public static int buffer_size=1024;

                public static DatagramSocket ds;

                public static byte buffer[ ] = new byte [buffer_size];

    /**

     * @param args the command line arguments

     */

    public static void TheServer ( ) throws Exception{

                int pos=0;

                while (true) {

                                int c=System.in.read ( );

                                if(c=='a'){

                                                ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost ( ),clientPort));

                                                pos=0;

                                               

                                }

                                                                buffer[pos++] = (byte) c;

                                               

                                }

                }

    public static void main(String[] args) throws Exception{

        // TODO code application logic here

        ds = new DatagramSocket(serverPort);

                TheServer( );

    }

   

}

And the client code is:

import java.net.*;

/**

*

* @author HP

*/

public class Client1Main {

        public static int serverPort = 666;

                public static int clientPort = 999;

                public static int buffer_size=1024;

                public static DatagramSocket ds;

                public static byte buffer[ ] = new byte [buffer_size];

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws Exception{

        // TODO code application logic here

        TheClient( );

    }

public static void TheClient ( ) throws Exception{

                ds = new DatagramSocket(clientPort);

                while (true) {

                                DatagramPacket p = new DatagramPacket(buffer, buffer.length);

                                ds.receive ( p);

                                System.out.println(new String( p.getData( ), 0, p.getLength ( )));

                }

    }

}

Some body please guide me.

Zulfi.

Comments

865185

Very good article for a docker starter.

3387467

Me gustó, bastante clara la explicación para cualquier persona que quiera iniciarse en tecnología Docker.

1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 11 2016
Added on Oct 14 2016
0 comments
389 views