Results 1 to 3 of 3
  1. #1
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Question Responses from UDP Server

    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.

    Java Code:
     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;
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: Responses from UDP Server

    You need to indent your code.
    That's hard to follow as it stands.

    Anyway, for (1):
    java <yourapp> 1234

    Java Code:
    public static void main(String[] args) {
        String port = args[0];
        // port will be == 1234 here.
    }
    You'll want to stick the usual check that you have some args supplied if they are supposed to be mandatory, or prompt at that point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Mnelson is offline Member
    Join Date
    Jun 2012
    Posts
    11
    Rep Power
    0

    Default Re: Responses from UDP Server

    Sorry about that! So would this work?


    public static void main(String[] args) throws Exception
    {
    int Part = 1;
    String port = args[0];
    DatagramSocket serverSocket = new DatagramSocket(port);

    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(Part == 1)

Similar Threads

  1. Replies: 1
    Last Post: 03-13-2012, 01:58 AM
  2. Replies: 6
    Last Post: 12-26-2011, 12:33 AM
  3. from local server to remote server
    By IDH in forum Java Servlet
    Replies: 1
    Last Post: 03-24-2011, 10:05 AM
  4. smtp server configuration with jboss server
    By vilas_patil in forum Java Servlet
    Replies: 0
    Last Post: 01-05-2009, 02:18 PM
  5. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 05:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •