What should I do if I use the TCP/IP connection to open the network communication interface in the process sequence?

Source: Internet
Author: User
Tags htons

Recently I am studying Java Network programming. I think network programming is very interesting. So I am looking for some related information on the Internet. below is my understanding of the plug-in .,

After March 1990s, with the development of computing machines and network technologies, many
Data processing systems use open and release systems to build a network of customer machines/servers
Module, that is, the customer server initiates a request for any task and sends it to the server through the network,
The server handles the requested tasks, and then returns the result.
To the customer. For example, the Bank ATM's frontend host and the data processing master
Build a network model for the customer machine/server; the number of front machines and silver lines of the telephone bank
The data processing mechanism also forms the network topology. Why is this?
Exchange information between the host and the data master.
Is the basis for implementing this network model. However, the TCP/IP encapsulation technology is to solve this problem.
Powerful tools. From the time it is extracted, it is always flushed and getting heavier.
And has been used as a standard for TCP/IP network programming in UNIX operating systems.
Windows and Java both have their universal interfaces. With this powerful tool,
We can implement the interconnection between different machines and different types of operations in the process sequence.
And Communication.
ETS is the base for supporting TCP/IP protocol network communication.
Operating unit RMB. It can be viewed as a two-way process between different master machines.
The endpoint of the communication. It is a compilation process between a single master machine and the entire network.
Interface. In a general sense, the cross-Host application process must be implemented in the network environment.
Communication, each end of the network must have a set of connected words and two
The set of words can be established or connected without connection.
Perform the "read" and "write" operations on the intercept to implement the network communication capability. Similar to Unix
The I/O concept in the system, such as the style of opening, reading, writing, and closing the file.
The root data transmission data type is different, and the data set can be split into face-to-face connections by words
Stream sockets and datainsockets)
Two types.

1. Link character streams
The word stream is not recorded in a specific field, and the TCP protocol is discussed in the TCP/IP protocol cluster, that is
Transmition control protocol ). It is provided to the user.
The process can depend on the entire dual-work face-to-face connection agreement, a large number of Internet should use the process order
For example, use TCP protocol for FTP and telnet. Communication endpoint: use TCP to peer Internet addresses
Connected to each other, the data can be transmitted in a forward order and by a single and accessible address.
. Because it is a node stream, the packet length package is not limited, and the packet transmission is not repeated,
It is a commonly used concatenation class.

2. Data Reporting
Data reports report the type of data stream to be recorded, and negotiate with UDP in the TCP/IP protocol cluster,
User Data Reporting Protocol ). Use Data Reporting Services
Now we have some simple network services, such as network point inspection and test process Ping. From no established connections,
It is reported that the negotiation is faster than the negotiation. However, the data that cannot be guaranteed is accurate and orderly.
To the destination. Non-warranty is sequential, reliable, and non-repeatable. It is connectionless.
Server Load balancer uses stand-alone packages for transmission and transmission, and uses UDP to access the Internet
Address. The two sides do not need to connect to each other. They are transmitted based on a fixed maximum length.
Transmission of a single message or a small file.

Set the process of writing and Process

No matter what kind of character encoding process, all use the user machine/Server method, its operation
This category is similar to the topic. It is limited to the text frame, which only describes the character stream set. Node stream
Set by-word service progress and customer progress, you must create a set of their own character before communication
Establish a connection, and then perform "read" and "write" operations on the corresponding set of connected words.
Exchange of information.

The server process creates a set of connected words. The service process starts with the customer process.
In the first step, call the socket () function to create an end-to-end streaming and extract
Three parameters are provided: the address type of the network, which is like af_inef (adress Family Internet );
Set the character type, which uses sock_stream; network Network Coordination, lack of provinces for TCP/IP coordination, the corresponding
The parameter number is 0.

Assign the initial value to the number of changes to the set address. After generating a set of connected words, use the server address
First, assign the initial value to the sockaddr_in constructor. Sockaddr_in/usr/include/netinet/in. h
It is only applicable to the Internet address type, including the internet address type,
Information such as the IP port number and IP address. The address type can be set to af_inet.
Attackers can access inaddr_any by any means. The IP port number can be set by the user.
The primary node is switched to the network node in ascending order.

Set the name of the domain name. There is no name for the concatenation created by the socket () function. What is life
BIND () is used to bind the server address to the set of words.

