Disadvantages of xmlsocket

Source: Internet
Author: User

// ================================================ =;

//Flash client (taking flash text chat as an example );

// ================================================ =;

VaR paramobj: Object = new object ();

// Command Separator;

Paramobj. commanddelimiters = "-@@##@@-";

// User list separator;

Paramobj. peopledelimiters = "-@#@-";

// Create an xmlsocket object;

VaR socket: xmlsocket = new xmlsocket ();

// Connection status event;

Socket. onconnect = function (SUCCESS ){

Trace ("socket. onconnect:" + success );

If (! Success ){

Trace ("the server connection failed. Check the network status! ");

}

};

// Close the event;

Socket. onclose = function (){

Trace ("the server is closed! ");

Logoutchat ();

};

//Data communication events;

Socket. ondata = function (SRC ){

// Trace ("socket. ondata:" + SRC );

Docommand (getcmdarraybymsg (TRIM (SRC )));

};

//User Logon;

Function loginchat (): void {

// Connect to the socket server;

Socket. Connect ("localhost", "8888 ");

Sendsocket ("info" + paramobj. commanddelimiters + MSG );

}

//User logout;

Function logoutchat (B: Boolean): void {

Sendsocket ("quit ");

}

//Show chat Information;

Function showchat (MSG: string): void {

Trace ("chat information:" + MSG );

}

//Send chat Information;

Function sendchat (MSG: string): void {

Sendsocket ("MSG" + paramobj. commanddelimiters + MSG + paramobj. commanddelimiters + MSG );

}

//Send messages to the server;

Function sendsocket (MSG: string): void {

Socket. Send (MSG + "\ r ");

}

//Process the information returned by the server;

Function getcmdarraybymsg (MSG: string): Array {

If (msg. charcodeat (0) = 13 & msg. charcodeat (1) = 10 ){

MSG = msg. substr (2 );

}

Return MSG. Split (paramobj. commanddelimiters );

}

Function docommand (ARR: array): void {

Switch (ARR [0]) {

Case "MSG ":

Showchat (ARR [1]);

Break;

Case "taken ":

Trace ("your login name already exists. Please change to another login name! ");

Break;

Case "people ":

Dopeople (ARR [1]);

Break;

}

}

//Show online user list;

Function dopeople (MSG: string): void {

VaR people_arr: array = MSG. Split (paramobj. leledelimiters );

Trace (lele_arr );

}

// The main Code related to xmlsocket is displayed. Just add the relevant components on your own!

// Note that "\ r" must be added at the end of the Command sent by flash to the server; otherwise, the server cannot receive the message (my server is developed in Java)

// ================================================ =;

//Server code(I useJavaDeveloped by others);

// Chatserver. Java

// ================================================ =;

Package com. klstudio. Socket. Chat;

Import java. Io. ioexception;

Import java.net. serversocket;

Import java.net. Socket;

Import java. util. vector;

// Import com. klstudio. util. Logger;

/**

* @ Author kinglong

*

* To change the template of the type annotation generated by todo, go to window-preferences-Java-code style-code template.

*/

