|
-
July 6th, 2012, 08:23 AM
#1
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.
[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;
}[/code]
-
July 9th, 2012, 03:11 AM
#2
Re: Responses from UDP Server
Hi,
First of all: Please, use code tags properly. Learn how to use here. Click on "preview post" to ensure.
1) To read port from command line, just read args parameter:
Code:
public static void main(String[] args){
int port = 4567; // default
if (args.lenght == 1) {
port = Integer.parseInt(args[0]); // if parameter supplied, overwrite port
}
new DatagramSocket(port);
[...]
}
You can also ask in runtime using
Code:
Scanner sc = new Scanner(System.in);
int port = sc.nextInt();
Although, in my opinion it's not very "professional". Best option is reading from a properties file.
2) I'm not sure what is your problem. Is it distinguish between numbers and text? If so, you can use a regular expression to check it out. One of simplest way is try to parse into a number and catch if Exception is thrown. Example:
Code:
int numInts = 0;
int numTexts = 0;
[...] // Iterate over bytes or words received
try {
int value = Integer.parseInt(string);
numInts++;
}
catch (NumberFormatException e){
numTexts++;
}
[...]
System.out.println("Integers received: " + numInts);
System.out.println("Non-Integers received: " + numTexts);
Hope this helps!
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|