Socket and Curl

Source: Internet
Author: User
Tags curl php source code prototype definition
What the socket is. Can do. Look like. The general principle is what. I think it is best to give an example to understand.
Here, I still use Java to implement a socket server and client (you can also use any other language to support socket operation), and then use PHP as a client to request the socket. As shown in the following illustration, a service is opened on the server side using a socket, and the port is 8001, so I can set up multiple clients to connect. On the client side, I send a message to the socket, and the server, after receiving my message, will handle it according to the situation, return it to the client, and print all the messages received on the server side.


From Figure 1, we see that both the server side and the client are implemented in Java, and in the middle of Tuyi, we see that once I turn on the server, my service will be registered in the Windows Network Service with a port of 8001, in order to convince you that We can use the netstat command to print the local network connection of each port, in the printed list, you can see that the service has been registered. Once a client connects to this socket, the operating system automatically assigns a random port to the client to communicate with the server-side 8001 ports.
Since this service has been registered in the operating system, the service and your qq,ftp are actually a level, and you can use it to do too many things. To verify, we can use Telnet to connect to it.

Because the socket is open, transparent, once run, that is, any language that can operate the socket can access this open service, figure one is using Java to access the socket, we can also use PHP, even JS (HTML5 new features http:// dev.w3.org/html5/websockets/) to access this service.
Example seven: PHP side code is as follows:
<?php
$sock = Fsockopen ("192.168.0.2", 8001, $errno, $ERRSTR, 1);
if (! $sock)
{
echo "$errstr ($errno) <br/>\n";
}else{
Socket_set_blocking ($sock, false);
Fwrite ($sock, "senddata....\r\n");//Note: You need to add "\ r \ n" to submit this request data at the end of the data, or you may not be able to get the service-side response, even if the refresh buffer is not valid, so you will not get a response until the connection is closed
Fwrite ($sock, "end\r\n"); Terminate this client connection using the end command
while (!feof ($sock)) {
Echo fread ($sock, 128);
Flush ();
Ob_flush ();
Sleep (1);
}
Fclose ($sock);
}
After running, the results are as follows:

To get more details, you can use iris grab software.
Well, after seeing so many demos, I think you have a more intuitive understanding of the socket. Now let me sum up the socket is a god horse dongdong. Open Baidu Encyclopedia, you can see the following definition: "As a 4BDSUNIX process communication mechanism, take the latter meaning." Often referred to as "sockets," used to describe IP addresses and ports, is a handle to a communication chain. "Sockets are often referred to as sockets," the application typically sends a request to the network via a "socket" or answers a network request. Plainly speaking is a communication mechanism. It's similar to the banking, telecommunications, and telephone service departments of these parts. When you're on the phone, there will be a distribution of a person to answer your question, the Customer service department is equivalent to the server side of the socket, your side is the equivalent of the client, and you call the end, if someone is looking for and you talk to the one talking, is impossible, because you are in communication with him, Of course, the telephone switchboard of the customer service department will not be duplicated.
We can look at the prototype definition of a socket function:
Socket socket (int AF, int type, int protocol);
The first parameter specifies the protocol family for the communication protocol used by the application, and for the TCP/IP protocol family, the parameter is set to Af_inet, and for UNIX, local sockets can be established;
The second parameter specifies the type of socket to create, the stream socket type is sock_stream, and the datagram socket type is SOCK_DGRAM;
The third parameter specifies the communication protocol used by the application.
It is very difficult to go deep into the internal implementation of the socket, as a non-low-level programmer, as long as we can understand that the socket is a set of operating system encapsulated functions, will create a call on the OK. In PHP, it's easy to create a socket.
Example eight: The code is as follows:
<?php
$host = "192.168.0.2";
$port = 12345;
Set_time_limit (0);/best run in CLI mode
Create socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("could notcreate socket\n");
Bind socket to Port
$result = Socket_bind ($socket, $host, $port) or die ("could not bindto socket\n");
Start listening for connections
$result = Socket_listen ($socket, 3) or die ("could not set up socketlistener\n");
Accept Incoming Connections
Spawn another socket to handle communication
$spawn = socket_accept ($socket) or die ("could not accept incomingconnection\n");
Read Client input
$input = Socket_read ($spawn, 1024) or die ("could not readinput\n");
Clean up input string
$input = Trim ($input);
Reverse client input and send back
$output = Strrev ($input). "\ n";
Socket_write ($spawn, $output, strlen ($output)) or Die ("could notwrite output\n");
Close sockets
Socket_close ($spawn);
Socket_close ($socket);
This code has been commented in detail, and if you want to know more, you can visit http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/, This site is a very detailed explanation of the creation and use of sockets. Because PHP does not support multithreading, only suitable for clients, but not for the service side, performance is relatively low. So the General Service end is to use C/java and other languages to achieve, so about PHP to create a socket server I do not do too much introduction. (Incidentally: see someone ask PHP to implement multithreading problem, someone has been insisting that PHP can implement multiple threads, even said that they have written a multithreaded or analog to a multi-threaded. In fact, many people just think of multiple processes or bulk operations as multithreading, or the use of some PHP partners, Linux operating systems or Web servers to approximate implementation. What I'm trying to say is that threading is not something that some people think so easily. Thread scheduling, message passing between threads, hibernation and wake-up, thread pooling, synchronization, locks, priority concepts are not playing, if someone claims to be using PHP to implement a multithreaded, then I hope you can give me a full threading operation. Perhaps, I would consider myself to be ignorant. )
In addition, PHP's memory management is very, very bad. Because it replaces us with 99% of memory management, we simply do not realize the need for memory management, which is fatal in application-level services, so PHP can not do application and server-side development. Even if it claims to have a mechanism to prevent memory leaks, it is not, and do not assume that PHP does not have such problems as wild pointers. So tangled in PHP multithreaded PHP programmers are egg pain lang, unless you are proficient in C, ready to transform the PHP source code. This work has already been done, have to admire these top1% PHP master. The remaining 99% will be washed and slept. Later, don't in front of me to mention the multithreading of PHP and God Horse server development.
To manipulate the socket on the client side, you can use the Socket_create,stream_socket_client function in addition to the Fsockopen function. You can look at the manual in detail. If it is php5, I recommend using Stream_socket to implement the socket operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.