Request for the connection from the customer's host when the server enters the quasi-standby mode. Call the listen () function first.
Number of incoming listener statuses of the server; then, the accept () function is called, and the quasi-standby mode is
The connection Letter Number of the client. When no connection request is received, the service process is blocked.

The customer calls the socket () function to create an end-to-end connection string.

Assign the value to the sockaddr_in constructor volume of the customer. The address type can still be af_inet, end
The port number is the same as the server port number of the server. To connect the server address, you can call it.
Inet_addr () can be converted. You can also convert the name to the specified value by using the gethostbyname () function.
Specify the volume of changes to the hostent, and then use bcopy ()
Returns the sockaddr_in structure variable.

The customer calls the connect () function to send a connection request to the service process.

After the connection request is obtained, the accpet () function of the blocked service process generates a new
And return to the sockaddr_in structure of the customer's machine.
The operator shall use the words in the process to connect the user with the new address assigned to the customer's host.
Then return the receiving number to the customer.

Once the character set of the customer machine is received as the receiving Number of the user server, the table shows the user
The server and server are connected. Either party can send or receive
Receive data from the peer. This can be realized by sending () and Recv. Or
Data can be exchanged through read () and write.

The service process and customer process can be switched over with shutdown () and colse ()
All sending and receiving operations, remove the pin, and disconnect.
All the system functions described above are included in the libsocket. A system library, ibm pc (UNIX)
During the compilation and translation of the operating system, the-llibsocket link must be added. Ibm rs/6000 (Aix) Operating System compilation and Translation
This parameter is not required.

The entire operation process can be shown in a chart as follows:

Programming example

In this article, we provide a byte stream concatenation function to implement real-time access on the TCP/IP network.
Example of user/server-side Process Communication, which is in the ibm rs/6000
Ibm pc 586 has passed the test. It mainly needs to simulate the process of querying the remaining amount of root accounts
Cheng, the customer machine is an e-phone bank's front machine (ibm pc), the server is the number of banks
Data processing master (ibm rs/6000), which includes three aspects:

The User Machine enters the read account number from the standard and sends the account number to the server.

After receiving the account number, the server determines whether data with this account number exists.
If it is returned to the customer.

The user's machine receives the returned message and sends the result to the bidding result.
The service process must start with the customer process. When the customer starts the process, it must carry
Server IP address, for example, clientpros 23.169.1.1

In the UNIX operating system, you can copy a hard disk with the doscp name:
CC hxserver. C-lsocket-0 Server
CC hxclient. C-lsocket-0 Client

In the AIX operating system, you can use the dosread command name to copy the data to the hard disk:
CC hxserver. C-0 Server
CC hxclient. C-0 Client

/* Server program: hxserver. C */
# Include

# Include

# Include

# Include

# Include "tcpipop. c"

