php-socket

php sockets read json array from java server

£可爱£侵袭症+ 提交于 2020-01-07 06:34:06
问题 I am connecting to a server written in JAVA using TCP/IP. My application sends json arrays to this server and in some cases also expects some results, json arrays. The problem is that i can easily send json via tcp but when reading it the script freezes waiting forever until it timeouts. Here is my code. $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket or die("error: could not create socket\n"); $succ = socket_connect($sock, Application_Model_Config::serverHost,

Send as hex in PHP

我们两清 提交于 2020-01-04 05:19:49
问题 I am struggeling a little to send a hex value to a device connected to my PHP socket server. I have this code: <?PHP # 7e hex = 126 decimal $str1 = "\x7e\x00MyData"; sendToDevice($str1); // works :-) # will send "~<NUL>MyData" and the device sends expected result back $dec = 126; $hex = dechex($dec); $str2 = $hex . "\x00MyData"; sendToDevice($str2); // does not work :-/ # will send "7eMyData" and the device sends nothing back $dec = 126; $hex = dechex($dec); $str3 = "\x$hex\x00MyData";

HTTP pipelining request text example

落花浮王杯 提交于 2019-12-29 04:19:18
问题 Below is an example HTTP 1.1 call with a single page requested : GET /jq.js HTTP/1.1 Host: 127.0.0.1 Accept: */* I understand with HTTP Pipelining, multiple requests can be sent without breaking the connection. Can someone post, some text example of how this request will be sent to the server, I want to be able to do it over the command line or with PHP sockets. Does support for pipelining need to enabled on the web-server as well? Is pipelining supported by major Web-servers(apache, nginx)

Sending sockets data with a leading length value

自作多情 提交于 2019-12-22 09:02:32
问题 I want to send JSON messages from a PHP script to a C# app over a network connection using PHP Sockets. Usually, for binary protocols, the first 4 bytes of every message must be an integer which represents the length (how many bytes) of the message. In C# I prefix every message with an integer that tells the length of the message as follow: byte[] msgBytes = UTF8Encoding.UTF8.GetBytes("A JSON msg"); byte[] prefixBytes = BitConverter.GetBytes(msgBytes.Length); byte[] msgToSend = new byte

php stream_get_contents hangs at the end of the stream

时间秒杀一切 提交于 2019-12-19 17:39:41
问题 Solution at the end of the question I am writing a PHP application that sends a message to a server and then reads a response back in using stream_get_contents . I communicate with the same server in an android app in the same way. The android app works fine and responds quickly, however the PHP hangs when reading the response back from the server. In the code sample below, I have set a tiny buffer size of 5 bytes to test a theory. If I remove this buffer size it hangs, however with the 5

Process socket data with a leading length value

孤街醉人 提交于 2019-12-12 12:12:54
问题 This is a follow-up question about how to process prefixed messages received from a network socket. What I try to do is: Read the first 4 bytes (which is the $prefix and represents the length of the message) Check if $prefix has really the size of 4 bytes and if it's an integer Read the complete $message using the length from $prefix Check if message has really the size of $prefix bytes So far I've the following two lines of code: $prefix = socket_read($socket, 4, PHP_BINARY_READ); //No 1. /

PHP Sockets with SSL / TLS - no suitable shared cipher could be used

半世苍凉 提交于 2019-12-07 16:59:16
问题 I am attempting to create a server and client using PHP Sockets with SSL / TLS. However, when sending data to the server, I receive the following error: PHP Warning: stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no suitable shared cipher could be used. This could be because the server is missing an SSL certificate (local_cert context option) in server.php on line 32 I've looked at other examples and tried various changes to no avail. Any help could be much appreciated. client.php <?php

Unable to connect to a non-blocking socket

筅森魡賤 提交于 2019-12-07 16:44:31
问题 This makes me nuts. I have to create a very simple non-blocking socket script in php 5.3 where a client connects to a server, both using non-blocking sockets. I've tried phpsocketdaemon and the example from the php manual, but in both cases when I try to connect to the server I get the following error: socket_connect() [function.socket-connect]: unable to connect [10035]: A non-blocking socket operation could not be completed immediately My client script where the error happens: $service_port

Unable to connect to a non-blocking socket

耗尽温柔 提交于 2019-12-06 01:03:27
This makes me nuts. I have to create a very simple non-blocking socket script in php 5.3 where a client connects to a server, both using non-blocking sockets. I've tried phpsocketdaemon and the example from the php manual , but in both cases when I try to connect to the server I get the following error: socket_connect() [function.socket-connect]: unable to connect [10035]: A non-blocking socket operation could not be completed immediately My client script where the error happens: $service_port = 2002; $address = '127.0.0.1'; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set

PHP Sockets with SSL / TLS - no suitable shared cipher could be used

送分小仙女□ 提交于 2019-12-05 22:55:18
I am attempting to create a server and client using PHP Sockets with SSL / TLS. However, when sending data to the server, I receive the following error: PHP Warning: stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no suitable shared cipher could be used. This could be because the server is missing an SSL certificate (local_cert context option) in server.php on line 32 I've looked at other examples and tried various changes to no avail. Any help could be much appreciated. client.php <?php $host = '192.168.10.10'; $port = 8080; $timeout = 30; $cert = 'assets/cert.pem'; $context = stream_context