Generic Connection Framework (GCF),Network Example by R4R Team

Generic Connection Framework (GCF),Network Example:

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import java.util.*;

import java.io.*;

public class NetEg extends MIDlet {

private static Command httpTestCommand = new Command("HttpConn", Command.SCREEN, 1);

private static Command datagramSendCommand = new Command("Send Datagram", Command.SCREEN, 1);

private static Command datagramReceiveCommand = new Command("Receive Datagram", Command.SCREEN, 1);

private static Command socketClientCommand = new Command("Socket Client", Command.SCREEN, 1);

private static Command socketServerCommand = new Command("Socket Server", Command.SCREEN, 1);

private Command commandType;

private List list = new List("Http Test", List.IMPLICIT);

/**

Used to create instances of the MIDlet

*/

public NetEg()

{

CommandListener listener = initListeners();

Display.getDisplay(this).setCurrent(list);

list.addCommand(httpTestCommand);

list.addCommand(datagramSendCommand);

list.addCommand(datagramReceiveCommand);

list.addCommand(socketClientCommand);

list.addCommand(socketServerCommand);

list.setCommandListener(listener);

}

/**

Used to create instances of Thread to run the actual command

*/

public NetEg(Command cmd)

{

commandType = cmd;

}

public void startApp()

{

}

public void pauseApp()

{

}

public void destroyApp(boolean unc)

{

}

private void appendData(String data)

{

list.append(data, null);

}

/**

Create a listener command handler

*/

private CommandListener initListeners()

{

return new CommandListener()

{

public void commandAction(Command command, Displayable displayable)

{

Thread t = new Thread(new CommandHandler(command));

t.start();

}

>};

}

class CommandHandler implements Runnable

{

private Command commandType;

public CommandHandler(Command type)

{

commandType = type;

}

 

private void httpTest()

{

HttpConnection c = null;

InputStream is = null;

StringBuffer sb = new StringBuffer();

try

{

c = (HttpConnection)Connector.open("<nowiki>http://localhost</r4r>", Connector.READ_WRITE, true);

c.setRequestMethod(HttpConnection.GET); //default

is = c.openInputStream(); // transition to connected!

int ch = 0;

for(int ccnt=0; ccnt &lt; 150; ccnt++) //get the title.

{

ch = is.read();

System.out.print((char)ch);

if (ch == -1)

{<br>break;<br>}<br>sb.append((char)ch);

}<br>}<br>catch (IOException x)

{<br>x.printStackTrace();

}

finally

{

try

{

is.close();

c.close();

} catch (IOException x)

{

x.printStackTrace();

}

}

appendData(sb.toString());

}

private void sendDatagram()

{

try

{

DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://localhost:9001");

try

{

byte[] payload = "Hello from a Datagram".getBytes();<br>Datagram datagram = dgc.newDatagram(payload, payload.length);

dgc.send(datagram);

} finally

{

dgc.close();

}

} catch (IOException x)

{

x.printStackTrace();

}

}

private void receiveDatagram()

{

try

{

DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://:9001");

try

{

int size = 100;

Datagram datagram = dgc.newDatagram(size);

dgc.receive(datagram);

appendData(new String(datagram.getData()).trim());

} finally

{

dgc.close();

}

} catch (IOException x)

{

x.printStackTrace();

}

}

private void clientSocket()

{

try

{

SocketConnection sc = (SocketConnection) Connector.open("socket://localhost:9002");<br>OutputStream os = null;<br>try<br>{<br>os = sc.openOutputStream();

byte[] data = "Hello from a socket!".getBytes();

os.write(data);

} finally

{

sc.close();

os.close();

}

} catch (IOException x)

{

x.printStackTrace();

}

}

private void serverSocket()

{

try

{

ServerSocketConnection ssc = (ServerSocketConnection) Connector.open("socket://:9002");

StreamConnection sc = null;

InputStream is = null;

try

{

sc = ssc.acceptAndOpen();

is = sc.openInputStream();

System.out.println("connection opened!");

int ch = 0;

StringBuffer sb = new StringBuffer();

while ((ch = is.read()) != -1)

{

sb.append((char)ch);

}

appendData(sb.toString());

} finally

{ssc.close();

sc.close();

is.close();

}

} catch (IOException x)

{

x.printStackTrace();

}

}

public void run()

{

System.out.println("Running Command");

if (commandType == httpTestCommand)

{

httpTest();

} else if (commandType == datagramSendCommand)

{<br>sendDatagram();

} else if (commandType == datagramReceiveCommand)

{

receiveDatagram();

} else if (commandType == socketClientCommand)

{

clientSocket();

} else if (commandType == socketServerCommand)

{

serverSocket();

}

else

{

throw new IllegalStateException("No command specified to run.");

}}}}

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!