Skip to Main Content

UDP: Using NetBeans: Client not Receiving

User_AYF65Oct 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.

Post Details
Locked due to inactivity on Nov 11 2016
Added on Oct 14 2016
0 comments
266 views