Java
Socket connection in CLDC
I have write a sample program to read
the content of a URL via SocketConnection. I got success to make a socket connection with server. which I had conform to seem in n/w monitor which i enabled previously.
But the problem is that nothing should be send by server.I check the available data in InputStream , which I got from connection is zero.
I write my code below:
package connection;
import display.Show;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.SocketConnection;
import midlet.GCFMIDlet;
public class Socket{
String url="socket://www.globallogic.com:443";
//String url="http://www.lambenttek.com:80";
SocketConnection scon=null;
HttpConnection hcon=null;
InputStream is=null;
OutputStream os=null;
DataInputStream din=null;
DataOutputStream dout=null;
StringBuffer buf= new StringBuffer();
StringBuffer header = new StringBuffer();
public Socket(GCFMIDlet midlet){
try{
//make a connection to server
SocketConnection scon = (SocketConnection) Connector.open(url);
//System.out.println("Connection : "+scon);
// set application-specific options on the socket. Call setSocketOption to set other options
scon.setSocketOption(SocketConnection.DELAY, 1);
scon.setSocketOption(SocketConnection.KEEPALI VE,1);
scon.setSocketOption(SocketConnection.LINGER, 1);
scon.setSocketOption(SocketConnection.RCVBUF, 8000);
scon.setSocketOption(SocketConnection.SNDBUF, 8000);
// send something to server
os = scon.openOutputStream();
dout = new DataOutputStream(os);
//os.write("GET /indus_diff/index.shtml HTTP/1.1 \r\n".getBytes());
dout.writeChars("GET /indus_diff/index.shtml HTTP/1.1\n");
// dout.writeChars("User-Agent : Profile/MIDP-2.0 Configuration/CLDC-1.0\n");
// dout.writeChars("Content-Language : en-US\r\n");
// dout.writeChars("content-encoding : UTF-8\n");
// dout.writeChars("\r\n");
dout.flush();
is = scon.openInputStream();
System.out.println("Input Stream Size : "+is.available());
// read server response
int c = 0;
while((c = is.read()) != -1) {
//storing server responce into buffer
buf.append((char)c);
}
//check the length of the buffer
System.out.println("Lenght of Buffer : "+buf.length());
//display buffer data on to a form
if(buf != null)
new Show(midlet,buf.toString()); // display on form
}catch(Exception e){
System.out.println(e);
}finally{
try{
// System.out.println("finally start");
is.close(); // close streams and scon
// System.out.println("A");
dout.close();
// System.out.println("AB");
os.close();
// System.out.println("ABC");
if(scon != null)
scon.close();
else
scon=null;
}catch(Exception e){
System.out.println("finally : "+e);
}
}
}
}
If u have any Idea then please send me, don't keep with u!
Thanks!
Amit Yadav
Replies
No replies to this message


