php和java的socket 通信

php和Java的socket通信

通过php给Java发送请求,java给树莓派发送请求控制树莓派

进而通过网页就可以对所有模块进行控制

php代码

<?php
  $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, '1' . "\n" );
  $str = socket_read ( $socket, 1024, PHP_NORMAL_READ );  
  echo $str;
  socket_close($socket);
 ?>

java代码

控制切割视频的方法

package srv_cntl;

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.util.Random;

public class php_loadfile_java {
    public static String is_end_status="end";

    public static void main(String args[]) throws IOException {
        ServerSocket server = new ServerSocket(6666);

        while (true) {
            Socket client = server.accept();
            PrintWriter printer = new PrintWriter(client.getOutputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String m = reader.readLine();
            System.out.println("hello"+new Random().nextFloat());
            System.out.println(m);

            if(m.charAt(0)=='1')
            {

                String str[]=m.split("&");
                try
                {
                    String path=str[1];
                    String start_time=str[2];
                    String end_time=str[3];
                    printer.println("start...");
                    printer.flush();
                    printer.close();
                    client.close();

                    /*******************调用方法***********************/
                    /*-----------------------------------------------*/
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                is_end_status="not end";
                                Thread.sleep(20000);
                                is_end_status="end";
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                    /*************************************************/
                }
               catch(Exception e)
                {
                    System.err.println("开始错误!");
                    printer.println("start err!");
                    printer.flush();
                    printer.close();
                    client.close();
                }

            }
            else
            {
                if(m.charAt(0)=='2')
                {
                    printer.println(is_end_status);
                    printer.flush();
                    printer.close();
                    client.close();
                }
                else
                {
                    printer.println("para err!");
                    printer.flush();
                    printer.close();
                    client.close();
                }
            }

            System.out.println("client leaving!\n");
        }
    }
}


java代码,控制树莓派

package srv_cntl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class php_raspibarry {

	public static final String piip = "192.168.2.146";


	public static void main(String args[]) throws IOException {
		ServerSocket server = new ServerSocket(5678);
		Pilib rpi = new Pilib(piip,8000);
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true){
					rpi.updateStatus();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			   }
			}
		}).start();
		while (true) {
			Socket client = server.accept();
			PrintWriter printer = new PrintWriter(client.getOutputStream());
			BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
			String m = reader.readLine();
			if (m.trim().equals("1")) {
				System.out.println("start");
				rpi.startPi();
				printer.println("starting");
			} else {
				if(m.trim().equals("2"))
				{
					System.out.println("end");
				    rpi.stopPi();
				    printer.println("ending");
				}
				else{
					System.out.println("end");
					printer.println(rpi.getStatus());
				}
			}

			printer.flush();
			printer.close();
			printer.close();
			client.close();
			System.out.println("client leaving!\n");
		}
	}
}


文章来源: php和java的socket 通信

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:php和java的socket 通信