SOCKET protocol & socket Functions

Source: Internet
Author: User
Image Description of the SOCKET protocol
The original meaning of socket is "hole" or "socket ". Here we use the 4bds UNIX Process
Information mechanism, take the latter meaning. Socket is very similar to a telephone outlet. Take a national-level telephone network as an example. The two sides of a telephone call are equivalent to two processes that communicate with each other. The area code is the network address of the phone call.
Vswitches in different units
On a host, the local number assigned to each user by the host is equivalent to the socket number. Before a call, any user must possess a telephone, which is equivalent to applying for a socket.
The other party's number is equivalent to a fixed socket. Then a dial-up call is sent to the other party, which is equivalent to a connection request (if the other party is not in the same region, the other party's Zone code is also called, which is equivalent to giving a network location
Address ). If the other party is present and idle (equivalent to the communication of another host starting and accepting connection requests), pick up the phone microphone and both parties can formally call, equivalent to a successful connection. The communication process between the two parties is
The process of sending a signal to the telephone and receiving a signal from the other party is equivalent to sending data to the socket and receiving data from the socket. After the call ends, one party suspends the phone, which is equivalent to shutting down
Socket to cancel the connection.
In the telephone system, the user can only feel the existence of the local phone number and the other party's phone number, establish the call process, voice transmission
The process and technical details of the entire telephone system are transparent to him, which is also very similar to the socket mechanism. Socket uses the inter-network communication facilities to implement process communication, but it specifies the details of the communication facilities.
Don't care, as long as the communication facilities can provide sufficient communication capabilities, it will satisfy.
So far, we have made an intuitive description of socket. Abstract: Socket essentially provides the end of Process Communication.
Point. Before processes communicate with each other, both parties must first create an endpoint. Otherwise, there is no way to establish a connection and communicate with each other. Just as before making a call, both parties must have their own telephones. Within the network
Each socket uses a semi-correlation description:
(Protocol, local address, local port)
A complete socket has a unique local socket number, which is allocated by the operating system.
Most importantly, socket
Designed for the customer/server model Program Provides different Socket System calls. The customer randomly applies for a socket
(Equivalent to a caller who wants to make a call on any incoming phone number), the system allocates a socket number for it. The server has a globally recognized socket
Any customer can send a connection request and information request to it (equivalent to a called phone with a phone number known to the caller ).
The socket uses the Client/Server mode to cleverly solve the problem of establishing communication connections between processes. Server
The semi-correlation of socket is recognized as very important globally. Readers may consider how to establish communication between two completely random user processes? Assume that neither party has a socket
Fixed, just like the two sides of a call do not know each other's phone number, it is impossible to call each other.
-----
The socket interface is the most widely used method to access the Internet.
If you have a host with the TCP/IP protocol configured, the IP address is 202.120.127.201. At this time, execute FTP on another host or the same host.
202.120.127.201, it is clear that a connection cannot be established. Because the host "202.120.127.201" does not run FTP service software. Similarly,
Run browsing software on another host or on the same host
For example, if you enter "http: // 202.120.127.201" in Netscape, a connection cannot be established. Now, if you run an FTP service software on this host
Open a socket and bind it to port 21), and then run a web
Service software (the software will open another socket and bind it to port 80 ). In this way, execute the ftp
202.120.127.201, FTP client software will call the host through port 21
Establish a connection with the socket provided by the service software. When "http: // 202.120.127.201" is entered in Netscape, port 80 is used to call
Call the socket provided by web service software on the host, establish a connection with it, and talk to it.
There are many such hosts on the Internet. These Hosts generally run multiple service software and provide several services at the same time.
Each service opens a socket and binds it to a port. Different ports correspond to different services. Socket is like a porous socket, just as it was originally intended. A host is full
Each socket has a serial number in each socket room. Some sockets provide 220 v ac, some provide 110 v ac, and some provide cable TV programs.
The customer software inserts the plug into a socket with different numbers to obtain different services.
-----
1. What is socket?
The so-called socket is also called "socket". It is used to describe the IP address and port and is a communication chain handle. Applications usually send requests to or respond to network requests through "Sockets.
Taking J2SDK-1.3 as an example, the socket and serversocket class libraries are located in the java.net package. Serversocket is used on the server, socket
Is used to establish a network connection. When the connection is successful, a socket instance is generated at both ends of the application to complete the required session. For a network connection, sockets are equal
There is no difference, not because there are different levels on the server side or on the client side. Both socket and serversocket work through the socketimpl class.
And its subclass.
Important socket
API: java.net. Socket inherits from Java. Lang. Object and has eight constructors. There are not many methods. The following describes the three most frequently used methods and other methods.
You can see the JDK-1.3 documentation.
The accept method is used to generate "blocking" until a connection is received and a client's socket pair is returned.
Instance. "Blocking" is a term that enables the program to run temporarily "Stay" in this place until a session is generated and then the program continues. Generally, "blocking" is produced by a loop.
The getinputstream method obtains the network connection input and returns an inputstream object.
Instance.
The other end connected by the getoutputstream method will get the input and return
Outputstream object instance.
Note: Both the getinputstream and getoutputstream methods can generate an ioexception, which must be captured because the returned streams
Object, which is usually used by another stream object.
2. How to develop a server-client model program development principle:
Server, using serversocket to listen to the specified port, the port can be specified at Will (because the port below 1024
Generally, a port is a reserved port and cannot be used in some operating systems. Therefore, we recommend that you use a port greater than 1024.) The session is generated after the client connects to the client; after the session is completed
Close the connection.
The client uses Socket to send a connection request to a port of a server on the network. Once the connection is successful
Session. After the session is complete, close the socket. The client does not need to specify the opened port. Generally, a temporary and dynamic port of over 1024 is allocated.
A socket interface is a TCP/IP network API. A socket interface defines many functions or routines.
You can use them to develop TCP/IP network applications. To learn TCP/IP network programming on the internet, you must understand the socket interface.
The socket interface designer first places the interface in the UNIX operating system. If you understand the input and output of Unix systems, you can easily understand socket. Network
Socket data transmission is a special type of I/O, and socket is also a file descriptor. Socket also has a function that calls socket () similar to opening a file.
Return to an integer socket descriptor. Subsequent connection establishment and data transmission operations are implemented through this socket.

