BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 06-05-2008, 03:44 AM   #1
kabrator
New Member
 
Join Date: Jun 2008
Location: Valencia, Spain
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 2
Default Udp

Please Login to Remove!

Hi!
I'm new in this forum and i new too programming in BB. I have a question about UDP. I search about UDP connection here and i find this:
PHP Code:

/*
 * RIMJUDPClient.java
 *
 * © <your company here>, 2003-2007
 * Confidential and proprietary.
 */

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import javax.microedition.io.*;
import java.util.*; 
import java.io.*;

class 
RIMJUDPClient extends UiApplication 
{
    static 
PersistentObject store;
    static 
    {
        
store PersistentStore.getPersistentObject0xb309d389c6a2436dL );
    }
    
    public 
RIMJUDPClient() 
    {
        
pushScreen(new RimUDPClient());
    }
    
    public static 
void main(String argsxxx91;xxx93;)
    {
        
RIMJUDPClient rimJApp = new RIMJUDPClient();
        
rimJApp.enterEventDispatcher();
    }
    
    public class 
RimUDPClient extends MainScreen 
    
{
        private 
LabelField title;
        private 
EditField editProtocol
        private 
EditField editMessage;
        private 
RichTextField editMsg;
        public 
String sProtocol;
        public 
String sMessage;
        public 
String sReceivedMessage;
        private 
sendThread sTh;
        private 
String mm ="Test Message";
        public 
Vector sVector;
        
        public 
RimUDPClient() 
        {
            
super();
            
sVector = new Vector(10);
            
title = new LabelField("UDP CLient",LabelField.USE_ALL_WIDTH);
            
setTitle(title);
            
// udp://10.1.0.122:5029;19780/rim.net.gprs
            // udp://127.0.0.1:5029;19780/rim.net.gprs : Use This String for Local Testing
            // udp://&lt;Public IP>:&lt;UDP Port>;19780/proxy : Use This String With AT&amp;T GPRS Service
            // udp://&lt;Public IP>:&lt;UDP Port>;19780/internet3.voicestream.com : Use This String With T-Mobile GPRS Service
            
editProtocol = new EditField("Protocol :","udp://10.1.0.122:5029;19780/rim.net.gprs");
            
editMessage = new EditField("Message : ",mm);
            
editMsg = new RichTextField("Message");
            
add(editProtocol);
            
add(editMessage);
            
add(editMsg);
        }
        
        public 
void storeMessage(String sMsg
        {
            try 
            {
                
sVector.addElement(sMsg);
                
System.out.println("Vector Size :" sVector.size());
                
synchronized(store
                {
                    
store.setContents(sVector);
                    
store.commit(); 
                }
            }
            catch (
Exception ex
            {
                
System.err.println("Error while Storing" ex);
            }
        }
        
        private 
MenuItem _sendItem = new MenuItem("Send Package",110,10
        {
            public 
void run()
            {
                
sProtocol editProtocol.getText().trim();
                
sTh = new sendThread();
                
sTh.start();
            }
        };
        
        private 
MenuItem _onCloseItem = new MenuItem("Close",200000,10
        {
            public 
void run() 
            {
                
onCLose();
            }
        };
        
        protected 
void makeMenu(Menu menu,int instance)
        {
            
menu.add(_sendItem);
            
menu.add(_onCloseItem);
        }
        
        public 
boolean onCLose()
        {
            
Dialog.alert("Good Bye");
            
Dialog.alert("Msg: " sReceivedMessage);
            
System.exit(0);
            return 
true;
        }
        
        protected 
void disPlayMessage(final String str)
        {
            
Application.getApplication().invokeLater(new Runnable() 
            {
                public 
void run() 
                {
                    
Dialog.alert("Msg Recvd. : " str.trim());
                    
editMsg.setText(str.trim());
                }
            });
        }
        
        private class 
sendThread extends Thread 
        
{
            
DatagramConnection conn null
            
            public 
void run() 
            {
                try 
                {
                    
String msg editMessage.getText();
                    
int length msg.length();
                    
bytexxx91;xxx93; message = new bytexxx91;lengthxxx93;;
                    
System.arraycopy(msg.getBytes(), 0message0length); 
                    
conn = (DatagramConnection)Connector.open((editProtocol.getText()).trim());
                    
Datagram outDatagram conn.newDatagram(messagemessage.length);
                    
conn.send(outDatagram);
                    
int i0;
                    
                    while(
true
                    {
                        
Datagram inDatagram conn.newDatagram(conn.getMaximumLength()); 
                        
conn.receive(inDatagram); 
                        
bytexxx91;xxx93; data inDatagram.getData();
                        
String retval = new String(data0inDatagram.getLength()); 
                        
disPlayMessage(retval);
                        
storeMessage(retval);
                        
System.out.println(inDatagram.getAddress() + " at port says " +" : " sReceivedMessage );
                        
i++;
                    }
                }
                catch (
Exception e
                {
                    
disPlayMessage("Error:" e.toString());
                    return;
                }
                finally 
                {
                    
// if (conn != null) conn.close();
                
}
            } 
// Send Thread Run Method End Here
            
        
// Send Thread Class Ends Here
        
    
}
// Main Class RIMJavaUDP Client Ends Here 
this code was written by Dev2100.
when i run the program in the simulator, it no return nothing. is an error of the code?

another thing...
i want to send by UDP a location, i have de program tha search the location, how can implement this code in my program???

thanks a lot ;)
Offline  
Old 06-09-2008, 02:40 AM   #2
kabrator
New Member
 
Join Date: Jun 2008
Location: Valencia, Spain
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 2
Default

can someone helpme, please???
Offline  
Old 02-08-2009, 04:37 AM   #3
dohboy
New Member
 
Join Date: Mar 2007
Location: Denver, CO
Model: 8330
Carrier: TMo
Posts: 2
Default Re: UDP

UDP is stateless, meaning, you will not see a response to a message sent. You should be able to set up a simple UDP server locally to test your UDP messaging. Using the device simulator, load the jar and set the protocol to udp://localhost:1234, then send your package.

Code:
//UDPServer.java

import java.net.*;
import java.io.*;


class UDPServer
{
    public static void main(String args[]) throws Exception
    {
        byte[] receive_data = new byte[1024];
        byte[] send_data = new byte[1024];

        int recv_port;
        DatagramSocket server_socket = new DatagramSocket(1234);

        while(true)
        {
            DatagramPacket receive_packet = new DatagramPacket(receive_data,
                                             receive_data.length);
            server_socket.receive(receive_packet);
            String data = new String(receive_packet.getData(),0                                          ,0
                                           ,receive_packet.getLength());

            //InetAddress IPAddress = receive_packet.getAddress();
            //recv_port = receive_packet.getPort();

            if (data.equals("q") || data.equals("Q"))
                break;
            else
                System.out.print(data);
         }
    }
}
It's been over 6 months, but I hope this helps.
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


SA-3786-12 603350 Fuel Shutoff Solenoid Kubota Compatible 721D2 21HP Grasshopper picture

SA-3786-12 603350 Fuel Shutoff Solenoid Kubota Compatible 721D2 21HP Grasshopper

$30.29



Starter Solenoid - - 12 Volt - 3 Terminal fits Ford fits New Holland picture

Starter Solenoid - - 12 Volt - 3 Terminal fits Ford fits New Holland

$44.99



Cole Hersee (24059-BP) 12V Insulated SPST Continuous Duty Solenoid picture

Cole Hersee (24059-BP) 12V Insulated SPST Continuous Duty Solenoid

$29.85



1503ES-12A5UC5S SA-4569-T Fuel Shutoff Solenoid Compatible With Kubota picture

1503ES-12A5UC5S SA-4569-T Fuel Shutoff Solenoid Compatible With Kubota

$19.99



Trombetta 684-1261-212-08 12 Volt Solenoid PowerSeal DC Contactor M9 picture

Trombetta 684-1261-212-08 12 Volt Solenoid PowerSeal DC Contactor M9

$20.39



S.36121 Starter Solenoid Fits John Deere picture

S.36121 Starter Solenoid Fits John Deere

$38.69







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.