2013-05-20 3 views
3

Привет,,Сервер сокетов Java TCP с PHP-клиентом?

Я разрабатываю сервер сокетов TCP в Java. Клиент (ы) должен подключиться к веб-странице (в PHP)

Ну, вот моя проблема .. Они могут подключиться к указанному хосту, но сервер не может прочитать пакеты, которые отправляет клиент.

Если я создаю клиент на Java, он работает на 100%. Ну, вот некоторые фрагменты моего кода. Надеюсь, у кого-то есть ответ для меня. Потому что я застрял.

Это мой маленький PHP-скрипт, который посылает:

<?php 

set_time_limit(0); 

$PORT = 1337; //the port on which we are connecting to the "remote" machine 
$HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine) 
$sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket 
    or die("error: could not create socket\n"); 

$succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket 
    or die("error: could not connect to host\n"); 

$text = "wouter123"; //the text we want to send to the server 

socket_sendto($sock, $text, strlen($message), MSG_EOF, '127.0.0.1', '1337'); 
//socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket 
//  or die("error: failed to write to socket\n"); 



$reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket 
    or die("error: failed to read from socket\n"); 


echo $reply; 
?> 

Боковая розетка:

package com.sandbox.communication; 

public class PacketHandler { 

public String processInput(String theInput) { 
    String theOutput = null; 

    if (theInput == "wouter123") { 
     theOutput = theInput; 
    }else { 
     theOutput = "Cannot find packet. The output packet is " + theInput; 
    } 

    return theOutput; 
} 

}

И этот маленький код подключается к PacketHandler: PacketHandler тел = новый пакет PacketHandler();

 while ((inputLine = in.readLine()) != null) 
     { 

      outputLine = ph.processInput(inputLine); 

      out.println(outputLine); 
     } 

ответ

2

Поскольку вы используете readLine в своем потоке ввода, убедитесь, что ваши клиенты отправляют данные с помощью перевода строки.

От javadocs

ReadLine() Считывает строку текста. Линия считается завершенной любым каналом ('\ n'), возвратом каретки ('\ r') или возвратом каретки , за которым следует сразу строка.

+0

Как его исправить? – wouterdz

+1

Добавление \ n к моему inputLine не работает. Я действительно, действительно застрял. –

Смежные вопросы