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


NEW Mitsubishi A1SNMCA-8KE Memory Cassette picture

NEW Mitsubishi A1SNMCA-8KE Memory Cassette

$151.62



NEW Original Allen Bradley 2080-MEMBAK-RTC Memory Module With RTC Plug-In picture

NEW Original Allen Bradley 2080-MEMBAK-RTC Memory Module With RTC Plug-In

$284.00



NEW Original 2080-MEMBAK-RTC2 AB Micro800 Memory Module 4MB RTC Plug-In picture

NEW Original 2080-MEMBAK-RTC2 AB Micro800 Memory Module 4MB RTC Plug-In

$292.99



1PC New ABB ACS880-MU-ZCU-12/14 ZMU-02 Inverter Memory Card 1year waranty picture

1PC New ABB ACS880-MU-ZCU-12/14 ZMU-02 Inverter Memory Card 1year waranty

$79.39



Oktis - 2 Portable Fuel Analyzer Tester Meter Octane Number Gasoline Petrol picture

Oktis - 2 Portable Fuel Analyzer Tester Meter Octane Number Gasoline Petrol

$179.99



NEW Original Allen Bradley 2080-MEMBAK-RTC Memory Module With RTC Plug-In picture

NEW Original Allen Bradley 2080-MEMBAK-RTC Memory Module With RTC Plug-In

$288.89







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