Results 1 to 5 of 5
  1. #1
    IandI is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default [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

    Java 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);
    	}
    	}
    }
    The receiving station code

    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
    			}	
    	}
    	}

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    IandI is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    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,
    CJSL
    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"
    Java Code:
    String hostname = "LocalHost";  //tried both for case sensitive sake
    		InetAddress address = InetAddress.getByName(hostname);
    2. I also tried it by removing the variable "hostname" altogether and using the "getLocalHost" instead.

    Java Code:
    	InetAddress address = InetAddress.getLocalHost();
    Both attempts as you said leave myclient hanging.

    "So, is the host name "vista" correct?" It is

    Where am I going wrong? Thanks
    Last edited by IandI; 03-17-2009 at 09:39 PM.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Sorry about that... there's another thing that I did... I replaced the following in myClient:
    Java Code:
    String msg = new String(buffer,0,p.getLength());
    For this:
    Java Code:
    String msg = new String(p.getData());
    Sorry about that... I forgot about that change...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    IandI is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Sorry about that... there's another thing that I did... I replaced the following in myClient:
    Java Code:
    String msg = new String(buffer,0,p.getLength());
    For this:
    Java Code:
    String msg = new String(p.getData());
    Sorry about that... I forgot about that change...

    Luck,
    CJSL
    Thanks again....but "myclient" still seem to hang.

Similar Threads

  1. Monitoring the network
    By khajalid in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-21-2008, 10:22 AM
  2. Monitoring the network
    By khajalid in forum New To Java
    Replies: 4
    Last Post: 09-10-2008, 02:55 PM
  3. database+network
    By hidar in forum JDBC
    Replies: 4
    Last Post: 06-30-2008, 10:04 AM
  4. Using Datagrams to Get the Date
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:09 PM
  5. Non Blocking Network
    By mathias in forum Networking
    Replies: 1
    Last Post: 08-07-2007, 07:49 AM

Posting Permissions

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