Page 1 of 1

J2ME SocketConnection Help

Posted: Tue Aug 17, 2010 4:41 pm
by Herath
I have been trying to connect to MSN Authentication server as the feasibility study phase of building my own MSN client for Java Mobiles. The Initial command for the server is like "VER 0 MSNP8 MSNP7 MNSP6 CVR0". This is to negotiate the MSN protocol used for the communication. And the Java Implementation simply does not work for me. I have been trying whole say. :(

Code: Select all

     try{
               SocketConnection con=
                    (SocketConnection) Connector.open("socket://messenger.hotmail.com:1863");
                

                //Output and input streams opened....

                OutputStream output =con.openOutputStream();
                output.flush();
                output.write("VER 0 MSNP8 CVR0".getBytes());
                output.flush();
                InputStream in =con.openInputStream();

                int inputChar=in.read();
                while ( inputChar!=-1) {
                        sbf.append((char)inputChar);
                        inputChar=in.read();
                }
}
            catch(ConnectionNotFoundException error)
               {
                    Alert alert = new Alert(
                    "Error", "Cannot access socket.", null, null);
                        alert.setTimeout(Alert.FOREVER);
                    alert.setType(AlertType.ERROR);
                    display.setCurrent(alert);
                }
            catch(Exception ex){}
And following is the C# implementation. It works till it tells me the Notification Server IPs.

Code: Select all

TcpClient client = new TcpClient();

            try 
	            {
                    client.Connect("messenger.hotmail.com", 1863);
                    if (client.Connected)
                    {
                        textBox1.AppendText("Client is connected\n");
                        NetworkStream networkStream = client.GetStream();
                        StreamWriter streamWriter = new StreamWriter(networkStream);
                        StreamReader streamReader = new StreamReader(networkStream);
                        streamWriter.WriteLine("VER 0 MSNP8 MSNP5 CVR0");
                        streamWriter.Flush();
                        textBox1.AppendText(streamReader.ReadLine()+"\n");


                        streamWriter.WriteLine("CVR 1 0x0409 win 5.1 i386 MSNMSGR 2.1.6000 MSMSGS herat**@hotmail.com");
                        streamWriter.Flush();
                        textBox1.AppendText(streamReader.ReadLine() + "\n");

                        streamWriter.WriteLine("USR 2 TWN I herat**@hotmail.com");
                        streamWriter.Flush();
                        textBox1.AppendText(streamReader.ReadLine() + "\n");
                    }
                    else
                    {
                        textBox1.AppendText("Client is not connected\n");
                    }
	            }
	            catch (Exception)
	            {
		
		            throw;
	            }
So, I like to get some help from a Guru...

Re: J2ME SocketConnection Help

Posted: Tue Aug 17, 2010 7:40 pm
by Neo
Didn't do this myself. However I found this link which might help you.
http://sourceforge.net/apps/trac/java-jml

Re: J2ME SocketConnection Help

Posted: Tue Aug 17, 2010 7:53 pm
by Herath
Thanks for the link. :)
I tried that link before. It is written in J2SE, and too complicated without in-code comments. I found another J2SE MSN library which seems to be pretty straightforward and am trying to extract something from it. The thing is, they have used the same method I am using(In a little bit confusing way), and seems to do the job.

I will try with the project configuration too. I know that Socket connection needs the phone to support MIDP 2.0 and I am using that profile. Phone asks if I want to let the application to transfer data.But I never receive a single byte from the server except the 19 bytes I am sending.

I will try some more. I know there are lot of free chat clients. But I am doing this just to explore the technology and of course to build a one without Advertisements!. :D

Re: J2ME SocketConnection Help

Posted: Wed Aug 18, 2010 2:35 am
by Neo
Your idea is excellent.

The best thing you could do is try to understand and extract the protocol from a working code. Otherwise you'll mostly have to do lots of experiments (which will take a long period of time) due to lack of documentation, new protocol updates, etc...

