Ready to do the Java course design, an address book. Adopt C/S architecture. The client uses Java FX and Java, server side with PHP, socket communication.
Here's a talk about the socket for PHP:
Before, you have to talk about TCP/IP,UDP. With the increasing use of the Internet, it is believed that many people have heard about these protocols, so what are they?
1, what is TCP/IP, UDP?
TCP/IP (transmission Control protocol/internet Protocol) is a protocol/inter-network protocol, an industry-standard set of protocols designed for wide area networks (WANs). The TCP/IP protocol family includes transport layer, network layer, and link layer.
UDP (User data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a part of the TCP/IP protocol family.
Here's a visual representation of their relationship:
We don't have a socket here, so where is the socket? What's the use? Don't worry, let's introduce the socket to their relationship. Same.
Now understand their relationship. What exactly is the socket? What's the use?
2. What is socket? What's the use?
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.
Since the socket is a well-encapsulated set of interfaces, how do we use him to communicate? What is a specific process? We can detail the process of our telephone call. First of all, our phone must be in the listening state, right. If not, it would certainly not have received a call from someone else. When people call, the first is to dial first, the number is the target, but also the only. After dialing the phone will start to connect, after the connection can begin to talk, after the chat is over to hang up the phone so that others can get through this number. Socket is the same, the server must first establish a socket to listen to their own computer a port, when a client creates a socket to initiate a connection request to accept, so that the message can begin to transfer, after the transfer of the socket closed. Of course, the server side can also initiate a connection request to the client and then start the communication. This is different from Ajax, which can be communicated in two directions. Look at the following picture:
Start with the server side. The server-side initializes the socket, then binds to the port (BIND), listens to the port (listen), calls the Accept block, waits for the client to connect. At this point if a client initializes a socket and then connects to the server (connect), the client-server connection is established if the connection is successful. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, closes the connection, and ends the interaction at once.
3, the socket related functions:
Socket_accept () accepts a socket connection
Socket_bind () binds the socket to an IP address and port
Socket_clear_error () Clears the socket error or the last error code
Socket_close () Close a socket resource
Socket_connect () Start a socket connection
Socket_create_listen () Open a socket listener on the specified port
Socket_create_pair () produces a pair of undifferentiated sockets into an array
Socket_create () produces a socket equivalent to a data structure that produces a socket
Socket_get_option () Get socket options
Socket_getpeername () Gets the IP address of a remote similar host
Socket_getsockname () Gets the IP address of the local socket
Socket_iovec_add () Add a new vector to a scatter/aggregate array
Socket_iovec_alloc () This function creates a IOVEC data structure that can send the received read and write
Socket_iovec_delete () Delete an assigned Iovec
Socket_iovec_fetch () returns the data for the specified Iovec resource
Socket_iovec_free () releasing a Iovec resource
Socket_iovec_set () Sets the new data value of the Iovec
Socket_last_error () Gets the last error code for the current socket
Socket_listen () listens for all connections by the specified socket
Socket_read () reads the specified length of data
SOCKET_READV () reads data from a scatter/aggregate array
SOCKET_RECV () end data from socket to cache
Socket_recvfrom () accepts data from the specified socket, if not specified, the default current socket
Socket_recvmsg () receive messages from Iovec
Socket_select () multi-channel selection
Socket_send () This function sends the data to the connected socket
SOCKET_SENDMSG () Send message to socket
Socket_sendto () sends a message to the socket at the specified address
Socket_set_block () set as block mode in socket
Socket_set_nonblock () set to non-block mode in socket
Socket_set_option () Set socket options
Socket_shutdown () This function allows you to close a read, write, or specified socket
Socket_strerror () returns a detailed error for the specified error number
Socket_write () write data to the socket cache
Socket_writev () write data to a scatter/aggregate array
4. Socket Communication Demo:
Server-side:
= 5) {break; }; } socket_close ($msgsock);} while (true); Socket_close ($sock);? >
Client
";} while ($out = Socket_read ($socket, 8192)) {echo "Receives server backhaul information successfully! \ n "; echo "accepts the content as:", $out;} echo "Close socket...\n"; Socket_close ($socket); echo "Close ok\n";? >
Note: 1) The server side is running in CLI mode, which is the command line pattern. Do not use CGI (browser access); Locate the directory where the Php.exe is located, and then run the following code, PHP server files;
2) Run Netstat-ano to view port occupancy after running under another DOS window.
5. Package the socket into a class:
ServerSocket.class.php
Client socket Operation class
Server_host= $host; $this->server_port= $port; $this->createsocket (); }//Create a socket and use it to bind the listening port private Function Createsocket () {if ($this->create_socket = Socket_crea Te (Af_inet, Sock_stream, sol_tcp) ==false) {echo "Socket_create () failed. Reason: ". Socket_strerror (Socket_last_error ())." \ n "; } if (Socket_bind ($this->create_socket, $this->server_host, $this->server_port) ==false) {Ech O "Socket_bind () failed. Reason: ". Socket_strerror (Socket_last_error ($this->create_socket))." \ n "; } if (Socket_listen ($this->create_socket,5) ==false) {echo "Socket_listen () failed. Reason: ". Socket_strerror (Socket_last_error ($this->create_socket))." \ n "; }}//initiate connection to the target host public Function connectclient () {if (Socket_getpeername ($this->create_s Ocket, $this->client_host, $this->client_port) ==null) {echo "Socket_getPeerName () failed. Reason: ". Socket_strerror (Socket_last_error ($this->create_socket))." \ n "; } if (Socket_connect ($this->create_socket, $this->client_host, $this->client_port) ==false) {echo "Socket_connect () failed. Reason: ". Socket_strerror (Socket_last_error ($this->create_socket))." \ n "; }}//Accept connection get to a socket resource, want client to read and transmit information public function WR () {do{//loop Prevent blocking delay if ($this->a Ccept_socket=socket_accept ($this->create_socket) ==null) {echo "socket_accept () failed. Reason: ". Socket_strerror (Socket_last_error ($this->create_socket))." \ n "; Break } $this->get_data = Socket_read ($this->accept_socket, 8192); $this->send_data= $this->operatedata ($this->get_data); if (Socket_write ($this->accept_socket, $this->send_data, strlen ($this->send_data)) ==false) {echo "Socket_write () failed reason:" . Socket_strerror (Socket_last_error ($this->accept_socket)). " \ n "; } socket_close ($this->accept_socket); }while (TRUE); }//Data processing Private Function Operatedata () {return; }//Turn off the listener socket Private Function closesocket () {socket_close ($this->createsocket); }//destructor public Function __destruct () {$this->closesocket (); } }
clientsocket.class.php//Client Socke Operation class Socket {private $host;//host private $port connecting socket;//socket Port number Priva Te $error =array (); Private $socket =null;//socket connection identifies private $queryStr = "";//data sent public function __construct ($host, $port) {if (!extension_loaded ("Sockets")) {Exit ("Please open Socket extension"); } if (empty ($host)) exit ("Please enter Destination address"); if (empty ($port)) exit ("Please enter a valid port number"); $this->host= $host; $this->port= $port; $this->createsocket ();//Create Connection}//Create socket Private Function Createsocket () {! $this->socket&am p;& $this->socket=socket_create (af_inet, Sock_stream, sol_tcp);//create Socket $R = @socket_connect ($this Socket, $this->host, $this->port); if ($r) {return $r; }else{$this->error[]=socket_last_error ($this->socket); return false; }}//write data to the socket server and read public function WR ($contents) {$this->querYstr= ""; $this->querystr= $contents; ! $this->socket&& $this->createsocket (); $contents = $this->flitersenddata ($contents); $result =socket_write ($this->socket, $contents, strlen ($contents)); if (!intval ($result)) {$this->error[]=socket_last_error ($this->socket); return false; } $response =socket_read ($this->socket,12048); if (false=== $response) {$this->error[]=socket_last_error ($this->socket); return false; } return $response; }//Filter the data sent to the private function Flitersenddata ($contents) {//To process the written data return $contents; }//All error messages Public Function GetError () {return $this->error; }//Last error message Public Function GetLastError () {return $this->error (count ($this->error)); }//Gets the last message sent public function getlastmsg () {return $this->querystr; } Public Function GetHost () {return $this->host; } public Function Getport () {return $this->port; }//Close socket connection Private function close () {$this->socket&&socket_close ($this->socket);//close connection $this->socket=null;//Connection Resource Initialization} public function __destruct () {$this->close (); }}