1 Reply Latest reply: Jul 6, 2012 11:51 AM by Kayaman RSS

    Responses from UDP Server

    941118
      Hi Ive made the following program I'm just looking for a little help on how to fix the following:

      1)I'm stuck trying to figure out how to supply the listening port number as a command line argument when starting the program.
      )For example I want it to ask for a port number when I start the program and use that specified port.


      2)The second thing im trying to do is have the server report back a string that tells the client how many integers and non-integer words were in the previous message.
      )For example I send the following message from my client to my server "Hi today is July 6"
      I want my server to return a string "4 1"
      Indicating that the incoming string has 4 non integer words and 1 integer.

      Here is my code.


      [code=java] public static void main(String[] args) throws Exception
      {
      int kimbo= 1;
      DatagramSocket serverSocket = new DatagramSocket(4567);
                InetAddress myIp =InetAddress.getLocalHost();
      byte[] receiveData = new byte[1024];
      byte[] sendData = new byte[1024];
                System.out.println("Using port: " + serverSocket.getLocalPort() );
                System.out.println("IP: " + myIp.getHostAddress() );
      while(kimbo== 1)
      {
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      serverSocket.receive(receivePacket);
      String Str= new String(receivePacket.getData());
      if(Str.contains("1"))
      {

      System.out.println("The received string was: " + Str);
      InetAddress IPAddress = receivePacket.getAddress();
      int port = receivePacket.getPort();
      String capitalizedSentence = line.toUpperCase();
      sendData = capitalizedSentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
      serverSocket.send(sendPacket);
      }
      else if(Str.contains("2"))
      {
      DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
      Calendar cal = Calendar.getInstance();
      System.out.println("The received string was: " + Str);
      InetAddress IP2 = receivePacket.getAddress();
      int port2 = receivePacket.getPort();
      String time = dateFormat.format(cal.getTime());
      sendData = time.getBytes();
      DatagramPacket sendPacket2 =
      new DatagramPacket(sendData, sendData.length, IP2, port2);
      serverSocket.send(sendPacket2);
      }
      else
      {
      System.out.println("The received string was: " + Str);
      System.out.println("Exiting");
      kimbo =0;
      }
        • 1. Re: Responses from UDP Server
          Kayaman
          938115 wrote:
          1)I'm stuck trying to figure out how to supply the listening port number as a command line argument when starting the program.
          The arguments are in String[] args, what problems are you having?
          2)The second thing im trying to do is have the server report back a string that tells the client how many integers and non-integer words were in the previous message.
          )For example I send the following message from my client to my server "Hi today is July 6"
          I want my server to return a string "4 1"
          Indicating that the incoming string has 4 non integer words and 1 integer.
          And? What exact problems are you having with that?