Phone Scoop

printed September 17, 2014
See this page online at:
http://www.phonescoop.com/forums/forum.php?fm=m&ff=10&fi=1167552

Home  ›  Forums  ›

Java

Back to message list

Socket connection in CLDC

by amity    May 17, 2007, 4:08 AM

Hello,

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

Report to moderator

Replies

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.
2