澳门新葡萄京娱乐场php与java通过socket通信的实现代码

demo实现的简单功能是,接受PHP端写入的字符串,然后原样返回给输出端。代码如下:

Socket编程,为什么客户端无法接收来自服务器的数据?
如题,我写了段Socket服务器客户端通讯的程序,服务端能接收到客户端的数据,但是客户端就无法接收到服务端传回的数据,代码如下
Server端
[code=”java”]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

复制代码 代码如下:
import java.io.*;
import java.net.*;

/**

public class Server {
public static void main(String[] args) throws IOException{
  System.out.println(“Server started !n”);
  ServerSocket server=new ServerSocket(5678);
  while (true){
                Socket client=server.accept();
                System.out.println(“client coming!n”);
                PrintWriter printer = new
PrintWriter(client.getOutputStream());
                BufferedReader reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
                String  m = reader.readLine();
                System.out.println(“get infomation ” + m + “n from ” +
client.getInetAddress().toString());
                printer.println(m);
                printer.flush();
                printer.close();
                printer.close();
                client.close();
                System.out.println(“client leaving!n”);
              }
        }
}

  • @author Think
    *
    */
    public class SocketServer {

    /**

    • @param args*/public static void main(String[] args) {new
      SocketServer;}

    public void start(){
    try {
    ServerSocket server = new ServerSocket;
    while{
    System.out.println(“Waiting for a client…”);
    Socket client = server.accept();
    new SocketServerThread.start();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    class SocketServerThread extends Thread{
    Socket client = null;
    public SocketServerThread(Socket client){
    this.client = client;
    }
    @Override
    public void run(){
    try {
    // 得到CLIENT的输入流,从输入流出取出传输的数据
    BufferedReader reader_from_client = new BufferedReader(new
    InputStreamReader(client.getInputStream;
    String line = null;
    System.out.print(“from client message:”);
    while((line = reader_from_client.readLine != null){
    System.out.println;
    }
    // 得到CLIENT端的输入流,通过向CLIENT传输数据
    PrintWriter writer_to_client = new
    PrintWriter(client.getOutputStream;
    writer_to_client.println;
    writer_to_client.flush();

     reader_from_client.close(); writer_to_client.close(); client.close(); } catch (IOException e) { }}
    

    }
    }
    [/code]

运行起来,以后该java程序将监听5678端口,当接收到消息以后,将接收的消息原样返回给客户端……
PHP的代码如下:

Client
[code=”java”]import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

复制代码 代码如下:
    $socket = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP ) or die
( ‘could not create socket’ );
    $connect = socket_connect ( $socket, ‘127.0.0.1’, 5678 );
    //向服务端发送数据
    socket_write ( $socket, ‘Hello’ . “n” );
    //接受服务端返回数据
    $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );

/**

    echo $str;
    //关闭
    socket_close($socket);

  • @author Think
    *
    */
    public class SocketClient {

    /**

    • @param args
      */
      public static void main(String[] args) {
      try {
      Socket remoteServer = new Socket(InetAddress.getLocalHost;
      //
      得到SERVER端SOCKET的输出流,通过向输出流写入数据,与SERVER进行通信
      PrintWriter write_to_server = new
      PrintWriter(remoteServer.getOutputStream;
      write_to_server.write(“Hello, what’s you name?”);
      write_to_server.flush();

      // 把下面这几行放开,SERVER端就收不到数据了,一直处于阻塞状态// 得到SERVER端的输出流,从输出流中读取从SERVER传递来的数据
      

      // BufferedReader reader_from_server = new BufferedReader(new
      InputStreamReader(remoteServer.getInputStream;
      // String line = null;
      // while((line = reader_from_server.readLine != null){
      // System.out.println;
      // }
      // reader_from_server.close();

      write_to_server.close();remoteServer.close();
      

      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }[/code]

PHP程序连接本机的5678端口,写入
Hello,然后读取返回的数据……将返回的数据,输出到浏览器……
先运行起java的服务端,然后用浏览器访问PHP页面,将看到从服务器端返回的Hello

如果Client中的说明,只要是Client中放开了
[code=”java”]

BufferedReader reader_from_server = new BufferedReader(new
InputStreamReader(remoteServer.getInputStream;
String line = null;
while((line = reader_from_server.readLine != null){
System.out.println;
}
reader_from_server.close();
[/code]
这段代码,SERVER就阻塞在
[code=”java”]
while((line = reader_from_client.readLine != null){
[/code]
这行,不知道是哪个环节理解或处理错了,还望大家指点指点!

发表评论

电子邮件地址不会被公开。 必填项已用*标注