Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Server and Client in Java problem

  1. Nov 12, 2013 #1
    I need some guidance on how to Bind a UDP socket to a port and then read from server.

    The server is already done I just need help with the client here is what I have so far.


    One more thing, I dont know if I am using the ports right, 49009 is the server port and 12000 is the client port. I think I have all of them in the right place but if I dont will you correct me, thank you.
    Code (Text):
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;


    public class UDPClient
    {

        public static void main(String[] args) throws Exception
        {
            // read some input from user
            BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
           
            // create a udp socket
            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getLocalHost();
           
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
         
          //Creating a listening socket from local port
          DatagramSocket listeningSocket = new DatagramSocket(12000);
           
            // Specifying data request and port
            String sentence = "DATA REQUEST:12000";
            sendData = sentence.getBytes();
           
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 49009);
            clientSocket.send(sendPacket);
         
         
           
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            listeningSocket.receive(receivePacket);
           
            String modifiedSentence = new String(receivePacket.getData());
           
            System.out.println(modifiedSentence);

            clientSocket.close();
          listeningSocket.close();
         
           
        }

    }
     
    Last edited: Nov 12, 2013
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Server and Client in Java problem
  1. Client server (Replies: 2)

Loading...