There are two common socket types: stream socket (sock_stream) and Datagram
Socket (sock_dgram ). Streaming is a connection-oriented socket for connection-oriented TCP Service applications; datagram socket is a connectionless
Socket, which corresponds to a connectionless UDP Service Application.
To establish a socket, the program can call the socket function, which returns a handle similar to a file descriptor. Socket function prototype: int
Socket (INT domain, int type, int
Protocol); domain indicates the protocol family used, usually pf_inet, indicating the Internet protocol family (TCP/IP protocol family); Type parameter specifies the socket
Type: sock_stream
Or sock_dgram, the socket interface also defines the original socket (sock_raw), allowing the program to use the low-layer protocol; Protocol is usually assigned a value of "0 ".
The socket () call returns an integer socket descriptor, which you can use later.
The socket descriptor is a pointer to the internal data structure and points to the descriptor table entry. When the socket function is called, the socket execution body will establish a socket.
Setting up a socket "means allocating storage space for a socket data structure.
The socket execution body manages the descriptor table for you. A network connection between two network programs includes five types of information: communication protocol, local Protocol address, local host port, remote host address, and remote protocol end.
Port. The socket data structure contains these five types of information. Socket is also widely used in measurement software.

 

Two socket Functions

The socket function creates a socket that
Is bound to a specific service provider.
Socket socket (
Int AF,
Int type,
Int Protocol
);
Parameters
Afaddress family specification.
Type
Type specification for the new socket.
The following are the only two type
Specifications supported for Windows Sockets 1.1: Type explanation
Sock_stream provides sequenced, reliable,
Two-way, connection-based byte streams with an OOB data transmission
Machism. uses TCP for the Internet address family.
Sock_dgram supports datagrams, which are
Connectionless, unreliable buffers of a fixed (typically small) Maximum
Length. uses UDP for the Internet address family.
In Windows Sockets 2, using new socket types
Will be introduced and no longer need to be specified, since
Application can dynamically discover the attributes of each available
Transport Protocol through the wsaenumprotocols function. Socket Type
Definitions appear in winsock2.h, which will be periodically updated
New socket types, address families, and Protocols are defined.
Protocol
Protocol to be used with the socket that is
Specific to the indicated address family.
Return values
If no error occurs, socket returns
Descriptor referencing the new socket. Otherwise, a value
Invalid_socket is returned, and a specific error code can be retrieved
By calling wsagetlasterror.
Error code meaning
Wsanotinitialised a successful wsastartup
Call must occur before using this function.
Wsaenetdown the network subsystem or
Associated service provider has failed.
Wsaeafnosupport the specified address family
Is not supported.
Wsaeinss a blocking Windows Sockets
1.1 call is in progress, or the service provider is still processing
Callback function.
Wsaemfile no more socket descriptors are
Available.
Wsaenobufs no buffer space is available.
Socket cannot be created.
Wsaeprotonosupport the specified protocol is
Not supported.
Wsaeprototype the specified protocol is
Wrong type for this socket.
Wsaesocktnosupport the specified socket type
Is not supported in this address family.
Remarks
The socket function causes a socket
Descriptor and any related resources to be allocated and bound to
Specific transport-service provider. Windows Sockets will utilize
First available service provider that supports the requested combination
Of address family, socket type and protocol parameters. The socket that
Is created will have the overlapped attribute as a default.
Microsoft operating systems, the Microsoft-specific socket option,
So_opentype, defined in mswsock. h can affect this default. See
Microsoft-specific documentation for a detailed description
So_opentype.
Sockets without the overlapped attribute can
Be created by using wsasocket. All functions that allow overlapped
Operation (wsasend, wsarecv, wsasendto, wsarecvfrom, and wsaioctl) also
Support nonoverlapped usage on an overlapped socket if the values
Parameters related to overlapped operation are null.
When selecting a protocol and its supporting
Service provider this procedure will only choose a base protocol or
Protocol chain, not a protocol layer by itself. unchained Protocol
Layers are not considered to have partial matches on type or AF either.
That is, they do not lead to an error code of wsaeafnosupport or
Wsaeprotonosupport if no suitable protocol is found.
Important the manifest constant af_unspec
Continues to be defined in the header file but its use is stronugly
Discouraged, as this can cause ambiguity in interpreting the value
The Protocol parameter.
Connection-oriented sockets such
Sock_stream provide full-duplex connections, and must be in a connected
State before any data can be sent or already ed on it. A connection
Another socket is created with a Connect call. Once connected, data can
Be transferred using send and Recv CILS. When a session has been
Completed, a closesocket must be completed MED.
The communications protocols used
Implement a reliable, connection-oriented socket ensure that data is not
Lost or duplicated. If data for which the Peer protocol has Buffer
Space cannot be successfully transmitted within a reasonable length
Time, the connection is considered broken and subsequent callwill fail
With the error code set to wsaetimedout.
Connectionless, message-oriented sockets
Allow sending and processing ing of parallel Rams to and from arbitrary peers
Using sendto and recvfrom. If such a socket is connected to a specific
Peer, datagrams can be sent to that peer using send and can be attached ed
Only from this peer using Recv.
Support for sockets with type sock_raw is
Not required, but service providers are encouraged to support raw
Sockets as practicable.
Notes for IrDA sockets
The af_irda.h header file must be explicitly
Encoded ded.
Only sock_stream is supported;
Sock_dgram type is not supported by IRDA.
The Protocol parameter is always set to 0
For IRDA.
Note on Windows NT, raw socket support
Requires administrative privileges.
Requirements
Windows NT/2000/XP: supported ded in Windows NT
3.1 and later.
Windows 95/98/me: supported ded in Windows 95 and
Later.
Header: declared in winsock2.h.
Library: Use ws2_32.lib.
See also
Windows Sockets programming considerations
Overview, socket functions, accept, bind, connect, getsockname,
Getsockopt, ioctlsocket, listen, Recv, recvfrom, select, send, sendto,
Setsockopt, shutdown, wsasocke

 

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.