TCP communication between Java and flash (xmlsocket class)

Source: Internet
Author: User

Flash code: var mysocket: xmlsocket = new xmlsocket ();
// Local address, port 5000
Mysocket. Connect ("localhost", 5000 );
// Event
Mysocket. onconnect = function (mystatus)
{
If (mystatus)
{
Trace ("connection successful! ");
// Send information
Mysocket. Send ("soda" + "\ n ");
Trace ("sent successfully! ");
}
Else
{
Trace ("connection failed! ");
}
};
// Accept information events
Mysocket. ondata = function (MSG: string)
{
Trace ("received content:" + MSG );
}
--------------------------------------------------
Java code:
Import java.net .*;
Import java. Io .*;

Public class Server
{
Public static void main (string [] ARGs) throws exception
{
// Create a server (TCP)
Serversocket Ss = new serversocket (5000 );
System. Out. println ("waiting for connection .......");
Socket Sk = ss. Accept ();
System. Out. println ("connection successful ......");
// Create a producer stream
Bufferedreader BR = new bufferedreader (New inputstreamreader (SK. getinputstream ()));
System. Out. println ("Get bufferedreader ......");
System. Out. println ("Get the content ......");
// Wait for receiving information
String name = Br. Readline ();

// Send information
Printwriter OS = new printwriter (SK. getoutputstream ());
OS. println ("soda, soda" + "\ 0 ");
OS. Flush ();
OS. Close ();
System. Out. println ("content:" + name );
}
}

-----------------------------------------------------------
It is worth noting that mysocket. Send ("soda" + "\ n ");
If "\ n" is not added, the Java Server cannot accept the information immediately.
Because Br. Readline () accepts a row, if "\ n" is not found,
If it cannot find the line break symbol, it will listen and wait there without responding.
(Khan ~~~ I am stuck here for a long time)

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.