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.
Write your own client programAlso is to achieve the function of the previous article, the client program is no longer using Telnet, their own code implementation, the server-side program needs to be modified under
Server programs
#include #include #include #include #include #include #include voidErrorChar*MSG) {fprintf(stderr,"%s:%s\n", MSG, strerror (errno));Exit(1);}intOpen_listener_socket (void){ints =
return immediately.In addition, in many cases, socket programming is implemented on the client and server based on different situations, and the application program is compiled on the client to send requests to the specified port on the server, at the same time, the preparation of the server application to process the request has been mentioned in the above description; of course, not all socket programmin
description H represents HOST,N representative networks representative Span class= "hljs-built_in" >short , L represents long Socket () function功能创建一个套接字#include int socket(intinttype, int protocol);参数说明domain:通信协议族,即地址族type:套接字类型protocol:通信协议常设置为0 ,由内核根据指定的类型和协议族使用默认的协议返回值成功时,返回一个大于等于0的文件描述符失败时,返回一个小于0的值Linux supported protocols and address families
*) addrclient, socklen); If (invalid_socket =
Wsagetlasterror ())
MessageBox (null, "Connect client failed! "," Error ", mb_ OK); // 5. Use the returned socket to communicate with the user, send data, or accept data char sendbuf [100]; wsprintf (sendbuf,
"Welcome % s to my computer ",
Inet_ntoa (addrclient. sin_addr); send (sockconn,
Sendbuf, sizeof (sendbuf) + 1, 0); If (socket_error =
Wsagetlasterror ())
MessageBox (null, "server send data failed
invalid of object referenced. Usually this occurs when a socket de script or to a socket that cannot support this operation be trying to accept a connection O n A datagram socket.Wsaepfnosupport(10046)Protocol family not supported.The protocol family has not been configured to the system or no implementation for it exists. This message is has a slightly differen
list of functions in this series, see http://www.php.net/manual/en/ref.sockets.php.I think this list is very rich? However, it is a pity that this module is still very young and has many immature places, and there are very few references :(I am also working on it, so I will not discuss it for the moment. I will only give you a reference article.
Http://www.zend.com/pecl/tutorials/sockets.php
2. use PHP socket extension
Server code:
Client code:
Sta
("server % d is listening... \ n", Port );
Sockaddr_in addrclient;
Int Len = sizeof (sockaddr );
Char Buf [4096]; // received data
Char rbuf [100] = "successful"; // returned data
While (1)
{
// Accept the connection
Sockconn = accept (socksrv, (sockaddr *) addrclient, Len );
Printf ("Accept connection from % s \ n", inet_ntoa (addrclient. sin_addr ));
// Receive data
Php simple socket server client code example
This article shares a simple socket example using php. Implement a TCP Service that receives the input string, processes and returns the string to the client.
Generate a socket server
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* File name: socket_server.php */ // Set
characteristic is called the client, the clients mainly connects the remote system and obtains the data;(another behavior in the socket is called server, the server uses the socket to receive the connection and provides the data, and the client is the opposite, so www.baidu.com is an HTTP server, the browser is an HTTP client)# _*_ Encoding:utf-8 _*_import socket
2 PHP Test filesserver.php
Copy Code code as follows:
Phpinfo ();
Ensure that the client is not timed out when connected
Set_time_limit (0);
$ip = ' 127.0.0.1 ';$port = 1935;
/*+-------------------------------* @socket the entire process of communication+-------------------------------* @socket_create* @socket_bind* @socket_listen* @socket_accept* @socket_read* @socket_write* @socket_close+--------------------------------*/
/*--
2 PHP Test filesserver.php
Copy Code code as follows:
Phpinfo ();
Ensure that the client is not timed out when connected
Set_time_limit (0);
$ip = ' 127.0.0.1 ';
$port = 1935;
/*
+-------------------------------
* @socket the entire process of communication
+-------------------------------
* @socket_create
* @socket_bind
* @socket_listen
* @socket_accept
* @socket_read
* @socket_write
* @socket_close
+-----------------------
Socket Programming Principle
1, the introduction of the problem
1) Normal I/O operation process :
The set of I/O commands for UNIX systems evolved from commands in Maltics and earlier systems, with a pattern of opening a read/write one close (open-write-read-close). When a user process makes an I/O operation, it first invokes "open" to obtain the right to use the specified file or device and returns an integer called the file descriptor to describe
address of '+ host + 'is' + remote_ip now we know the Ip address of the server, you can use the connect function to Connect to a specific port of the IP address. In the following example, connect to port 80 (the default port of the HTTP Service): # Connect to r Emote servers. connect (remote_ip, port) print 'socket Connected to '+ host + 'on ip' + remote_ip to run the program: $ python client. pySocket createdIp of remote host www.google.com is 173.1
complete, exiting the loop.
When we have finished receiving the data, call the Close () method to turn off the socket so that a complete network communication is over:
# Close Connection
s.close ()
The data received includes HTTP headers and the Web page itself, we only need to separate HTTP headers and Web pages, the HTTP header printed out, the content of the Web page saved to the file:
header,html = Data.split (' \r\
This article details the usage of socket in PHP and share it for everyone's reference. The specific usage is as follows:
First, open the socket
Phpinfo () to see if the socket extension is turned on or open in php.ini.
Second, the server-side code of the wording
Copy Code code as follows:
Error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_
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.