Java 无法从PHP套接字服务器获取响应
我制作了简单的套接字java客户端和基于php cli的套接字echo服务器。Java 无法从PHP套接字服务器获取响应,java,php,sockets,connection,Java,Php,Sockets,Connection,我制作了简单的套接字java客户端和基于php cli的套接字echo服务器。 但客户端无法从服务器获取回显。 有一个密码 [客户端类(JAVA)] import java.net.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; public class SocketManager { public static void main(String args[])
但客户端无法从服务器获取回显。
有一个密码 [客户端类(JAVA)]
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SocketManager
{
public static void main(String args[])
{
int count = 0;
Socket socket = null;
System.out.println("[TO 8175] Connecting to " + "clashofages.ru" + ":" + 8175 + "...");
while (socket == null) {
if (count > 10) {
System.out.println("[TO 8175] Can't connect to server...");
return 1;
}
socket = connect("clashofages.ru", 8175);
count++;
}
System.out.println("[TO 8175] Successfuly connected!");
DataInputStream in = getInput(socket);
if (in == null) {
return 2;
}
DataOutputStream out = getOutput(socket);
if (out == null) {
return 3;
}
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
System.out.println("[TO 8175] Type in message for server.");
try {
line = keyboard.readLine();
} catch (IOException ex) {
Logger.getLogger(SocketManager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("[TO 8175] Sending message to server...");
int writeres = write(out, line);
if (writeres != 2) {
return 4 + writeres;
}
System.out.println("[TO 8175] Message sent successfuly!");
if (line.equals("exit")) {
System.out.println("[TO 8175] Exiting...");
return 0;
}
line = read(in);
if (line == null) {
return 6;
}
System.out.println("[TO 8175] Server: " + line);
}
}
public static Socket connect(String adress, int port)
{
Socket socket = null;
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(adress);
} catch (UnknownHostException ex) {
System.out.println("[SOCKET_ERROR] Can't convert " + adress + " to InetAdress object");
return socket;
}
try {
socket = new Socket(ipAddress, port);
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't connect to " + adress + ":" + port);
return null;
}
return socket;
}
public static DataInputStream getInput(Socket socket)
{
InputStream sin = null;
try {
sin = socket.getInputStream();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't get socket's input stream");
return null;
}
return new DataInputStream(sin);
}
public static DataOutputStream getOutput(Socket socket)
{
OutputStream sout = null;
try {
sout = socket.getOutputStream();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't get socket's output stream");
return null;
}
return new DataOutputStream(sout);
}
public static int write(DataOutputStream out, String string)
{
try {
out.writeUTF(string);
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't write to output stream");
return 0;
}
try {
out.flush();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't flush output stream");
return 1;
}
return 2;
}
public static String read(DataInputStream in)
{
try {
return in.readUTF();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't read input stream");
return null;
}
}
#!/usr/bin/php
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '0.0.0.0';
$port = 8175;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "[SERVER] socket_create() failed: ".socket_strerror(socket_last_error())."\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "[SERVER] socket_bind() failed: ".socket_strerror(socket_last_error($sock))."\n";
}
if (socket_listen($sock, 5) === false) {
echo "[SERVER] socket_listen() failed: ".socket_strerror(socket_last_error($sock))."\n";
}
echo "[SERVER] Start listening ".$address.":".$port."\n";
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "[SERVER] socket_accept() failed: reason: ".socket_strerror(socket_last_error($sock))."\n";
break;
}
echo "[SERVER] Client connected!\n";
$msg = "[SERVER] Welcome!\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "[SERVER] socket_read() failed: ".socket_strerror(socket_last_error($msgsock))."\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
$talkback = "[SERVER] You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "[CLIENT] $buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
[服务器客户端类(PHP)]
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SocketManager
{
public static void main(String args[])
{
int count = 0;
Socket socket = null;
System.out.println("[TO 8175] Connecting to " + "clashofages.ru" + ":" + 8175 + "...");
while (socket == null) {
if (count > 10) {
System.out.println("[TO 8175] Can't connect to server...");
return 1;
}
socket = connect("clashofages.ru", 8175);
count++;
}
System.out.println("[TO 8175] Successfuly connected!");
DataInputStream in = getInput(socket);
if (in == null) {
return 2;
}
DataOutputStream out = getOutput(socket);
if (out == null) {
return 3;
}
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
System.out.println("[TO 8175] Type in message for server.");
try {
line = keyboard.readLine();
} catch (IOException ex) {
Logger.getLogger(SocketManager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("[TO 8175] Sending message to server...");
int writeres = write(out, line);
if (writeres != 2) {
return 4 + writeres;
}
System.out.println("[TO 8175] Message sent successfuly!");
if (line.equals("exit")) {
System.out.println("[TO 8175] Exiting...");
return 0;
}
line = read(in);
if (line == null) {
return 6;
}
System.out.println("[TO 8175] Server: " + line);
}
}
public static Socket connect(String adress, int port)
{
Socket socket = null;
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(adress);
} catch (UnknownHostException ex) {
System.out.println("[SOCKET_ERROR] Can't convert " + adress + " to InetAdress object");
return socket;
}
try {
socket = new Socket(ipAddress, port);
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't connect to " + adress + ":" + port);
return null;
}
return socket;
}
public static DataInputStream getInput(Socket socket)
{
InputStream sin = null;
try {
sin = socket.getInputStream();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't get socket's input stream");
return null;
}
return new DataInputStream(sin);
}
public static DataOutputStream getOutput(Socket socket)
{
OutputStream sout = null;
try {
sout = socket.getOutputStream();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't get socket's output stream");
return null;
}
return new DataOutputStream(sout);
}
public static int write(DataOutputStream out, String string)
{
try {
out.writeUTF(string);
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't write to output stream");
return 0;
}
try {
out.flush();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't flush output stream");
return 1;
}
return 2;
}
public static String read(DataInputStream in)
{
try {
return in.readUTF();
} catch (IOException ex) {
System.out.println("[SOCKET_ERROR] Can't read input stream");
return null;
}
}
#!/usr/bin/php
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '0.0.0.0';
$port = 8175;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "[SERVER] socket_create() failed: ".socket_strerror(socket_last_error())."\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "[SERVER] socket_bind() failed: ".socket_strerror(socket_last_error($sock))."\n";
}
if (socket_listen($sock, 5) === false) {
echo "[SERVER] socket_listen() failed: ".socket_strerror(socket_last_error($sock))."\n";
}
echo "[SERVER] Start listening ".$address.":".$port."\n";
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "[SERVER] socket_accept() failed: reason: ".socket_strerror(socket_last_error($sock))."\n";
break;
}
echo "[SERVER] Client connected!\n";
$msg = "[SERVER] Welcome!\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "[SERVER] socket_read() failed: ".socket_strerror(socket_last_error($msgsock))."\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
$talkback = "[SERVER] You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "[CLIENT] $buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
客户端输出:
[TO 8175] Connecting to clashofages.ru:8175...
[TO 8175] Successfuly connected!
[TO 8175] Type in message for server.
Hi!
[TO 8175] Sending message to server...
[TO 8175] Message sent successfuly!
有什么想法吗,兄弟们
另外,我认为当PHP试图写入客户端时会出现问题…还有,你可以自己试试。服务器正在工作。读取地址: PHP\u正常\u读取-读取在\n或\r处停止 阅读时间: 返回:包含行内容的字符串,不包括 任何行终止字符,如果流的结尾有 联系到
所以,在发送之前,您必须向读取的字符串添加\n或\n。现在服务器显示
[CLIENT]hi
,但我仍然无法在CLIENT中获得响应!而且[SERVER]欢迎
不会出现在客户端日志中。。。所以在服务器端编写中有一个问题。。。或者在UTF中读取客户端。请尝试使用BufferedReader如下方式:new BufferedReader(new InputStreamReader(socket.getInputStream())并使用readLine方法复制DataInputStream