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.
This is a blog post from Php100, after looking at the socket programming a little bit of understanding, hereby record:Original Address "http://www.php100.com/html/webkaifa/PHP/PHP/2008/1218/970.html"Let's start with a simple example---a TCP service that receives the input string, processes and returns the string to the client. The following is the corresponding code:PHP Code://Set some basic variables$host = "192.168.1.99";$port =1234;//Set timeout ti
:
Package chat.chat;
Import Java.io.DataInputStream;
Import java.io.EOFException;
Import java.io.IOException;
Import java.net.BindException;
Import Java.net.ServerSocket;
Import Java.net.Socket; /** * Java uses socket and AWT components to simply implement the online chat function server can implement a client-side connection and continuously send messages to the server * but not multiple clients are connected at the same time, because i
peer name# Include # Include # Include # Include # Include # Include # Include # Include # Define sure do {If (errno! = 0) {fprintf (stderr, "line % d: % s/n" ,__ line _, strerror (errno); exit (1 );}} while (0)Int main (INT argc, char * argv []){Int S;Int C;// Int on = 1;Struct sockaddr_in ADDR;Int Len = sizeof (ADDR );Memset ( (ADDR), 0, Len );S = socket (af_inet, sock_stream, 0 );Sure;ADDR. sin_family =
the application layer, you do not have to understand the principle, but it is better to know it. Php APIs for socket are available on the Internet. And then use it.
2. socket server. php
View copy print?
// Create a server socket
$ Tcp = getprotobyname ("tcp ");
$ Socket = socket_create (AF_INET, SOCK_STREAM,
"data received so far."
Check for End-of-file tag. If It is not there, read
More data.
Content = Encoding.ASCII.GetString (
State.buffer,0,bytesread);
if (content). Lengthgt;0 amp;amp; content. EndsWith ("{
All of the data has been read from the
Client. Display it on the console.
Console.WriteLine ("Receive {0} bytes data from the client.") \ n Data: {1} ",
Content. Length, content);
Echo the data back to the client.
Send (Handler, "-Data confirmat
is a generic socket programming diagram .Establishment of the server:Gets the native name used to parse the IP address of the machine, then the IP address is successfully bound, then the server is listening, waiting for the client to connect. That is, the next step is the TCP/IP three handshake operation, the operation succeeds the server and the client can communicate. Here is the comment code:1 voidSocketconnect ()2 {3 Charserver_name[ -];4 wsad
, content $ buf \ n"; # actually get $ bs bytes
Close SOCK;
Execution result:Perl tcp_socket_cli.pl localhost 25Received 41 bytes, content 220 ESMTP Postfix-ExtMail 0.12-hzqbbc
TCP server Socket module, forking/accept ModelIntroduction: a multi-process TCP server implements the daytime function in the sample.
Copy codeThe Code is as follows :#! /Usr/bin/perl-w
# Tcp_socket_dt_srv.pl
Use strict;
Use
functionality, only function recv can be used for connection-oriented sockets, and function
//recvfrom does not have this restriction, which can be used to receive data
//parameters from connectionless sockets buf point to receive buffers
// Parameter len Specifies the size of the buffer
//parameter flags is the control option, the meaning is consistent with recv
//If the parameter from is Non-null, and the socket is not connection-oriented, then the
MEM (memory, is a function provided by ansic.Functions starting with B include:(1) void bzero (void * s, INTN): sets the first n Bytes of the memory specified by parameter S to 0, which is usually used to clear the socket address 0.(2) void bcopy (const void * SRC, void * DEST, INTN): copy the specified number of bytes from the memory area specified by the SRC parameter to the memory area specified by the
PHP Basic socket-based communication 1, prefaceA 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
in the buffer, so we need to implement a function that can read the specified byte Readn function, and the corresponding function, You also need an efficient readline function that reads a full line.In the socketio package, the ReadLine function is a more efficient function by previewing a chunk of data in the kernel to see if there is a ' \ n ' presence, or read to ' \ n ' If it exists, otherwise the bloc
Let's start with a simple example---a TCP service that receives the input string, processes and returns the string to the client. The following is the corresponding code:
PHP Code:
// 设置一些基本的变量$host="192.168.1.99";$port=1234;// 设置超时时间set_time_limit(0);// 创建一个Socket$socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not createsocket\n");//绑定
$ Welcome = "welcome to the php service/n"; // define a stringSocket_write ($ msg, $ welcome, strlen ($ welcome); // write a socketSocket_close ($ socket); // Close the socket */ $ Ip = "127.0.0.1"; // define an ip address$ Port = 1000; // define the port$ Socket = socket_create (af_inet, sock_stream, sol_tcp); // Crea
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.