Bom,
Meu objetivo é fazer uma comunicação de um server rodando php com um aplicativo java utilizando sockets…
O server php ja ta funcionando, testei utilizando um aplicativo SocketWorkbench.
Meu servidor basicamente (e inicialmente para testes) reenvia ao cliente a mensagem que o cliente enviou.
fiz seguinte teste…
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class App {
/**
* @param args
*/
private static final String url = "localhost";
private static final int port = 8000;
private static Socket socket;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream out;
InputStream in;
socket = new Socket(url, port);
out = socket.getOutputStream();
in = socket.getInputStream();
out.write('f');
out.flush();
int i = in.read();
System.out.println(i);
out.close();
in.close();
socket.close();
}
}
mas o aplicativo fica na espera na linha 30, meio que esperando uma resposta…
Pedindo ajuda, nao faço ideia…
segue o codigo php caso seja necesário
[code]<?php
//Tempo indefinido.
set_time_limit (0);
$port = 8000;
$max_clients = 10;
$server_ip = '127.0.0.1';
$connectionOk = "Communication Established";
$downloadDB = "Download Database";
$downloadStart = "Initializing download: ";
$transmitionOk = "Done";
$downloadImg = "Download Images";
$uploadSales = "Upload sales";
$endMsg = "\0";
//Não Sei
// error_reporting(1);
// ini_set(‘display_errors’, ‘1’);
//Cria o socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//nao faço ideia.
// socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
// especifica o ip e a porta de escuta
socket_bind($socket, $server_ip, $port);
// Deixa o socket em estado de escuta.
socket_listen($socket, $max_clients);
// Cria um vetor, que na posição 0 é um (vetor que na posição 'sockets' é $socket
$clients = array('0' => array('socket' => $socket));
while(TRUE)
{
$read[0] = $socket;
//Prepara os clients para leitura.
for($i=1; $i<count($clients)+1; ++$i)
{
if($clients[$i] != NULL)
{
$read[$i+1] = $clients[$i]['socket'];
}
}
// socket_select() accepts arrays of sockets and waits for them to change status.
// On success socket_select() returns the number of socket resources contained in the modified arrays, which may be zero if the // Timeout expires before anything interesting happens.
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
//verifica se está cotido em...
if(in_array($socket, $read))
//objetivo do if. pega um unico socket e taca no clients, aceita um client.
{
for($i=1; $i < $max_clients+1; ++$i)
{
//Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
if(!isset($clients[$i]))
{
//retorna o proximo client pronto
$clients[$i]['socket'] = socket_accept($socket);
//pega ip.
socket_getpeername($clients[$i]['socket'],$ip);
$clients[$i]['ipaddy'] = $ip;
//socket_write($clients[$i]['socket'], 'Communication Established');
/*socket_write($clients[$i]['socket'], 'There are '.(count($clients) - 1).' client(s) connected to this server.'."\r\n");*/
//echo 'New client connected: ' . $clients[$i]['ipaddy'] .' ';
break;
}
elseif($i == $max_clients - 1)
{
//echo 'To many Clients connected!'."\r\n";
}
if($ready < 1)
{
continue;
}
}
}
for($i=1; $i<$max_clients+1; ++$i)
{
//Se está no array de leitura, ou seja, pronto
if(in_array($clients[$i]['socket'], $read))
{
// ler se há data. se tiver retorna.
$data = @socket_read($clients[$i]['socket'], 1024, PHP_NORMAL_READ);
if($data === FALSE)
{
// destroys the specified variables;
unset($clients[$i]);
//echo 'Client disconnected!',"\r\n";
continue;
}
// returns a string with whitespace stripped from the beginning and end of str.
$data = trim($data);
if(!empty($data))
{
if($data == 'exit')
{
//socket_write($clients[$i]['socket'], 'Thanks for trying my Custom Socket Server, goodbye.'."\n");
//echo 'Client ',$i,' is exiting.',"\n";
unset($clients[$i]);
continue;
}
for($j=1; $j<$max_clients+1; ++$j)
{
//Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
if(isset($clients[$j]['socket']))
{
if(($clients[$j]['socket'] != $clients[$i]['socket']) && ($clients[$j]['socket'] != $socket))
{
echo($clients[$i]['ipaddy'] . ' is sending a message!'."\r\n");
socket_write($clients[$j]['socket'], '[' . $clients[$i]['ipaddy'] . '] says: ' . $data . "\r\n");
}
}
}
break;
}
}
}
}
?>
[/code]