Results 1 to 3 of 3
Thread: Responses from UDP Server
- 07-06-2012, 03:22 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 11
- Rep Power
- 0
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; }
- 07-06-2012, 03:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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
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.Java Code:public static void main(String[] args) { String port = args[0]; // port will be == 1234 here. }Please do not ask for code as refusal often offends.
- 07-06-2012, 03:43 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 11
- Rep Power
- 0
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
-
Question based on MVC - require responses ASAP please
By icedy in forum New To JavaReplies: 1Last Post: 03-13-2012, 12:58 AM -
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorre
By nandhinianand in forum New To JavaReplies: 6Last Post: 12-25-2011, 11:33 PM -
from local server to remote server
By IDH in forum Java ServletReplies: 1Last Post: 03-24-2011, 09:05 AM -
smtp server configuration with jboss server
By vilas_patil in forum Java ServletReplies: 0Last Post: 01-05-2009, 01:18 PM -
Does any file in an FTP server ends up in an HTTP server?
By islamfunny in forum CLDC and MIDPReplies: 4Last Post: 08-15-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks