We're giving away 10 HTC One (M8) harman/kardon edition phones for Sprint! Enter now!
Phone Scoop

printed October 1, 2014
See this page online at:

Home  ›  Forums  ›


Back to message list

Socket connection in CLDC

by amity    May 17, 2007, 4:08 AM


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){


//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");

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

//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("finally start");
is.close(); // close streams and scon
// System.out.println("A");

// System.out.println("AB");

// System.out.println("ABC");

if(scon != null)

}catch(Exception e){
System.out.println("finally : "+e);



If u have any Idea then please send me, don't keep with u!


Amit Yadav

Report to moderator


No replies to this message

Reply You must log in to reply.

Back to message list

Subscribe to Phone Scoop News with RSS Follow @phonescoop on Twitter Phone Scoop on Facebook Phone Scoop on Google+ Subscribe to Phone Scoop on YouTube Follow on Instagram


All content Copyright 2001-2014 Phone Factor, LLC. All Rights Reserved.
Content on this site may not be copied or republished without formal permission.