Main ()
{
Int newsockfd, sockfd;
Int clilen;
Struct sockaddr_in cli_addr, serv_addr;
Char info [100];
Int infolen;
Int RC;

If (sockfd = socket (af_inet, sock_stream, 0) <0)/* Create a TCP byte stream */{fprintf (stderr, "socket failed !!
"); Return (-1);} bzero (char *) & serv_addr, sizeof (serv_addr);/* server address clearance 0 */serv_addr.sin_family =" af_inet; /* The network type uses ARPA "Internet address */serv_addr.sin_addr.s_addr =" htonl (inaddr_any); "/* the IP address is a recognized legal IP Address */serv_addr.sin_port =" htons (1234 ); "/* set the IP port number and convert the host byte sequence to the network byte sequence */If (BIND (sockfd, (struct sockaddr *) & serv_addr, sizeof (serv_addr )) <0)/* bind the server address to the created socket */{fprintf (stderr, "bind failed !!
"); Return (-1);} If (Listen (sockfd, 5) <0) /* set up a five-member listening queue to listen to connection requests from the socket */{fprintf (stderr, "Listen failed !!
"); Return (-1);} fprintf (stderr," blocked here .....
"); Clilen =" sizeof (cli_addr); "/* blocked connection requests to the client */newsockfd =" accept (sockfd, "(struct sockaddr *) & cli_addr, & clilen);/* If a connection request exists, a new socket is returned to the client address. The connection has been established */fprintf (stderr, "connected now !!!
"); If (newsockfd <0) {fprintf (stderr," Accept failed !!
"); Return (-1);} Shutdown (sockfd, 2); close (sockfd); memset (Info, 0, sizeof (Info); infolen =" 6; "If (rc =" tcpreceive (newsockfd, "info, infolen) =" = "1)/* accept information sent from the client */fprintf (stderr, "tcprecv failed !!
"); If (! Memcmp (Info, "123456", 6) {/* return the account information of the account 123456 */printf ("Your Ed account no is % 6.6 s
", Info); sprintf (Info," % 6.6s | % 8.8s | % 10.2lf ", info," Zhang Li ", 123.456);} else {/* otherwise, if 000000 is returned, this account does not exist */printf ("No such account no % 6.6 s
", Info); sprintf (Info," % 6.6s | % 8.8s | % lf "," 000000 "," ", 0.0);} infolen =" sizeof (Info ); "If (rc =" tcpsend (newsockfd, "info, infolen) =" = "1)/* Send this account information to the client */fprintf (stderr, "tcpsend failed !!
"); Shutdown (newsockfd, 2); close (newsockfd);}/* client program: hxclient. C */# include

# Include

# Include

# Include

# Include "tcpipop. c"

Typedef struct {
Char accno [7];
Char name [9];
Double AMT;
} Data_type;
/* Account data structure */

Main (argc, argv)
Int argc;
Char ** argv;
{
Int I = 0;
Int sockfd;
Struct sockaddr_in serv_addr;/* address of the storage server */
Char info [100];
Int infolen;
Data_type data;
Int RC;

If (argc! = 2)
{
Fprintf (stderr, "client IPaddress
");
Exit (-1 );
}
/* The parameter is the Server IP Address */

For (; I <10; I ++)/* Up to 10 connections */{bzero (char *) & serv_addr, sizeof (serv_addr )); /* clear the server address variable 0 */serv_addr.sin_family = "af_inet;"/* use the ARPA Internet address type */serv_addr.sin_addr.s_addr = "inet_addr (argv [1]); "/* The Server IP address is converted to a long integer IP address, and the address Structure Variable */serv_addr.sin_port =" htons (1234); "/* the same IP port number as the server, conversion from host bytes to network bytes */If (sockfd = "socket (af_inet," sock_stream, 0) <0) /* Create a TCP byte stream socket */{fprintf (stderr, "client: can not open stream socket
"); Shutdown (sockfd, 2); close (sockfd); Return (-1);} If (connect (sockfd, (struct sockaddr *) & serv_addr, sizeof (serv_addr)> = 0)
/* Establish a connection with the service process */
Break;
Shutdown (sockfd, 2 );
Close (sockfd );
}

If (I> = 10)
Exit (-1 );
/* At this time, the connection has been established. You can implement communication through reading/writing the socket */
Memset (& Data, 0, sizeof (data ));
Printf ("Enter Account No :");
Scanf ("% s", Data. accno);/* enter the account to search */

Infolen = sizeof (data. accno );
Bcopy (& Data. accno, info, infolen );
If (rc = tcpsend (sockfd, info, infolen) =-1)
Fprintf (stderr, "tcpsend failed !!
");
/* The customer process sends account information to the service process */
Infolen = sizeof (Info );
If (rc = tcpreceive (sockfd, info, infolen) =-1)

Fprintf (stderr, "tcprecv failed !!
");
/* Account information returned by the client process to receive the service process */

Sscanf (Info, "% [^ |] | % [^ |] | % lf", Data. accno, Data. name, & data. AMT);/* format conversion */

If (! Memcmp (data. accno, "000000", 6)/* determine whether to check */
Printf ("there is no such account in server database.
");
Else {/* Display account information */
Printf ("accno is % s
", Data. accno );
Printf ("name is % s
", Data. Name );
Printf ("amount is %-10.2lf
", Data. AMT );
}

Shutdown (sockfd, 2 );
Close (sockfd );
}

/* Public Program: tcpipop. C */
/*
* Using TCP/IP protocol.
*/

# Include

# Include

# Include

# Include

# Include

Extern int tcpsend (int fd, char * Buf, int Len)
{
Int I = 0;
Int send_rc;

For (; I <10; I ++) {send_rc = "write (" FD, Buf, Len); If (send_rc = "=" Len) break; sleep (1);} if (I> = 10)
Return (-1 );
Else
Return (send_rc );
}

Extern int tcpreceive (int fd, char * Buf, int Len)
{
Int I = 0;
Int receive_rc;

For (; I <10; I ++) {receive_rc = "Read (FD," Buf, Len); If (receive_rc = "=" Len) break; sleep (1);} if (I> = 10)
Return (-1 );
Else
Return (receive_rc );
}

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.