Implementation of simple socket chat by Using J2EE

Source: Internet
Author: User

This post is intended to be used by java fans j2se j2j2j2ee.

Ugly here



Server
Package demo;

Import java. io. DataInputStream;
Import java. io. DataOutputStream;

Import javax. microedition. io. Connector;
Import javax. microedition. io. ServerSocketConnection;
Import javax. microedition. io. SocketConnection;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. CommandListener;
Import javax. microedition. lcdui. Display;
Import javax. microedition. lcdui. Displayable;
Import javax. microedition. lcdui. Form;
Import javax. microedition. lcdui. TextField;
Import javax. microedition. midlet. MIDlet;
Import javax. microedition. midlet. MIDletStateChangeException;


Public class server extends MIDlet implements CommandListener {
Private Display display;
Private Form form = new Form ("server chat ");
Private Command cmd = new Command ("send", Command. SCREEN, 1 );
Private TextField tf_msg = new TextField ("Enter:", "", 50, TextField. ANY );

Private ServerSocketConnection ssc = null;
Private SocketConnection SC = null;
Private DataInputStream dis = null;
Private DataOutputStream dos = null;
Protected void destroyApp (boolean arg0) throws MIDletStateChangeException {

}

Protected void pauseApp (){

}

Protected void startApp () throws MIDletStateChangeException {
Display = Display. getDisplay (this );
Display. setCurrent (form );
Form. addCommand (cmd );
Form. append (tf_msg );
Form. setCommandListener (this );

Try {
Ssc = (ServerSocketConnection) Connector. open ("socket: //: 8888 ");
SC = (SocketConnection) ssc. acceptAndOpen ();
Dos = SC. openDataOutputStream ();
Dis = SC. openDataInputStream ();
New ChatThread (). start ();
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}

Public void commandAction (Command c, Displayable d ){
If (c = cmd ){
Try {
String str = tf_msg.getString ();
Dos. writeUTF (str );
Form. append ("server say:" + str + "");
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
}

Class ChatThread extends Thread {
Public void run (){
While (true ){
Try {

String str = dis. readUTF ();
Form. append ("client say:" + str + "");
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
}
}

}



Client

Package demo;

Import java. io. DataInputStream;
Import java. io. DataOutputStream;

Import javax. microedition. io. Connector;
Import javax. microedition. io. SocketConnection;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. CommandListener;
Import javax. microedition. lcdui. Display;
Import javax. microedition. lcdui. Displayable;
Import javax. microedition. lcdui. Form;
Import javax. microedition. lcdui. TextField;
Import javax. microedition. midlet. MIDlet;
Import javax. microedition. midlet. MIDletStateChangeException;

Public class client extends MIDlet implements CommandListener {
Private Display display;
Private Form form = new Form ("server chat ");
Private Command cmd = new Command ("send", Command. SCREEN, 1 );
Private TextField tf_msg = new TextField ("Enter:", "", 50, TextField. ANY );

Private SocketConnection SC = null;
Private DataInputStream dis = null;
Private DataOutputStream dos = null;

Protected void destroyApp (boolean arg0) throws MIDletStateChangeException {

}

Protected void pauseApp (){

}

Protected void startApp () throws MIDletStateChangeException {
Display = Display. getDisplay (this );
Display. setCurrent (form );
Form. addCommand (cmd );
Form. append (tf_msg );
Form. setCommandListener (this );

Try {

SC = (SocketConnection) Connector. open ("socket: // FIG: 8888 ");
Dos = SC. openDataOutputStream ();
Dis = SC. openDataInputStream ();
New ChatThread (). start ();
& Nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.