Public class chatserver {

// Private logger;

Private Static vector clients = new vector ();

Private Static serversocket Server = NULL;

Private Static Socket socket = NULL;

Public static string commanddelimiters = "-@@##@@-";

Public static string peopledelimiters = "-@#@-";

Public chatserver (){

}

Public static void policyroom (){

Stringbuffer people = new stringbuffer ("people" + commanddelimiters + "All people ");

For (INT I = 0; I <clients. Size (); I ++ ){

Client client = (client) clients. elementat (I );

People. append (leledelimiters + client. getclientname ());

}

Sendclients (people );

}

Public staticboolean checkname (client newclient ){

For (INT I = 0; I <clients. Size (); I ++ ){

Client client = (client) clients. elementat (I );

If (client! = Newclient & client. getclientname (). Equals (newclient. getclientname ())){

Return false;

}

}

Return true;

}

Public static void closeall (){

While (clients. Size ()> 0 ){

Client client = (client) clients. firstelement ();

Try {

Client. getclientsocket (). Close ();

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// Logger = new logger (system. Out );

// Logger. Log ("error-" + E. tostring ());

} Finally {

Clients. removeelement (client );

}

}

}

Public static synchronized void disconnect (Client client ){

Client. Send (New stringbuffer ("quit "));

Try {

Client. getclientsocket (). Close ();

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// Logger = new logger (system. Out );

// Logger. Log ("error-" + E. tostring ());

} Finally {

Clients. removeelement (client );

}

}

Public static synchronized void sendclients (stringbuffer SB ){

For (INT I = 0; I <clients. Size (); I ++ ){

Client client = (client) clients. elementat (I );

Client. Send (SB );

}

}

Public static synchronized void sendclients (stringbuffer Sb, string ownername, string toname ){

For (INT I = 0; I <clients. Size (); I ++ ){

Client client = (client) clients. elementat (I );

If (toname. Equals (client. getclientname () | toname. Equals ("All persons") | ownername. Equals (client. getclientname ())){

Client. Send (SB );

}

}

}

Public static synchronized void sendclients (client ownerclient ){

For (INT I = 0; I <clients. Size (); I ++ ){

Client client = (client) clients. elementat (I );

If (client. getclientname (). Equals (ownerclient. getclientname ())){

Client. Send (New stringbuffer ("MSG" + commanddelimiters + "system information> welcome! "));

} Else {

Client. Send (New stringbuffer ("MSG" + commanddelimiters + "system information> [" + ownerclient. getclientname () + "] the user enters! "));

}

}

}

Public static void main (string [] ARGs ){

Int Port = 8888;

If (ARGs. length> 0 ){

Port = integer. parseint (ARGs [0]);

}

// Logger = new logger (system. Out );

// Logger. Log ("info-chatserver [" + port + "] the service is starting ...");

Try {

Server = new serversocket (port );

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// Logger. Log ("error-" + E. tostring ());

}

While (true ){

If (clients. Size () <5 ){

Try {

Socket = server. Accept ();

If (socket! = NULL ){

// Logger. Log ("information-" + socket. tostring () + "connection ");

}

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// Logger. Log ("error-" + E. tostring ());

}

Int I = 0;

Do {

Client client = new client (socket );

If (client. getclientname ()! = NULL ){

Clients. addelement (client );

If (checkname (client )){

// Logger. Log ("info-" + "currently [" + clients. Size () + "] connected users ");

Sendclients (client );

Client. Start ();

Policyroom ();

} Else {

Client. Send (New stringbuffer ("taken "));

Disconnect (client );

}

I ++;

}

Break;

} While (I <clients. Size ());

} Else {

Try {

Thread. Sleep (200 );

} Catch (interruptedexception e ){

// Todo automatically generates catch Blocks

// Logger. Log ("error-" + E. tostring ());

}

}

}

}

}

 

// ================================================ =;

//Client. Java

// ================================================ =;

/*

* Creation date: 2005-10-10

*

* To change the template of the generated file in todo, go

* Window-preferences-Java-code style-Code Template

*/

Package com. klstudio. Socket. Chat;

Import java. Io. bufferedreader;

Import java. Io. ioexception;

Import java. Io. inputstreamreader;

Import java. Io. printstream;

Import java.net. Socket;

// Import com. klstudio. util. Logger;

/**

* @ Author kinglong

*

* To change the template of the type annotation generated by todo, go to window-preferences-Java-code style-code template.

*/

Public class Client extends thread {

Private socket clientsocket;

Private string clientname;

Private string clientip;

Private bufferedreader BR;

Private printstream pS;

// Private logger;

Private chatserver server;

Public client (Socket socket ){

// This. Logger = new logger (system. Out );

This. clientsocket = socket;

Try {

This.br = new bufferedreader (New inputstreamreader (socket. getinputstream (), "UTF-8 "));

This. PS = new printstream (socket. getoutputstream (), true, "UTF-8 ");

String info = this.br. Readline ();

If (info! = NULL ){

String [] info_arr = info. Split (chatserver. commanddelimiters );

If (info_arr.length> 1 ){

This. clientname = info_arr [1];

}

This. clientip = socket. getremotesocketaddress (). tostring ();

} Else {

Socket. Close ();

}

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// This. Logger. Log ("error-" + E. tostring ());

}

}

/**

* @ Return returns the IP address.

*/

Public String getclientip (){

Return clientip;

}

/**

* @ Return returns the name.

*/

Public String getclientname (){

Return clientname;

}

/**

* @ Return returns socket.

*/

Public socket getclientsocket (){

Return clientsocket;

}

Public void send (stringbuffer MSG ){

This. PS. println (msg. tostring () + "\ 0 ");

// This. PS. Flush ();

}

Public void run (){

While (true ){

String line = NULL;

Try {

Line = this.br. Readline ();

} Catch (ioexception e ){

// Todo automatically generates catch Blocks

// This. Logger. Log ("error-" + E. tostring ());

Chatserver. Disconnect (this );

Chatserver. notifyroom ();

Return;

}

If (line = NULL ){

// This. Logger. Log ("information-[" + this. clientname + this. clientip + "] the user leaves! ");

Chatserver. Disconnect (this );

Chatserver. notifyroom ();

If (this. clientname! = NULL ){

Chatserver. sendclients (New stringbuffer ("MSG" + chatserver. commanddelimiters + "system information> [" + this. clientname + "] the user leaves! "));

}

Return;

}

// This. Logger. Log ("info-" + line );

String [] Line _arr = line. Split (chatserver. commanddelimiters );
String keyword = pai_arr [0];

Keyword = keyword. substring (1 );

If (keyword. Equals ("MSG ")){

Stringbuffer MSG = new stringbuffer ("MSG" + chatserver. commanddelimiters );

MSG. append (this. clientname + "> ");

MSG. append (pai_arr [1]);

Chatserver. sendclients (MSG, this. clientname, pai_arr [2]);

} Else if (keyword. Equals ("quit ")){

// This. Logger. Log ("information-[" + this. clientname + this. clientip + "] the user leaves! ");

Chatserver. Disconnect (this );

Chatserver. notifyroom ();

Chatserver. sendclients (New stringbuffer ("MSG" + chatserver. commanddelimiters + "system information> [" + this. clientname + "] the user leaves! "));

This. Stop ();

Return;

}

}

}

}

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.