简体   繁体   English

带有 PHP 客户端的 Java 套接字服务器

[英]Java Socket Server with PHP client

the question is totally rewritten since I have understood that previously it was really unclear.这个问题被完全改写了,因为我知道以前真的不清楚。 I have created a Java Socket server with threads to accept multiple connection in order to handle php tcp requests.我创建了一个带有线程的 Java Socket 服务器来接受多个连接,以便处理 php tcp 请求。 The java server just for the testing purposes it reverse the string supplied from php. Java 服务器仅用于测试目的,它反转 php 提供的字符串。 Java server is hosted on a ubuntu server and the php is located on another machine. Java 服务器托管在 ubuntu 服务器上,而 php 位于另一台机器上。 The java server shows that php client is connected, but the php is not loading and the string is not sent. java服务器显示php客户端已连接,但php未加载,字符串未发送。 From the codes given below what could be the mistake?从下面给出的代码中,可能是什么错误?

UPDATE the problem is the received string from the Java server.更新问题是从 Java 服务器接收到的字符串。 I have checked with debugger and the BufferedReader is full of '\' and server stops responding.我已经用调试器检查过,BufferedReader 充满了 '\' 并且服务器停止响应。 The rest code and communication is working perfect.其余的代码和通信工作完美。 How I can avoid those null characters or decode the string correct?如何避免那些空字符或正确解码字符串?

ReverseServer反向服务器

import java.io.*;
import java.net.*;

public class ReverseServer {

public static void main(String[] args) {

    int port = 10007;

    try (ServerSocket serverSocket = new ServerSocket(port)) {

        System.out.println("Server is listening on port " + port);

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("New client connected");

            new ServerThread(socket).start();
        }

    } catch (IOException ex) {
        System.out.println("Server exception: " + ex.getMessage());
        ex.printStackTrace();
    }
}
}

ServerThread服务器线程

import java.io.*;
import java.net.*;

public class ServerThread extends Thread {
private Socket socket;

public ServerThread(Socket socket) {
    this.socket = socket;
}

public void run() {
    try {
        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        OutputStream output = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(output, true);


        String text;

        do {
            text = reader.readLine();
            String reverseText = new StringBuilder(text).reverse().toString();
            writer.println("Server: " + reverseText);

        } while (!text.equals("bye"));

        socket.close();
    } catch (IOException ex) {
        System.out.println("Server exception: " + ex.getMessage());
        ex.printStackTrace();
    }
}
}

PHP client PHP客户端

<?php

// websocket connection test
$host    = "ip_of_server";
$port    = 10007;
$message = "Hello";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('TCP'));
$result = socket_connect($socket, $host, $port);

if ($result) {
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  :" . $result;
}

socket_close($socket);

I am trying to read a line, but on the string given on the php client didn't had the carriage return symbol "\\r".我正在尝试读取一行,但是在 php 客户端上给出的字符串上没有回车符“\\r”。 Once I have put this on the string it works as expected.一旦我把它放在字符串上,它就会按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM