How to open a socket?

Following, you will find some examples on how to open a socket in different programming languages.

Example for AS1/AS2

class SOS{

private static varsocket :XMLSocket;

public static function main(container : MovieClip : void {

socket = new XMLSocket ();

socket.connect("localhost", 4444);

socket.onConnect = onConnect;

}

private static function onConnect() : void {

socket.sent("Hello World!\n");

}

}

Example for AS3

package {

import flash.display.Sprite;

import flash.events.Event;

import flash.events.IOErrorEvent;

import flash.events.SecurityErrorEvent;

import flash.net.XMLSocket;

public class SOS extends Sprite {

private var socket : XMLSocket;

public function SOS() {

socket = new XMLSocket;

socket.addEventListener(Event.CONNECT,onConnect);

socket.addEventListener(IOErrorEvent.IO_ERROR,onError);

socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);

try {

socket.connect("localhost", 4444);

}catch (error : SecurityError) {

trace("SecurityError in SOSAppender: " + error);

}

}

private function onError(event : Event) : void {

trace("Error in SOSAppender: " + event);

}

private function onConnect(event : Event) : void {

socket.send("Hello World!\n");

}

}

}

Example for PHP

=fsockopen ("localhost",4444,$errno,$errstr,1);

fwrite($fp,"Hello World!".chr(0));

Example for JAVA

import java.io.IOException;

import java.io.PointWriter;

import java.net.Socket;

import java.net.UnknownHostException;

public class SOS{

public static void main(String[] args)throws UnknownHostException,

IOException {

Socket socket = new Socket("localhost", 4444);

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

writer.write("Hello World!" + (char) 0);

writer.flush();

}

}