3
我正在開發一個Java的TCP套接字服務器。該客戶機必須連接到網頁中(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 pH值=新的PacketHandler();
while ((inputLine = in.readLine()) != null)
{
outputLine = ph.processInput(inputLine);
out.println(outputLine);
}
他應該怎樣解決這個問題呢? – wouterdz
將\ n添加到我的inputLine不起作用。我真的很困難。 –