views:

20

answers:

2

Hello ,

How can i connect to a TcpServer(using ip & port) with J2ME ? i want to send a request to the server and get responses !

+2  A: 

Check this document it will certainly answer your question

org.life.java
Hi , Thank you for response . but i have a problem . when i'm using "Connector.open()" i get some errors . whereas , all of important libraries have imported . let me a easy sample please
Kermia
@Kermia can you post your error stark trace
org.life.java
@Kermia If this the answer to your Question than you can mark this as answer.
org.life.java
A: 

My Problem solved using this code :

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

       StreamConnection connection = (StreamConnection) 
         Connector.open("socket://IP:Port");

       PrintStream output = 
         new PrintStream(connection.openOutputStream());

       output.println("Request");
       output.flush();

       InputStream in = connection.openInputStream();
       int ch;
       String recx;

       while( ( ch = in.read() ) != 13 )
       {
         recx = recx + (char)ch;  
       }


       in.close();
       output.close();
       connection.close();
Kermia