Socket-related programming has been introduced in as2.0, And the socket class has been added after as2.0, which enables more powerful socket-related programming, however, Flash has not yet achieved the ability to write server-related classes, so we have to use Java Socket to write a simple communication method. One afternoon's study initially reflected a little. At the beginning, I used Java to compile a client and the server for a simple communication effect, and then I could use that server to perform a simple communication test.
Main class:
Import java. Lang .*;
Import java.net .*;
Public class mainserver {
Public static void main (string [] ARGs)
{
Try {
Serversocket Ss = new serversocket (2900 );
Boolean begin = true;
While (BEGIN)
{
System. Out. println ("waiting for connection ");
Socket mysocket = ss. Accept ();
System. Out. println ("connection successful ");
Thread TD = new thread (New myserver2 (mysocket ));
TD. Start ();
}
}
Catch (exception ex ){
System. Err. println (Ex );
}
}
}
// Use the interface to use the thread
Import java. Lang .*;
Import java.net .*;
Import java. Io .*;
Public class myserver2 implements runnable {
Private socket connection = NULL;
Public myserver2 (socket mysocket)
{
This. Connection = mysocket;
}
Public void run ()
{
Printwriter PW = NULL;
// Socket connection = NULL;
Try {
// Serversocket Ss = new serversocket (8000 );
// Connection = ss. Accept (); // The connection is blocked until a connection is interrupted and a socket object is returned, so that the socket is connected to the server.
Bufferedreader userin = new bufferedreader (New inputstreamreader (system. In); // The server reads data from the keyboard to the buffer zone.
Bufferedreader BR = new bufferedreader (New inputstreamreader (connection. getinputstream (); // read the information in the socket
PW = new printwriter (connection. getoutputstream ());
While (true)
{
String strline = Br. Readline (); // read socket data
System. Out. println (strline );
String word = userin. Readline ();
PW. println (Word );
PW. Flush ();
}
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
Finally
{
Try
{
If (connection! = NULL)
Connection. Close ();
}
Catch (ioexception ex ){
}
}
}
}
// Code in Flash
Package {
Import flash.net. Socket;
Import flash. Events .*;
Import flash. display. Sprite;
Import flash. errors .*;
Public class myclient extends sprite {
Private var mysocket: socket;
Public Function myclient (){
Mysocket = new socket ();
Mysocket. addeventlistener (event. Connect, onconnect );
Mysocket. addeventlistener (ioerrorevent. io_error, ioerrorhandler );
Mysocket. Connect ("localhost", 2900 );
}
Private function onconnect (E: Event): void {
Trace ("connection successful ");
Mysocket. writemultibyte ("test successful", "Unicode ");
Mysocket. Flush ();
}
Private function ioerrorhandler (E: Event): void {
Trace ("connection failed ");
}
}
}
Problem: When I opened the firewall and tested whether flash could communicate with Java through socket, I found that it could not be connected all the time and can be connected when it was closed. I do not know whether the firewall has intercepted the packets sent by the socket during connection.
Problem 2: the response time of the Java program is slow. After using the thread, I don't know if a problem has occurred.
Question 3: socket programming has always been relatively small. I often don't know what the application is in? Can you know a communication version ,?
Problem 5: If Java's non-blocking Io implementation is used, the performance will be improved more effectively. If there are one hundred flash client, how can we improve the server performance? This is a consideration.
Question 6: what are the difficulties and highlights of using Java Io streams?