How to set Timeout for DatagramConnection in J2ME

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 22
Reputation: gitech is an unknown quantity at this point 
Solved Threads: 0
gitech gitech is offline Offline
Newbie Poster

How to set Timeout for DatagramConnection in J2ME

 
0
  #1
Jun 2nd, 2009
Hai Friends, iam using udp connection for my project in j2me, its working fine, but i doknow how to set timeout for the Datagram connection. because there no server means it doesnot give exception.
my code is given below
public String UDP_Client(String str)
	{
	String UDP_RString="";
	String UDP_Senddata = str;
	DatagramConnection conn = null;
       try {
			String ServerAdd = "datagram://"+UDP_ServerIP+":"+UDP_ServerPort;
            conn = (DatagramConnection)Connector.open(ServerAdd,Connector.READ_WRITE, false);
            Datagram dg = conn.newDatagram(1000);
//			String senddata = "JA_LGNDTLS 100002~100002~1.0.0ALPHA~No Mac Address~LINUXJAVA~ ~1111111111111111~23870BD7A92F6D~1~THEA,jaTHEA.BIL,19db6b53|~0";
            dg.setData(UDP_Senddata.getBytes(), 0, UDP_Senddata.length());
            conn.send(dg);
			//conn.setTimeout(3000);
            
			String Str="";
			while (true)
				{
				 Datagram rdg = conn.newDatagram(1000);
				 conn.receive(rdg);
                if (rdg.getLength() > 0)
					{
                   // si.setText("Message received - " + new String(dg.getData(), 0, dg.getLength()));
				    Str = (new String(rdg.getData(), 0, rdg.getLength()));
					conn.close();
					//conn.disconnect();
					UDP_RString = Str;
                    }
              }
        }
		catch (ConnectionNotFoundException cnfe )
			{
            System.out.println(cnfe);
			UDP_RString = "*";
            }
		catch (Exception ex)
			{
             System.out.println(ex);
			 UDP_RString = "*";
            }
			System.out.println("out");
			return UDP_RString;
	}
Last edited by Tekmaven; Jun 3rd, 2009 at 3:18 am. Reason: Code Tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC