Simple Application of socket programming

Source: Internet
Author: User

Simple Application of socket programming

In the previous article, the implementation process of socket programming is described in detail. At the end, we can send a string on the client to receive it on the server. Well, it seems interesting. But this is far from enough.

We need to learn a new technology, instead of implementing it, but to solve the problem at hand. The well-known socket not only sends a string, but also receives it. Well, this younger brother is here to make an axe.

Since the socket technology allows two different processes to communicate with each other, the two processes can be on different computers, but they only need correct physical connections, well, the problem is: can client programs obtain some server host information with the help of server programs? Oh, it seems like a Trojan !!! Haha. This question sounds attractive, but we think it is naive to think about it. Since messages can be sent to each other, the server program can send any information back to the client as the message content.

Well, let socket solve the following two problems:
1. Obtain the file list and directory list under a directory on the server host. The directory path must be sent by the client;
2. Obtain the content of a file on the server host.

The above issues are representative. If the problem succeeds, the entire file system of the server host will be displayed to the client.

From the two issues to be resolved, we can easily see that:
1. the requested service type is different. The column directory or file list, or the file content is obtained;
2. A service type has specific parameters. For example, a column directory list requires a parent directory path and a file path is required for obtaining the file content.

Oh, it seems that the message to be sent or received is not as simple as we thought. We need to encapsulate the "message" to solve the problem that we can only send and receive strings, after some thinking or design, I encapsulate the message into the following structure:

// Message
// +-Protocol Version 1 byte
// +-Message Type 2 bytes
// +-Message length 2 bytes
// ()
// +-Patameter type 2 bytes
// +-Patameter tag 1 byte
// +-Patameter index 2 bytes
// +-Patameter Data Length 2 bytes
// +-Patameter data VaR

A brief description
Protocol version is reserved for later version upgrades. Different values indicate different interpretations of messages;
Message Type indicates the type of request. The values of the column directory and the obtained file content are different;
Message Length indicates the total length of the parameter;
For () indicates that multiple parameters can exist;
Patameter type indicates the parameter type, such as the directory path or file path;
Patameter tag and patameter index. If the information data cannot be stored in a message. It will be divided into several messages, the former indicates whether it is the last message, and the latter indicates the first message. For example, if the content of a file is divided into two messages, the patameter tag of the first message is 0, indicating that the patameter index is 0 instead of the last message, indicates the first message. The patameter tag of the second message is 1, indicating the last message. The patameter index is 1, indicating the second message, this will be parsed in the client program;
Patameter Data Length indicates the length of patameter data;
Patameter data indicates the specific data of the parameter.

The preceding bytes are of the unsigned char type. It is not hard to figure out that the maximum length of a message is 65535, which is large enough. In the code, I specify the maximum length of a message as 512. It doesn't matter. It is worth mentioning that, the length of most messages is far from reaching this length. Do not send messages when sending them, which wastes resources. This is mainly done in the program. Here, we can only say this: no matter how long the message is, only send its valid data.

For a new generation of programmers, there is not much contact with the displacement operation, and the above data structure is involved. I will post some code here as an example:

/**
* Returns the type of this message object.
*/
Int sockmsg: getmessagetype (void) const {
Return (_ DATA [1] <8) | _ DATA [2];
}

/**
* Sets the type of this message object.
*/
Void sockmsg: setmessagetype (const Int & type ){
_ DATA [1] = type> 8;
_ DATA [2] = type;
}

The above are two operation functions for the message type.

Well, we can construct a message object and replace the original sent string with the message data.

On the client, we will send the message in the following ways:

Declare a sockmsg object
While (1 ){
Get user operations

If (column file list ){
Obtains the path specified by the user.

Set message type to -- column File
Set message Parameters
}
Else if (Get File Content ){
Obtains the specified file name.

Set message type to -- Get File Content
Set message Parameters
}
Else if (disconnect ){
Break;
}
Else {
Continue;
}

Send message
Receive messages
}
Closesocket ();

On the server side, you need to process the messages sent by the client as follows:

While (1 ){
If (Message accepted successfully ){
If (get message type: -- close connection ){
Break;
}
Else if (get the message type as -- column file ){
Get parameter value
Get file list
Add the file list to a message as a parameter.
}
Else if (get message type is -- Get File Content ){
Get parameter value
Get File Content
Add the file content to a message in the form of parameters. If one cannot be placed, separate multiple
}
Send message

}
}

Oh, well, the above is the key logic processing. But this is already a framework. You can improve it on the basis of the existing one, but there is a lot of work to do.

I do not like to post Code in articles, and write logic processing is clearly displayed. However, in actual programming, there are many problems that need to be addressed to obtain the file list in a directory, or reading the content of a file is not covered in this article. You also have a self-built implementation method, which will be pasted out a lot and makes little sense. I have always thought that programming is actually a kind of thought. Just write your own thoughts and others will adopt them if they think they are good. If you want to see my source code, you can also view it on this blog.

Well, I will be here. The application of socket technology is just the tip of the iceberg. As long as you are used to thinking, practicing, and waiting for you, remember: the world is just a bit difficult!

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.