After making it, if you could put up a nice 'how to' tutorial, it would be really great for all the members (including me). Learning has no end.

Good luck!

Re: J2ME SocketConnection Help

Posted: Thu Aug 19, 2010 9:25 am
by Herath
I figured out what's happening in the server connection.
I get WinSock Error 10054 while reading the InputStream from the server while i have the output stream opened. Although it hit my mind, i did not try it. I thought that the full duplex communication is the standard for networks these days. :D

This code worked. I got the expected reply from the server, other than the "hard close" on me. ;)

Code: Select all

        new Thread(){
            public void run(){
                try{
                SocketConnection server=(SocketConnection)Connector.open("socket://messenger.hotmail.com:1863");
                //server.setSocketOption(SocketConnection.KEEPALIVE,0);
               // server.setSocketOption(SocketConnection.LINGER,5);
                
                

                OutputStream os=server.openOutputStream();
                

                os.write("VER 0 MSNP8 MSNP9 CVR0\r\n".getBytes());//Write bytes to the stream
                os.flush();//Flush the buffer
                os.close();//close output stream

                InputStream is=server.openInputStream();//Open inputStream
             
                //Start reading from it.
                int inchar;
                StringBuffer sbf=new StringBuffer();
                while((inchar=is.read())!=-1){
                    sbf.append((char)inchar);
                }
               
                //Display the data read from the inputStream.
                Alert o=new Alert("MSG");
                o.setString(sbf.toString());
                o.setTimeout(Alert.FOREVER);
                display.setCurrent(o);
                }catch(Exception ex){

                    tf.setString(ex.getMessage().toString());
                }

                
            }
        }.start();


Information on the MSN Protocol can be found at http://www.hypothetic.org/docs/msn/. You can use wireshark too. :)

Now I am trying to build a simple library of my own for MSN. :mrgreen:

Re: J2ME SocketConnection Help

Posted: Fri Sep 24, 2010 2:39 pm
by ni30rocks
On the same points I am trying to connect to GMail server but its response is empty... But I have not included the Initial command for the server "VER 0 MSNP8 MSNP7 MNSP6 CVR0", as I thought it is basically for MSN server.

Code: Select all

socket = (SocketConnection)Connector.open("socket://imap.gmail.com:993");
            DataOutputStream oStrm = socket.openDataOutputStream();
            oStrm.flush();
            oStrm.writeChars("Account Name:[email protected]");
            oStrm.flush();
            oStrm.writeChars("Email Address:[email protected]");
            oStrm.flush();
            //oStrm.writeChars("Password:");
            // Create the connection
            //OutputStream
           
           InputStream iStrm = socket.openInputStream();
           int inchar;
           StringBuffer sbf=new StringBuffer();
           while((inchar=iStrm.read())!=-1)
           {
                sbf.append((char)inchar);
           }
           System.out.println("String:"+sbf); // This is providing me NULL pointer exception  :idea: 
                //Display the data read from the inputStream.
                Alert o=new Alert("MSG");
                o.setString(sbf.toString());
                o.setTimeout(Alert.FOREVER);
                display.setCurrent(o);
Help me in coming out of issue

Re: J2ME SocketConnection Help

Posted: Fri Sep 24, 2010 3:24 pm
by Herath
"VER 0 MSNP8 MSNP7 MNSP6 CVR0" is used to negotiate the protocol to use by the client and the server. You do not need it in gmail.

Seems like you are trying to implement an imap client. I do not know anything about IMAP protocol.

But I had to write Unicode encoded bytes in to the server in order to get the correct response. Otherwise I did not get any response from the server. You can check "MGTalk" source for that. It is a free j2me chat client for google talk.

Debug your code and see if you get the connection opened to google imap server. The socket might not be opened. :?:

Re: J2ME SocketConnection Help

Posted: Fri Sep 24, 2010 5:33 pm
by Neo