Results 1 to 5 of 5
- 03-17-2009, 09:39 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
[SOLVED] Network Programming Using Datagrams
Hello Coders,
I'm trying to write a simple application with two programs running on different computers on the same network. The idea is to send a string of text to one machine which then replies with another. I having trouble getting it to work despite serious reviews/investigation.
What I've done to try and rectify the problem
1. I noticed some activity with the firewall when trying to run the program and decided to disable the firewall on both computers with no luck.
2. I used the net view command which shows both the computers.
3. I can also ping them both
4. I've gone over the code a number of times - and suspect/convinced the
issue lies within it.
5. I kindly request your assistance
The receiving station codeJava Code:import java.net.*; public class myServer { //send a message to another computer via datagram public static void main(String []args) { try { String hostname = "vista"; InetAddress address = InetAddress.getByName(hostname); int port = 1234; String message = "Who am I"; byte[] data = message.getBytes(); DatagramSocket s = new DatagramSocket(); DatagramPacket p = new DatagramPacket(data,data.length,address,port); s.send(p); System.out.println("Packet sent2"); s.close(); } catch(UnknownHostException e) { System.exit(0); } catch(SocketException f) { System.exit(0); } catch(java.io.IOException g) { System.exit(0); } } }
Java Code:import java.net.*; import java.io.*; import java.lang.String.*; public class myClient { public static void main(String args[]) { try { byte[] buffer = new byte[4096]; //to contain bytes in the datagram packet. DatagramSocket s = new DatagramSocket(1234); //create socket to recieve & send datagrams. DatagramPacket p = new DatagramPacket(buffer,buffer.length); s.receive(p); String msg = new String(buffer,0,p.getLength()); s.close(); } catch(SocketException e) //Exception Handler { System.exit(0); //clean up } catch(java.io.IOException e) //Exception Handler { System.exit(0); //clean up } } }
- 03-17-2009, 12:58 PM #2
I tried the code and it worked OK if I replaced the variable hostname with "localhost" since I was running on the same computer. Without that change, I was seeing that myServer was finishing and myClient was still waiting to receive data. So, is the host name "vista" correct?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-17-2009, 05:57 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Thanks for the replying.
I still seem to have issues. I tried following your suggestions and running it on the same computer but myClient was still waiting to receive data. Using the same computer, I tried a couple of things
1. I replaced the variable hostname with "localhost"
2. I also tried it by removing the variable "hostname" altogether and using the "getLocalHost" instead.Java Code:String hostname = "LocalHost"; //tried both for case sensitive sake InetAddress address = InetAddress.getByName(hostname);
Both attempts as you said leave myclient hanging.Java Code:InetAddress address = InetAddress.getLocalHost();
"So, is the host name "vista" correct?" It is
Where am I going wrong? ThanksLast edited by IandI; 03-17-2009 at 08:39 PM.
- 03-17-2009, 11:14 PM #4
Sorry about that... there's another thing that I did... I replaced the following in myClient:
For this:Java Code:String msg = new String(buffer,0,p.getLength());
Sorry about that... I forgot about that change...Java Code:String msg = new String(p.getData());
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-17-2009, 11:24 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Monitoring the network
By khajalid in forum Threads and SynchronizationReplies: 1Last Post: 12-21-2008, 09:22 AM -
Monitoring the network
By khajalid in forum New To JavaReplies: 4Last Post: 09-10-2008, 01:55 PM -
database+network
By hidar in forum JDBCReplies: 4Last Post: 06-30-2008, 09:04 AM -
Using Datagrams to Get the Date
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:09 PM -
Non Blocking Network
By mathias in forum NetworkingReplies: 1Last Post: 08-07-2007, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks