How to send Multiple Sockets?
Hi, I'm working on a Palm application in a Palm Emulator connected to a SocketServer. I'm accessing SQL Server DB to retrieve items to put on my combobox. Now that I retrieved the items, I need to send them to the Palm application. Previously, I concat the items into one string, send them to the Output Stream using the write() method. Then I realised that J2ME CLDC does not support StringTokenizer which I used to process any string from the palm to the server.
So now I've stored the combobox items into a Vector and I want to send the elements one at a time through many sockets. So my question is how does a Server send multiple sockets?
Please keep in mind that I am using J2ME CLDC and all J2ME socket examples I've seen only shows one socket and only one file being sent and received.
Please reply, thanks.
zad.
Hi
I do not know much about J2ME CLDC, but does it support SocketServer ? if so then start a few of these, and send information about them to the SocketServer on the server. The process on the server can then make connections and send the data across the multiple sockets. Question - why do you which to have multiple sockets ? does the J2ME CLDC not support serialization ?
Hi, thanks for replying. Sure, J2ME CLDC can connect to a socketserver, but only if the application is running in a Palm OS Emulator. No, J2ME CLDC does not support serialization. Since it is for software development of small wireless devices, its api have been scaled down. So, much of the methods that I want to use are not available.
Also, the items need to be inserted into a vector-like object at the palm application before loading them into a combobox.
OK, implement your own StringTokenizer class, by breaking the String into chars, and then looping through them finding the tokens. I would have thought this should be possible, because you only need the String class, and a for loop.
Your own string tokenizer would work. You could alos build the string as a byte array figure out the max length of any one string and then build a byte array
byte b[] = new byte[200];
b[0].. b[20] == String 1
b[21] .. b[40] == String 2
The advantage to this approach is you can directly index and build the strings, and you would know how many stirng there are by the size of the array. The disadvantage would be size if your sending a lot of string, you are gona use more memory than you really need, till you get them into strings and trim them down to remove the extra space.
ghaga at 2007-6-29 10:26:24 >

Ok I'm not too good at this, so pls correct this code. Thanks.
public String Tokenizer(String stringFromServer)
String str;
StringBuffer tokens = new StringBuffer();
char[] cString;
int leng = stringFromServer.length();
cString = stringFromServer.toCharArray();
for(int i = 0;i<leng;i++){
tokens.append(cString[i]);
if(cString[i].equals("\n")){ //"\n" being my delimiter
str = tokens.toString();
return str;
}
}
}
>
It looked OK what you wrote but there was no means to get the next token as far as i could see. Below is a simple example, but remember there is not much error logic like if you have an empty token etc.
public class MyStringTokenizer
{
char[] mArray;
char mSep;
int mIndex;
public MyStringTokenizer(String stringFromServer, char sep)
{
mArray = stringFromServer.toCharArray();
mSep = sep;
mIndex = 0;
}
/**
* @return The next string or null if the end has been reached.
*/
public String next()
{
String str;
StringBuffer tokens = new StringBuffer();
for(;mIndex<mArray.length;mIndex++)
{
if(mArray[mIndex] == mSep)
{
mIndex++;
break;
}
tokens.append(mArray[mIndex]);
}
if (tokens.length() == 0)
{
return null;
}
else
{
return tokens.toString();
}
}
public static void main(String args[])
{
MyStringTokenizer a = new MyStringTokenizer("this\nis\na\ntest",'\n');
String next;
while ((next = a.next()) != null)
{
System.out.println("Next:" + next);
}
MyStringTokenizer b = new MyStringTokenizer("this\nis\na\ntest",'\n');
while ((next = b.next()) != null)
{
System.out.println("Next:" + next);
}
}
}
>
Thanks! It worked! zad :)