gitech 0 Newbie Poster

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;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.