Alibabacloud.com offers a wide variety of articles about socket operation on non socket, easily find your socket operation on non socket information here online.
1, the server is not fixed IP listening, only monitoring the host any IP address: (the client specifies the local IP address, the server does not need to modify any IP address information) bzero (adr_inet, sizeof (adr_inet)); adr_inet.sin_family = af_inet; ADR_INET.SIN_ADDR.S_ADDR = htonl (Inaddr_any); Adr_inet.sin_port = htons (port);
Note: The dual IP address of the host has not been processed, to be verified;
2, obtain the local IP address: (no TCP and UDP points)//Remove the local IP address
Socket network programming first, socket first
First, I would like to summarize some of the books required for job interviews: I suddenly felt very steadfast in my mind, "the beauty of programming", "The sword refers to offer", "Unix environment programming", "Unix network programming", and "POSIX multi-thread programming, I am no longer confused. I finally know what I can do after I graduated from the comp
, just give you a reference article
http://www.zend.com/pecl/tutorials/sockets.php
2. Use PHP Socket extension
Server-side code:
Client code:
To start the server by using the CLI method:
PHP server.php
Note here the Socket_read function:The optional type parameter is a named constant:Php_binary_read-Use the system recv () function. Security for reading binary data. (in php> "default = 4.1.0)Php_normal_read-read-stop in \
);if ($connection){Socket_write ($connection, "you have connected to the socket...\n\r");}?>
You should use your command prompt to run this example. The reason is that this will produce a server instead of a Web page. If you try to run the script using a Web browser, chances are that it will exceed the 30-second limit. You can use the following code to set an unlimited run time, but it is recommended that y
stream.
2. The shutdowninput () and shutdownoutput () Methods of socket are called successively. They only close the input stream and output stream, and are not equivalent to the close () method of socket. After the communication ends, you still need to call the close () method of the socket, because only this method can release the resources occupied by the
entry. When you call the socket function, the socket executor creates a socket, in effect "building a socket" means allocating storage space for a socket data structure. The socket executor manages the descriptor table for you. A
("accept error ");Continue;}Printf ("received a connection from % s \ n", inet_ntoa (remote_addr.sin_addr ));If (! Fork () {/* child process code segment */If (send (client_fd, "Hello, you are connected! \ N ", 26, 0) =-1)Perror ("send error! ");Close (client_fd );Exit (0 );}Close (client_fd );}}} The workflow of the server is as follows: first call the socket f
');
}
Else
{
DL ('ets ETS. so ');
}
}
?> If you do not know whether your socket is enabled, you can use the phpinfo () function to determine whether the socket is enabled. You can check the phpinfo information to check whether the socket is enabled. For example:
View the socket information of phpinfo () ◆ generate a
Http://blogold.chinaunix.net/u1/43206/showart_518836.htmlSocket INADDR_ANY listens to the 0.0.0.0 address. The socket only binds the port so that the route table decides which ip address to transfer.INADDR_ANY indicates the address 0.0.0.0. In fact, this address indicates an uncertain address, "all addresses", or "any address ".Generally, if you want to create a network server, you need to notify the server operating system at a specific address xxx.
The PHP socket function is about the same as the C-style socket functionSocket function:http://php.net/manual/en/book.sockets.phpServer-side:Set_time_limit(0);$server _socket= Socket_create (Af_inet, Sock_stream, sol_tcp);Socket_bind ($server _socket, ' 127.0.0.1 ', ' 1212 ');Socket_listen ($server _socket, 4);Socket_set_nonblock ($server _socket); Do{$msg _socket= Socket_accept ($server _socket);if($msg _s
open your socket, if you do not open, please edit your php.ini file, remove the following line of comments:Extension=php_sockets.dllIf you cannot remove the annotation, use the following code to load the extension library:if (!extension_loaded (' Sockets ')){if (Strtoupper (substr (Php_os, 3)) = = "WIN"){DL (' Php_sockets.dll ');}Else{DL (' sockets.so ');}}?>
If you don't know if your socket is open, yo
, 'localhost', 1337 );Socket_listen ($ socket );// Accept any incoming connections to the server$ Connection = socket_accept ($ socket );If ($ connection){Socket_write ($ connection, "you have connected to the socket.../n/R ");}?>
You should use your command prompt to run this example. The reason is that a server is ge
process, because the receive of fin means that the application process can no longer receive additional data on the corresponding connection;
After some time, the application process that receives the file terminator calls close to close its socket. This causes its TCP to send also a fin N;
This fin is received by the source send side TCP to confirm it.
So there is a fin and an ack in each dir
($ connection, "You have connected to the socket... \ n \ r ");}?>You should use your command prompt to run this example. The reason is that a server is generated, not a Web page. If you try to use a Web browser to run this script, it is likely to exceed 30 seconds. You can use the following code to set an unlimited running time, but we recommend that you use a command prompt to run it.Set_time_limit (0 );
, remove the comments before the following line:Extension = php_sockets.dllIf you cannot remove the annotation, use the following code to load the extension Library:If (! Extension_loaded ('buckets ')){If (strtoupper (substr (PHP_ OS, 3) = "WIN "){Dl ('php _ sockets. dll ');}Else{Dl ('ets ETS. so ');}}?>
If you do not know whether your socket is enabled, you can use the phpinfo () function to determine whether the
);
Socket_listen ($socket);
Accept any incoming connections to the server
$connection = Socket_accept ($socket);
if ($connection)
{
Socket_write ($connection, "you have connected to the socket...\n\r");
}
?>
You should use your command prompt to run this example. The reason is that this will prod
understand the principle, but it's better to know that. PHP API for sockets on the Web. It's OK to use it down.Second, socket server server.phpView copy print?
Establishing a server-side socket
$tcp = Getprotobyname ("TCP");
$socket = Socket_create (Af_inet, Sock_stream, $tcp);
Socket_bind ($socket, ' 1
(daddr ));Example: (programs in Linux ):// Customer Service Program: Remember that the customer service program does not require the BIND () function.Int main (){Int sockfd;Int Len;Struct sockaddr_un address;Int result;Char CH =;Sockfd = socket (af_unix, sock_stream, 0 );/* Set up the client interface above, using the af_unix Unix domain Protocol */Address. sun_family = af_unix;Strcpy (address. sun_path, "server_socket ");Len = sizeof (Address );/* T
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.