12.01.2013, 23:59
Bem, hoje eu decidi experimentar mais uma coisinha e entao foi atй б area de plugins e lembreime de sockets, que tinha grande curiosidade, hora, vou ser sincero, passei as ultimas 4 horas ou 5 prae a procura de tutoriais e exemplos de codigos e a fazer experiencias e atй agora nao consegui nada decente. Jб sei que tem que haver um servidor e um cliente, nao percebo bem isso, pensei que so o server tinha de criar socket, entretanto tbm encontrei a difereca entre TCP e UDP e mais umas quantas coisas, mas nao consegui fazer nada de jeito. Alguйm sabe me explicar como fazer? Como funciona isso?
eu usei esse codigo (e sу funcionou com a porta 80, nem com a 7777, nada, alguem me pode dar uma luz sobre isto? fico agradecido)
OBS. Codigo nao й da minha autoria
no codigo php usei isso
tentei abrir a pagina pelo browser (Opera) mas deu erro
ahh, e no response recebo isso
eu usei esse codigo (e sу funcionou com a porta 80, nem com a 7777, nada, alguem me pode dar uma luz sobre isto? fico agradecido)
OBS. Codigo nao й da minha autoria
pawn Код:
#include <a_samp>
#include <socket>
new Socket:Client;
public OnFilterScriptInit()
{
// Create the client socket (feed data to webserver)
Client = socket_create(TCP);
if(is_socket_valid(Client))
{
/*
Exemplo de aplicacoes:
portas 20/21 - FTP
porta 23 TELNET
porta 25 SMTP
porta 80 HTTP
*/
socket_bind(Client,"127.0.0.1");
socket_listen(Client,80);
socket_connect(Client, "127.0.0.1",80);
socket_send(Client, "Hi, server!\n",24);
}
return 1;
}
public OnFilterScriptExit()
{
// Destroy the socket if it is valid!
if(is_socket_valid(Client))
socket_destroy(Client);
}
// Any response from the PHP script (through PHP func socket_write) will fire this callback:
public onSocketAnswer(Socket:id, data[], data_len)
{
printf("onSocketAnswer(%d, %s, %d)", _:id, data, data_len);
}
PHP код:
<html>
<head>
</head>
<body>
<?php
// Time limit and flushing setting
set_time_limit(0);
ob_implicit_flush();
// Create the socket and make it listen for 127.0.0.1 on port 7778.
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')) or die("Error in socket_create: " . socket_strerror(socket_last_error($socket)) . "\n");
socket_bind($socket, "127.0.0.1", 80) or die("Error in socket_bind: " . socket_strerror(socket_last_error($socket)) . "\n");
socket_listen($socket, 5) or die("Error in socket_listen: " . socket_strerror(socket_last_error($socket)) . "\n");
while(true)
{
// Accept an incoming connection!
$spawn = socket_accept($socket);
// Send a welcoming notice
$welcome = "I'm your humble host today, dear gameserver!";
socket_write($spawn, $welcome, strlen($welcome));
while(true)
{
// Try to read the input
$input = socket_read($spawn, 1024, PHP_NORMAL_READ);
if($input === false)
{
// Return to the first loop and look for a new connection to accept.
break;
}
if($input == "Hi, server!")
{
$response = "Hello to you as well, dear Sir!";
socket_write($spawn, $response, strlen($response));
}
}
}
socket_close($socket);?>
</body>
</html>
Код:
Warning: socket_bind(): in C:\xampp\htdocs\testesocket.php on line 12 Error in socket_bind: Normalmente sу й permitido uma utilizaзгo de cada endereзo de socket (protocolo/endereзo de rede/porta).
Код:
onSocketAnswer(0, <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Bad request!</title> <link rev="made" href="mailto:postmaster@localhost" /> <style type="text/css"><!--/*--><![CDATA[/*><!--*/ body { color: #000000; background-color: #FFFFFF; } a:link { color: #0000CC; } p, address {margin-left: 3em;} span {font-size: smaller;} /*]]>*/--></style> </head> <body> <h1>Bad request!</h1> <p> Your browser (or proxy) sent a request that this server could not understand. </p> <p> If you think this is a server error, please contact the <a href="mailto:postmaster@localhost">webmaster</a>. </p> <h2>Error 400</h2> <address> <a href="/">localhost</a><br /> <span>Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4</span> </address> </body> </html> , 979)