这段代码中什么决定了发送回客户端的内容? TCP 套接字

发布于 2024-12-11 09:00:13 字数 1290 浏览 0 评论 0原文

在下面的代码中,什么决定了将发送回客户端(PHP 页面)的内容。我正在尝试更改此设置,以便它将一个变量发送回 PHP 页面,并带有一条错误消息,该错误消息是根据我的 java 代码中所做的操作定义的。

编辑:为了回答一些问题,我想做的是这样的。

使用套接字将字符串发送到 java 脚本,并将其转换为要在 java 脚本中使用的变量。它将运行一些 if 语句,我需要将错误语句设置为一个变量,比如“reply”。我需要发送“回复”然后返回 PHP 文件。

public class MyJavaServer {
public static void main(String[] args) {

int port = 20222;
ServerSocket listenSock = null; //the listening server socket
Socket sock = null;             //the socket that will actually be used for communication

try {

   listenSock = new ServerSocket(port);

   while (true) {       //we want the server to run till the end of times

       sock = listenSock.accept();             //will block until connection recieved

       BufferedReader br = 
          new BufferedReader(new InputStreamReader(sock.getInputStream()));
       BufferedWriter bw = 
          new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
       String line = "";
       while ((line = br.readLine()) != null) {
           bw.write("PHP said: " + line + "\n");
           bw.flush();
       }

       //Closing streams and the current socket (not the listening socket!)
       bw.close();
       br.close();
       sock.close();
   }
} catch (IOException ex) {
   ex.printStackTrace();
}
}
}

In the code below, what determines what will be sent back to the client (the PHP page). I am trying to alter this so that it sends a variable back to the PHP page with an error message that is defined based on actions made in my java code.

Edit: To answer some questions, what I am trying to do is this.

Send a string to the java script with a socket and convert it to a variable to be used in the java script. It will run through some if statements and I need to set the error statements to a variable lets say "reply". I need to send "reply" then back to the PHP file.

public class MyJavaServer {
public static void main(String[] args) {

int port = 20222;
ServerSocket listenSock = null; //the listening server socket
Socket sock = null;             //the socket that will actually be used for communication

try {

   listenSock = new ServerSocket(port);

   while (true) {       //we want the server to run till the end of times

       sock = listenSock.accept();             //will block until connection recieved

       BufferedReader br = 
          new BufferedReader(new InputStreamReader(sock.getInputStream()));
       BufferedWriter bw = 
          new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
       String line = "";
       while ((line = br.readLine()) != null) {
           bw.write("PHP said: " + line + "\n");
           bw.flush();
       }

       //Closing streams and the current socket (not the listening socket!)
       bw.close();
       br.close();
       sock.close();
   }
} catch (IOException ex) {
   ex.printStackTrace();
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-12-18 09:00:13

如果我的问题正确,则将答案发送给对等方的行是

bw.write("PHP said: " + line + "\n");

将给定字符串写入 bw

If I get your question right, the line where the answer gets sent to the peer is

bw.write("PHP said: " + line + "\n");

which writes the given string to bw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文