Sign in to follow this  
Followers 0
seesoe

VERY simple socket server

12 posts in this topic

hello, im trying to create a very simple socket server

im trying to make a socket client out of php that will send a string to the au3 socket server

here is what i got so far (i forgot where i got it from but it was some place on this forum, i changed how its displayed a little)

#include "TCP.au3"

TrayTip('SERVER:',"Creating server...",10,1)
$hServer = _TCP_Server_Create(4321)
sleep(6000)
TrayTip('SERVER:',"Server is runnning...",30)

_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server.
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects.

While 1
    
WEnd

Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function)
     TrayTip('SERVER:',"New client connected."&@CRLF&"Sending this: Bleh!",30)
     ; send something
     _TCP_Send($hSocket, "Bleh!")
EndFunc
 
; client lost
Func Disconnect($hSocket, $iError)
     TrayTip('SERVER:',"Client disconnected.",30)
EndFunc

i tried to use 2 different php socket client codes that send hello world but php is getting a timed out error

also i know something is going on between them but not correctly because when the server is running and i run the au3 socket server i instantly get a client disconnected message.

<?php
$host     = "server ip";
$port     = 4321; // nat opened port
$timeout = 6000;

$sk = fsockopen($host,$port,$errnum,$errstr,$timeout);

if (!is_resource($sk)) {
    exit("connection fail: ".$errnum." ".$errstr);
} else {
    fputs($sk, "hello world");
    $dati = "";
    while (!feof($sk)) {
        $dati.= fgets ($sk, 1024);
    }
}

fclose($sk);
echo($dati);
?>

Share this post


Link to post
Share on other sites



This works for me:

#include "TCP.au3"

TrayTip("SERVER","Creating server...",10,1)
$hServer = _TCP_Server_Create(4321)
sleep(1000)
TrayTip(";SERVER","Server is runnning...",30)


_TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received
_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get&apos;s called when a new client connects to the server.
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects.

While 1
   Sleep(10)
WEnd

     Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.
         TrayTip("SERVER", "We received this: "& $sReceived, 30); (and we'll display it)
     EndFunc

Func NewClient($hSocket, $iError); Yo, check this out! It&apos;s a $iError parameter! (In case you didn&apos;t noticed: It&apos;s in every function)
     TrayTip("SERVER","New client connected."&@CRLF&"Sending this: Bleh!",30)
     ; send something
     _TCP_Send($hSocket, "Bleh!")
EndFunc
 
; client lost
Func Disconnect($hSocket, $iError)
     TrayTip("SERVER","Client disconnected.",30)
EndFunc

<?php
$host     = "127.0.0.1";
$port     = 4321; // nat opened port
$timeout = 6000;

$sk = fsockopen($host,$port,$errnum,$errstr,$timeout);

if (!is_resource($sk) || $errnum) {
    exit("connection fail: ".$errnum." ".$errstr);
} else {
stream_set_blocking($sk, false);
    fwrite($sk, "hello world");
    $dati = "";
    while (!feof($sk)) {
        $chunk = fgets ($sk, 1024);
        if ($chunk !== False) {
            $dati.= $chunk;
            break;
        }
    }
    echo $dati;
}

fclose($sk);
?>

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

are you running that locally?

or on remote host with port forwarded?

Share this post


Link to post
Share on other sites

I run it locally with PHP 5.


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

i think that is my problem, im running it on a remote server, and i have my router open on port 4321.

is it possible like this? or do i need more ports open or something or some extra settings somewhere?..

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

It should be possible, i think. Have you tried the scrpts i posted? i changed them a bit. And if it doesn't work i can't help you since i can't reproduce it...

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

yeah, i still got the same results, im trying to get this to work now

error_reporting(E_ALL);

echo "<h2>TCP/IP Connection</h2>\n";

/* Get the port for the WWW service. */
$service_port = 4321;

/* Get the IP address for the target host. */
$address = gethostbyname('host name');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}

stream_set_blocking($socket, false);
$in = "Hello World!\r\n";
$out = '';

echo "Sending request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($chunk = socket_read($socket, 2048, PHP_NORMAL_READ)) {
        if ($chunk !== False) {
            $out.= $chunk;
            break;
        }
        echo $out;
}

echo "Closing socket...";
socket_close($socket);
echo "OK.";
can you tell me if that worked for you with the same server you posted? Edited by seesoe

Share this post


Link to post
Share on other sites

i think that is my problem, im running it on a remote server, and i have my router open on port 4321.

Webhosts don't allow you to create servers. And you (the client) don't need to forward ports, only the server. And since the server is a webhost you can't forward any ports on their routers.

Share this post


Link to post
Share on other sites

that is incorrect, my computer is running the autoit socket server, and the webhost is running the client php script

Share this post


Link to post
Share on other sites

It works, but the receive.code is not correct. It should be this:

while ($chunk = socket_read($socket, 2048, PHP_NORMAL_READ)) {
        if ($chunk !== False) {
            $out.= $chunk;
            break;
        }
}
        echo $out;

and for PHP_NORMAL_READ, you have to add a lf in the autoit-send or you have to remove this parameter.

_TCP_Send($hSocket, "Bleh!"&@LF)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

ok thank you, i removed that parameter, and i moved the echo out the loop.

i just wanted to make sure the client script works and the server script works, so that i can see what exactly is going on.

Share this post


Link to post
Share on other sites

i did a test with a remote vnc server of mine with a autoit socket client and it worked fine, i guess im done where i need to move on to a php forum

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0