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

Java 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);
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            String modifiedSentence = new String(receivePacket.getData());


    Last edited: Nov 12, 2013
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

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