First what is socket.
The Socket class enables ActionScript code to make socket connections and to read and write raw binary data. It is similar to XMLSocket but does not dictate the format of the received or transmitted data.
The Socket class is useful for working with servers that use binary protocols.
When you use this class, consider the Flash Player security model:
- Data loading is not allowed if the calling SWF file is in the local-with-file-system sandbox and the target resource is from a network sandbox.
- Data loading is also not allowed if the calling SWF file is from a network sandbox and the target resource is local.
- The calling SWF file and the network resource being accessed must be in exactly the same domain. For example, a SWF file at adobe.com can connect only to a server daemon at adobe.com.
- Websites can permit cross-domain access to a resource through a cross-domain policy file.
- create new as file
Save it name clientConnect.as
and add this code.
package
{
import flash.net.Socket;
import flash.events.Event;
import flash.events.ProgressEvent;
////////////////////////////////////////////////////////////// import some class that have to use for this project
public class clientConnect
{
private var client:Socket = new Socket();// instance of Socket class we will use this instace to connect to demon.
private var s_path:String = "";//variable for set server path .
private var s_port:uint;//variable for set server port .
private var returnData:uint;//variable for store data that client recive form server.
private var c_data:uint;//variable for store data that wee want to sent to server.
private var recived:Boolean=false;//variable for check client recived data yet.
public function clientConnect(path:String,port:uint)//when we define class variable and call this method we must sent serve path and port to this method.
{
s_path=path;
s_port=port;
}
public function connected()//sure this function try to connect to server*-*.
{
try
{
client.connect(s_path,s_port);
}
catch (e:ArgumentError)
{
trace(e);
}
client.addEventListener(Event.CONNECT,sentData);
client.addEventListener(ProgressEvent.SOCKET_DATA,reciveData);
}
public function setData(data_in:uint)//set data that we want to sent to server.
{
c_data = data_in;
}
private function sentData(evt:Event):void
{
client.writeInt(c_data);
}
public function getData():uint//when we need to use the data that server had sent to client call this function .
{
if(recived)
return returnData;
else
return null;
}
private function reciveData(evt:ProgressEvent)
{
returnData = client.readInt();
recived = true;
trace(returnData)
client.close();
}
}
}
and add this code.
package
{
import flash.net.Socket;
import flash.events.Event;
import flash.events.ProgressEvent;
////////////////////////////////////////////////////////////// import some class that have to use for this project
public class clientConnect
{
private var client:Socket = new Socket();// instance of Socket class we will use this instace to connect to demon.
private var s_path:String = "";//variable for set server path .
private var s_port:uint;//variable for set server port .
private var returnData:uint;//variable for store data that client recive form server.
private var c_data:uint;//variable for store data that wee want to sent to server.
private var recived:Boolean=false;//variable for check client recived data yet.
public function clientConnect(path:String,port:uint)//when we define class variable and call this method we must sent serve path and port to this method.
{
s_path=path;
s_port=port;
}
public function connected()//sure this function try to connect to server*-*.
{
try
{
client.connect(s_path,s_port);
}
catch (e:ArgumentError)
{
trace(e);
}
client.addEventListener(Event.CONNECT,sentData);
client.addEventListener(ProgressEvent.SOCKET_DATA,reciveData);
}
public function setData(data_in:uint)//set data that we want to sent to server.
{
c_data = data_in;
}
private function sentData(evt:Event):void
{
client.writeInt(c_data);
}
public function getData():uint//when we need to use the data that server had sent to client call this function .
{
if(recived)
return returnData;
else
return null;
}
private function reciveData(evt:ProgressEvent)
{
returnData = client.readInt();
recived = true;
trace(returnData)
client.close();
}
}
}
At the main swf file add the code like this.
import clientConnect;
var client:clientConnect = new clientConnect("localhost",1500);
client.setData(100);//sent 100 te server.
client.connected();
import clientConnect;
var client:clientConnect = new clientConnect("localhost",1500);
client.setData(100);//sent 100 te server.
client.connected();
Now we had finished the client codding.
next go to fun with some easy java code for demon!
you must have jdk download it!
- create new text file and save its name Server.java
- add this code
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
{
while(true)
{
try{
ServerSocket serverSocket = new ServerSocket(1500); //new server socket instace that set port 1500 (flash client must set like this demon)
System.out.println("Please, wait....");
Socket clientSocket = serverSocket.accept(); //accepted client who connected to this port
System.out.println("Connected : "+clientSocket.getInetAddress().toString());
DataInputStream inData = new DataInputStream(clientSocket.getInputStream()); //instace for store data from client
DataOutputStream outData = new DataOutputStream(clientSocket.getOutputStream());//instace for sent data to client.
System.out.println("recive Data ...."+inData.readInt()+" and return Data 20");
outData.writeInt(20); //write 20 to flash client
clientSocket.close();
serverSocket.close();
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
}
You can implement this bacsic socket project to make your own flash chat,mutiplayer game,
or some easy realtime msg sending.
Thank ! for visited this blog.
Hudsadin keox
2 comments:
Thanks a lot sir. It was very useful :)
horrible conventions
Post a Comment