dstd2129 2015-08-08 18:36
浏览 72
已采纳

如何通过java服务器套接字将多个对象发送到php套接字?

I am trying to send multiple objects through a php socket from a java server. Although when i try to send 2 or more json objects to php client, the website just refuses to load. But when i send only one json object, it seems to work fine.

This is the code from my java server:

package org.articron.server;
import org.json.simple.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author Articron | Arno
 *         on 8-8-2015.
 */
public class TCPConnection implements Runnable {

    private Socket client;
    private PrintWriter writer;
    private BufferedReader reader;
    private JSONObject tosend;
    private JSONObject tosend2;

    public TCPConnection(Socket client, PrintWriter writer, BufferedReader reader) {
        this.client = client;
        this.writer = writer;
        this.reader = reader;
        tosend = new JSONObject();
        tosend2 = new JSONObject();
        tosend.put("scriptid","2");
        tosend.put("name","mathias@cochet.lol");

        tosend2.put("scriptid","1");
        tosend2.put("botname","arno.gerver@gmail.com");
    }

    @Override
    public void run() {
        try {
            tosend.writeJSONString(writer);
            tosend2.writeJSONString(writer);
            writer.write("END");
            writer.flush();
            writer.close();
            reader.close();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and this is what the php looks like:

function dataTrans()
{
    $recieved = array();
    /* Get the port for the WWW service. */
    $service_port = 5678;

    /* Get the IP address for the target host. */
    $address = gethostbyname('104.251.212.75');

    /* Create a TCP/IP socket. */
    $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
    if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
    } else {
       // echo "OK.
";
    }


    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) {
        echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
    } else {
       // echo "OK.
";
    }
        do{
        $obj = socket_read($socket, 1024);
            if($obj == 'END'){
                break;
            }
        array_push($recieved, json_decode($obj));
            }while(true);
    socket_close($socket);
    return $recieved;
}
  • 写回答

1条回答 默认 最新

  • dshmkgq558192365 2015-08-08 18:47
    关注

    I haven't tried this, but out of instinct I would suggest to send a JSONArray with the 2 (or more) JSONObjects inside, and in the PHP side of stuff, assume that you recieve an array, instead of many objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)