Simple Winsock Application Design (2)

xiaoxiao2021-03-05  23

Lin Jun

In the previous article, the author introduces you how to build a master from the Winsock environment.

Connection and Close of the TCP Socket of the Architecture (Client / Server); Today, the author will continue to be everyone

Describe how to use TCP Socket to send information, and explain in detail in the WSaasyncselect library.

FD_READ and FD_WRITE events (the author found that there are quite a number of people can't get these two events.

solution).

I believe that readers already know that the connection of TCP Socket is in the CLIENT telephone call.

After the work, and the server is called the Accept card, it is completely established; after the connection is established,

Client and Server can also use this connection successful socket to transfer information to each other, or

The information sent by the other party is charged.

(Figure 1. TCP Socket data delivery)

Before the introduction of the information, the author first introduces the TCP Socket and UDP Socket in the delivery capital.

Features of the material:

Stream (TCP) Socket provides "two-way", "reliable", "order", "no heavy remover"

Data transfer.

DataGram (UDP) Socket provides "two-way" communication, but there is no "reliable", "once

Order, "no heavy remover", etc., so the user may receive no sequence, recovering information, even

The information may also be omission during transmission.

Since the UDP Socket is transmitted, it does not guarantee the information to be delivered to the other party, so I

Some applications commonly used (such as Telnet, Mail, FTP, News ..., etc.) are TCP

Socket to ensure the correctness of the data. (TCP and UDP packet transfer agreements are not we discuss

Readers who want to know, please refer to the relevant books.

TCP and UDP socket are both bidirection, so we use the same socket to do delivery and

Take the action of the information; the general TCP Socket's data is sent, and it is called Send () and RECV ().

The one is reached, and the UDP socket is two functions in Sendto () and Recvfrom (). but

TCP Socket can also be used in Sendto () and Recvfrom (), and UDP socket also available Send () and

RECV () in a letter; this point we will explain later.

Now let's take a look at the letter instructions for Send () and RECV () and go back to our previous process.

formula.

◎ Send (): Use the connect-type Socket to transfer data.

Format: Int Pascal Far Send (Socket S, Const Char Far * BUF,

INT LEN, INT FLAGS;

Parameters: s socket identification code

BUF stores the provisional area for information to be transferred

Length of Len Buf

Flags This card is called

Passage value: success - the length of the delivery

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Note: This function is suitable for connecting DataGram or Stream sockets to transfer information. Correct

DataGram Socket, if the size of DataGram exceeds the limit, will not be sent out, and

Will pass the error value. For the Stream Socket, in the Blocking mode, if the storage space in the Transport system is not stored in the transfer, Send () will be block.

Live until the data is delivered; if the socket is set to the non-blocking mode, the opinion is

How much data is sent out before the Output buffer space is sent, and it will not be lived by Block. Users must also

Note that the Send () is executed, does not mean that the information has been successfully sent to each other, but has already put it.

In the Output Buffer of the system, you wait. Flags value can be set to 0 or MSG_DONTROUTE

And combination of MSG_oob. (See Winsock No. 1.1 48)

◎ RECV (): Receive information from Socket.

Format: Int Pascal Far Recv (Socket S, CHAR FAR * BUF, INT LEN, INT FLAGS;

Parameters: s socket identification code

BUF storage regions received

Length of Len Buf

Flags This card is called

Passage value: success - received the length of the data (0) If the other Socket is closed, it is 0)

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Note: This file receives information with DataGram Socket or Stream Socket from the connection.

For Stream Socket, we can receive information in Input Buffer, but the quantity

Do not exceed the size of the LEN. If this Socket sets SO_OOBINLINE, there is Out-of-Band's capital

The material is not read, then only the data of Out-of-Band is taken out. For DataGram Socket, only take

The first DataGram; if the DataGram is larger than the storage space provided by the user, then only the empty

The size of the size is taken out, and how much data will be lost, and the wrong message is represented. Also if Socket

For the blocking mode, and there is no information in Input Buffer, RECV () will block to

What information is reached; if it is a non-blocking mode, and the input buffer has no information,

Looped a reply error. The value of the parameter Flags can be a combination of 0 or MSG_PEEK, MSG_OOB;

The MSG_Peek represents the buffer provided by the data to the user, but the information is not from the system's Input.

Drive in the buffer; 0 indicates that the copy is removed and removed. (Refer to Winsock 1.1 version 41)

[SERVER data delivery and closing socket?

In the previous period, we said that it is a SERVER in an Asynchronous mode;

We have called a wsaasyncselect () in the SOCKET () in Listen_SD, and set

FD_ACCEPT event, so when the client is connected to us, the system will pass it.

Async_event message (see the previous articles content); we have received the message and judged

FD_ACCEPT event, then call accept () to establish a connection.

MY_SD = Accept (Listen_SD, (Struct Sockaddr Far *) & SA, & SA_LEN) We have completed the Accept () in the call, successfully established the connection of the Server side and the Client side,

The new Socket (My_SD) can be used to send information. Because we also want to use

Asynchronous's way, so we must use the wsaasyncselect () to help build

Socket sets some events so that Winsock Stack can actively inform us when the event occurs. Due to I

Our Server is a passive acceptance of the client, then do reply, so we set up

FD_READ event; we also hope that Winsock Stack can be the Lord when you know Client Close Socket.

Inform us, so simultaneously set the fd_close event. (Readers must pay attention, we set up events

The Socket number is the new Socket number that is sent back after calling accept, not the original listening state.

Socket number)

WSaasyncselect (my_sd, hwnd, async_event, fd_read | fd_close)

Here, we also use hwnd this window and async_event this message;

In the forebel, the author told you that we can use it when you receive an Async_EVENT message.

WSAGETSELECTEVENT (LPARAM) to determine which event (fd_read or

Fd_close happens; so it is not confusing. Then when will we receive it?

FD_READ or FD_CLOSE event?

[FD_READ Event]

We will receive the FD_READ event to notify us to read the information:

(1) When calling the WSAAsyncselect library to set the fd_read event on this socket,

Information in Input Buffer.

(2) Input buffer in the original system is empty, and when the system receives the information, it will notify us.

(3) User calls the RECV or Recvfrom function, read the information from the Input Buffer, but

Nothing once to read light, then drive an FD_READ event again, indicating that there is still information

Input buffer.

Readers must pay attention: If we receive a message notified by fd_read event, we deliberately

If you don't call RECV or Recvfrom to read the information, when the system receives the information, it will not be again.

Inform us, be sure to wait for us to call the RECV or Recvfrom, it may be received again.

FD_READ event notification.

[FD_CLOSE Event]

When the system knows that the other party has closed Socket (received the FIN notification, and each other

Do the Hand-Shaking that is closed, we will receive the fd_close event notification so that I can

They can also close this opposite Socket. The fd_close event will only happen to TCP Socket, because

For it is connection-oriented; UDP Socket for Connectionless, even if set

FD_CLOSE does not work.

In the program, when the client is sent to a request, the system will notify us of the HWND window in an Async_EVENT message; we are using

WsagetSelectEvent (LPARAM) and WsagetSelectError (LPARAM) know

After the fd_read event and check are correct, the credign () is called the information sent by the Client.

RECV (WPARAM, & DATA, SIZEOF (DATA), 0)

The author also mentioned that the fd_xxxx incident occurred in the previous article, when receiving the message,

The parameter wparam when the window Handle is called is the Socket number that occurs.

The value of WPARAM is the MY_SD this Socket number mentioned earlier. Fourth Part 4 of RECV ()

The number is set to 0, indicating that we have to read the information from the system's Input Buffer and remove it.

After receiving the request, we have to reply the client, which is to send information to client; then we will

To use the Send ().

Let's put your information in the data temporary area of ​​Data, then call send () to send it, we

It is also a WPARAM (my_sd) this same Socket to do the transmission action because it is two directions.

of.

Send (WPARAM, & DATA, STRLEN (DATA), 0)

Server and Client send information after a period of time (all the information is delivered), if the client end

First call CloseSocket () Close the socket on the end, then the system will notify us

A message from an FD_Close event, and we can also call usSocket ().

Socket is closed; of course, we can also call CloseSocket () to take the initiative to close our end.

Socket.

[CLIENT data delivery and closing socket]

The client of our example is a blocking mode, so in the call connect () Verifier and Server

When connecting, it may be equal to the success; the connection is returned, and there is no error,

The Client's TCP Socket connection is successful. At this time, we can use this connection.

The successful Socket is sent to send information. Since we don't have to set it as ASYNCHRONOUS mode,

So don't call WSAAsyncSelect () to set events.

The client terminal is usually the first request to send request to the Server terminal, so we call send () to transfer

This information. Our data is small, so it will not be lived by the send () letter block;

The amount of information you want to send is large, then you will wait for a while to return from the Send () letter;

It is said that it must be returned after the data is placed in the system's Output Buffer; this is because we client

Socket is blocking mode. If we use the Socket with non-blocking mode, then send ()

How much is the space of the Output Buffer of the system, only some of the data is copied to Output Buffer,

After returning, and tell the user how much information is sent, do not have to put all the information to Output

Buffer is returned.

We will request to place in the Data data temporary area, then call Send () will be sent. Data delivery

After we call RECV () to wait for the reply of the Server.

Send (MySD, DATA, STRLEN (DATA), 0)

Recv (MySD, & Data, Sizeof (Data), 0)

Since our client end is a Blocking mode, RECV () will always be block, until

One of the cases of the column will occur before it will be returned.

(1) SERVER is sent. (At this point return is the length of the data read)

(2) The Server is turned off against the opposite Socket. (At this point returnome is 0)

(3) The Client side calls WSAcAncelBlockingCall () to cancel the call of the RECV ().

(At this point the return is a socket_error error, error code 10004 WSAEINTR)

Similarly, after all the information is finished, we also call CloseSocket () to close the socket.

close.

◎ WSACANCELBLOCKINGCALL (): Cancel the blocking action currently in progress.

Format: Int Pascal Far WSAcancelBlockingCall (Void);

Parameter: None

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Note: This file is used to cancel the BLOCKING action in this app. Usually

Using the timing is: (a) blocking action is in progress, the application receives a message

(Mouse, Keyboard, Timer, etc.), call this file in the paragraph of the message. (b)

The blocking action is in progress, and the Windows Sockets calls back to the program?

When the "Blocking Hook" is in this function, this function can be called to cancel the blocking action.

Users must pay attention to the action of a Winsock Blocking function, except

WSAISBLOCKING () and WSACANCELBLOCKINGCALL () can no longer call any other

The WINDOWS Sockets DLL provides a function, otherwise an error will occur. In addition to canceling

The blocking action is not accept () or select (), then the socket may be undecided.

Status, the user is best to call CloseSocket () to close the socket, not to do it again.

What moves.

(Figure 2.) Demoserv and DemoCLNT to send information on Winking on Winking

(Figure 3.) Demoserv and DemoCLNT on the Schedule on the Socket on Winking Winking

Introducing the information sent to TCP Socket, the author introduces Sendto () and

RECVFROM () These two functions, as well as many people may easily make the wrong fd_write event.

[Sendto and Recvfrom Version]

In general, TCP Socket uses two functions of Send () and RECV (); and UDP Socket

Used in Sendto () and RECVFROM (). This is because TCP is Connection-Oriented, must

After you have finished the true connection program of the SOCKET, you can start sending information. At this time, the system already knows the connection to the other party, so we don't have to specify where the information is to be sent. And UDP is ConnectionLess,

Both sides of the delivery information have not established a real connection, so we need to use Sendto () and Recvfrom ()

To specify the other party and those who know who is to send us?

TCP Socket can also be sent to send information with Sendto () and Recvfrom (), just at this time

The last two parameters of the function have no effect, it will be ignored by the system. UDP socket if it is called

Connect () in a function to specify the location of the other party (this connection does not really connect the other party

To tell us that our system says that we only want to collect, to what information is sent), then you can benefit

Use Send () and RECV () to send information.

◎ Sendto (): Send data to the destination designated by the user.

Format: Int Pascal Far Sendto (Socket S, Const Char Far * BUF,

INT LEN, INT FLAGS, Const struct sockaddr far * to, int

TOLEN);

Parameters: s socket identification code

BUF stores the provisional area for information to be transferred

Length of Len Buf

Flags This card is called

Address to be delivered to TO

TOLEN TO size

Passage value: success - the length of the delivery

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description: This function is suitable for DataGram or Stream Socket to transmit information to specified

Address. For DataGram Socket, if the size of DataGram exceeds the limit, it will not

Send any information and pass back the error value. For Stream Socket, its role is combined with Send ()

The same; the value of parameters to and Tolen will be ignored by the system. If it is the transfer (TRANSPORT)

Save space is not enough to store information to be transmitted, Sendto () will be held by Block until the information is

Send; unless the Socket is set to the non-blocking mode. Users must also pay attention to sendto ()

The function is completed, does not indicate that the information has been successfully sent to each other, but may still be in the system Output

Buffer. The value of Flags can be set to 0, MSG_DONTROUTE and MSG_OOB.

(See Winsock No. 1.1 Edition 51)

◎ Recvfrom (): Read the information and store the location of the data source.

Format: Int Pascal Far Recvfrom (Socket S, Char Far * BUF, INT LEN, INT FLAGS,

Struct socketaddr far * from, int far * fromlen;

Parameters: s socket identification code

BUF storage regions received

Length of Len Buf

Flags This card is called

Address of from FROM

Fromlen fromLomlen from

Passage value: success - received the length of the data (0) If the other Socket is closed, it is 0)

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description: This file is used to read the data and record the location where the data source is recorded. DataGram Socket

(UDP), a DataGram is read at a time; the STREAM Socket (TCP) is the same as the RECV (), the parameter from and the owl will be ignored by the system. If Socket is Blocking model

, And now there is no information in Input Buffer, RECVFTOM () will block to any information to

Dani; if it is a non-blocking mode, and INPUT BUFFER does not have any information, will return to the wrong

error.

[FD_WRITE Event]

The author introduces the occurrence opportunity of FD_READ events, and now continue to introduce FD_WRITE

This is more confused, because there is really a number of people in the event that the incident is unknown.

From the literal, FD_WRITE should be required to notify us to inform us whether there is a Socket now

Transfer information with call send () or sendto ()? The answer can be said "Yes", but it and fd_read

But there are different places.

In front, we know that after calling RECV (), if there is information in Input Buffer, it is not taken out.

If the system will notify us of fd_read. So if we call Send (),

The system's Output Buffer still has space to be writable, it will notify us one more

FD_WRITE, told us to continue to transfer information? This answer is "negative"! The system is not

Will notify us.

The system will notify us of the information of the FD_WRITE event, only the following cases:

(1) Call wsaasyncselect () to set the fd_write event, Socket is already available

Transfer information (TCP Scoket has been successfully connected, or UDP socket has been completed),

And now Output Buffer still has space to write information.

(2) Call wsaasyncselect () When setting the fd_write event, the socket is still not possible.

Send information, but once the socket is successful, it will receive the FD_WRITE.

know.

(3) When calling send () or sendto (), the system is informs the error and the error code is

10035 WSAEWOULDBLOCK (Call wsagetlasterror () knows this error), this

When the Output Buffer is already full, no need to write any information (at this point, call again.

Send () also failed); once the system successfully sent part to each other, empty Output Buffer

Afterwards, you will send an FD_WRITE to the user, and inform us to continue the information. In other words, read

When calling send (), as long as it is not returned to the error 10035, you can continue to call

Call send () to transfer information; once send () returns an error 10035, then don't call send ()

Transfer information, and after receiving FD_WRITE, continue to transfer information.

[Conclusion]

In this issue, the author introduces you about TCP Socket, sending methods and

FD_READ, FD_WRITE and other incidents, the occurrence of the occurrence of the readers, the readers can have been able to establish a pair of main processes from the architecture, and use TCP socket to transmit information.

Next, the author will continue to introduce a letter about how to get online information, such as

GethostName (), getSockName (), getPeername (), and synchronous with non-synchronous network database

Cultivate Funitional getXBYY (), wsaasyncgetxbyy ().

The WINKING trial version mentioned herein can be Septs1.seed.Net.tw from SEEDNET Taipei

(139.175.1.10) The UPLOAD / WINKING directory is obtained, and the file name is wkdemo.exe;

Winking provides Ethernet and PPP connection, suitable for general Ethernet networks, can also be used

Taking the phone, the data machine is connected to the SEEDNET PPP servo host; □ Case Demoserv, DemoClnt,

And some author writings Winsock program (including original program)

Upload / winking / jnlin directory; interested readers can use anonymous ftp mode

.

-

Origin: ● Zucai BBS ● from: 202.121.132.131

Like a frog jumps in the bottom of the well, when can I jump out ............................

2002-12-10 02:26 # 33

Wind and high

Middle school

member

UID 186

Essence 6

Points 2088

Post 990

Read permission 40

Registration 2002-11-4

Twenty-eighth article

Simple Winsock Application Design (3)

Lin Jun

In the previous two articles, the author introduced how to build a master from the Winsock environment.

TCP Socket, and how to use Socket to collect information; Today, let's take a look at how

Use the functions provided by Winsock to achieve some basic network information, including our own host

What is the name, the system actively specifies the IP address for our socket and port number, our

Who is connected to the other party connected to the Socket, how to find some host IP address or name, and some

Which of the port number used by Well-KNown services (such as FTP, Telnet, etc.).

Today we use the exhibition program is a 46 for Winsock 1.1 written before.

Make a form of test or teaching, interested in learning how to call the reader of 46 functions, available

Anonymous FTP mode to "tPTS1.SEED.NET.TW" "UPLOAD / WINKING / JNLIN"

The executive file and the original program of this program are obtained in the directory, and the file name is Hello. *. Readers can also use

The Hello program is to simulate the Server or Client program to verify what we do.

[How to know the Local host name we use]

Usually we will also help us with this host used to set a name; in the program, I

They can also achieve this master through a letter called gethostname () through Winsock.

Machine name.

◎ gethostname (): Get the name of the local host used by the current user.

Format: Int Pascal Far GethostName (Char Far * Name, INT Namelen);

Parameters: Name is used to store a temporary area for local host name

Namelen Name

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description: This file is used to get the name of Local Host.

The method of our call in the program is as follows:

Gethostname (Char Far *) Hname, Sizeof (HNAME))

If readers use Trumpet Winsock, you may know the environment setting of TrumpetT

Don't let us set the field of the local host name, so do some public domain

When Winsock Application (such as WS_PING, WINTALK), it will be missed when calling gethostname ().

Map; solution is to add your host IP address and name in the "Hosts" file of Trumpet.

Then call this in this group, it will not have an error.

[How to know the system proactive designation to our IP address and port number】

In the previous article, the author once mentioned the TCP Socket of the Client side to the call connect ().

Before connecting the Server end, you can call the bind () in a letter to specify the IP address used by the Client side SOCKET.

And Port Number; however, in general, our Client end does not need to call Bind () to specify specific

IP address and port number, but transfer to the system actively helps our Socket setting IP address and

PORT NUMBER (Call CONNECT () is a function). But how do we know what IP specified?

Add us and port number? This is to make a letter from getSockName ().

◎ getSockName (): Get the local location of the socket and Port Number data.

Format: Int Pascal Far getSockName (socket S,

Struct SockAddr Far * Name, Int Far * Namelen;

Parameters: s socket identification code

Name stores the temporary area of ​​this Soccl Local

Namelen Name length

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Description: This file is used to obtain the local order information of the set location or connected SOCKET. If

Yes, this Socket is set to INADDR_Addr_any, and then the correct establishment is true after successful.

Address.

The method of call in the program is:

Struct sockaddr_in sa;

INT SALEN = SIZEOF (SA);

GetSockName (SD, STRUCKADDR FAR * & SA, & SALEN)

[How do you know who is connected to our socket?

The connected socket has two ends, so relatable relative to getSockName (), Winsock also

For a getPeerName () card, let us get the other party's IP address and port

Number.

◎ getPeerName (): Gets the other party IP address of the connection success Socket and Port Number.

Format: Int Pascal Far getPeername (socket S,

Struct SockAddr Far * Name, Int Far * Namelen;

Parameters: s Socket Identification Code Name Save Temporary Region for the other party IP address connected to this socket

Namelen Name length

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Note: This one can be used to obtain the site data of the end of the connected Socket.

The way the call is as follows:

Struct sockaddr_in sa;

INT SALEN = SIZEOF (SA);

GetPeername (SD, STRUCKADDR FAR * & SA, & SALEN)

Now we still use Winking to be our Winsock Stack and use it.

Tools to observe whether sockets' links and information is correct.

From Figure 1, we can see the name of this host by Winking Window is the name of this host.

"Vincent", IP address is "140.92.61.24". We use two Hello programs, one

It is a Client (the right side of the screen), one as a server (minimizing the left side of the screen). Server

The port number used is "7016"; the client does not call Bind () to specify port.

Number, but call Connect () by the system specified.

We call gethostname (), the answer is "Vincent"; and Client call

GetsockName () gets its own IP address is "140.92.61.24", Port Number is "2110"

(The author has previously mentioned that the PORT Number, which is specified by the system, 1024 to 5000);

Then call getPeerName () Get the server-side IP address connected to the Client is "140.92.61.24"

(Because our Client and Server are in the same host), Port Number is "7016". fruit

That's right! (Sockets), which can also be observed by Winking Sockets' Status window.

The same result as the result of our call figures)

(Figure 1) Using the Hello program to simulate Client and Server

Readers must pay attention to the IP site and port obtained by getSockName () and getPeerName ().

Number is NetWork Byte Order, not Host Byte ORDER; if you want to turn into Host Byte

ORDER must have two functions of NTOHL () and NTOHS (). And we can see IP address "words"

The string "is expressed, and it is used in the inet_ntoa (), and we can also use the relative land.

INET_ADDR () in a letter converts a string mode IP address into an in_addr format (NetWork Byte Order)

Unsigned long).

◎ INT_NTOA (): Convert a network location into a "point format" string.

Format: Char Far * Pascal Far inet_ntoa (struct in_addr in);

Parameters: in a structure on behalf of the internet Host address

Passage: Success - a "Dotted) string failed - NULL

Description: This card transfers an Internet address to the "A.b.c.d" string format.

◎ INET_ADDR (): Converts the location of the string format into 32-bit yuan in_addr format.

Format: UNSIGNED Long Pascal Far inet_addr (Const Char Far * CP);

Parameters: CP A "Dotted) string representing IP address

Passage: Success - a unsigned long representing the internet location

Failure - INADDR_NONE

Description: This card translates a "point format" address to the applicable INTENET address.

The "point format" string can be one of the following four ways:

(i) a.b.c.d (ii) a.b.c (iii) A.B (iv) a

In the Hello program of Figure 1, we write Local data to dispmsg, then display it;

Usage is as follows:

WSPRINTF ((LPSTR) Dispmsg, "OK! Local IP =% S, Local Port =% D",

inet_ntoa (sa.sin_addr), ntohs (sa.sin_port);

[WINSOCK provided by the library "

Winsock also provides synchronous and non-synchronized network database functions; however, readers must know, this

The database refers to the database system, such as the commercial use of INFORMIX, ORACLE, but refers to the host.

IP site and name, Name of Well-KNown and Socket type and Port Number used.

And the protocol name and code.

[Synchronous Database Function]

First let's take a look at the first group: gethostbyname () and gethostbyaddr ()

The use of these two functions is to let us get its IP address, or by a host name, or by

Its IP address is called its name. Generally, we will often use the name of the IP; because

For a few people, I will remember the IP address of a machine, and the IP header of the TCP / IP package must also be

Refer to the IP address of the host, not the host name.

◎ getHostByname (): Use a host name to get the information of this host.

Format: Struct Hostent Far * Pascal Far

GethostByName (Const Char Far * name);

Parameters: Name Host Name

Passage value: success - pointing to an indicator of a Hostent structure

Failure - NULL (Call wsagetlasterror () can know the reason)

Note: This function uses the HOST name to get other information of the host, such as the address of Host,

Alias, location, length, etc.

◎ gethostbyaddr (): Use a Host's IP address to get the information of this host.

Format: Struct Hostent Far * Pascal Far

GethostByaddr (Const Char Far * Addr, Int Len, Int Type);

Parameters: AddR NetWork Alignment

Length of Len Addr

TYPE PF_INET (AF_INET)

Passage value: success - pointing to an indicator of a Hostent structure

Failure - NULL (Call WsageTlasterror () can be learned) Description: This file uses IP location to get other information of the host, such as the name of Host,

Name, location, length, etc.

The way the call in the program is as follows:

Char Host_name [30];

Struct Hostent Far * htptr;

/ * Suppose Host_Name's value has been set to the host name of our request information * /

Htptr = (Struct Hostent Far *) gethostByname (Char Far *) Host_name)

Struct in_addr host_addr;

Struct Hostent Far * htptr;

/ * Assumment Host_ADDR's value is set to Network Byte for the host of our request information

ORDER mode IP address * /

Htptr = (Struct Hostent Far *) gethostbyaddr ((Char far *) & host_addr, 4,

PF_INET)

Generally speaking, call to gethostByName () and gethostbyaddr (), Winsock

Stack will look at the information of this host in the "Hosts" file of Local; if not,

Then, through the "Domain Name Service" function, "Denoms"

"Name Server" query; so when you call these two functions, sometimes you will wait until you get it.

reply. If you want to make the program, you can put the usage of the usage host in the HOSTS file.

This will not have to query through DNS.

Let's take a look at GetServbyName () and getServbyport ().

Most readers should have used Telnet, Mail, FTP, News and other service applications;

Procedures for the process, such as the service name, port number used by the servo end, and Socket type

Samples are fixed; these materials, we can use GetServbyName () or getServByport ()

Come, don't deliberately remember others.

◎ GetServByname (): Get this in accordance with the Service (Service) Name and Communication Agreement (TCP / UDP)

Other information on the service.

Format: struct server * Pascal Far

GetServByname (Const Char Far * Name, Const Char Far * Proto);

Measure: Name Service Name

Proto Communication Agreement Name

Passage value: success - a indicator pointing to the SERVENT structure

Failure - NULL (Call wsagetlasterror () can know the reason)

Explanation: Use the service name and communication agreement to get the alias of the service, use the PORT number

Wait.

◎ GetServbyport (): In accordance with the service (Service) PORT number and communication agreement (TCP / UDP)

Other information on the service.

Format: struct server * Pascal Far

GetServByport (int port, const char far * proto);

Parameters: port number of port service

Proto Communication Agreement Name

Passage value: success - a indicator pointing to the SERVENT structure

Failure - NULL (Call WsageTlasterror () can be learned: Use the port number and communication agreement to get the name, alias, etc. of the service.

The usage methods in the program are:

Char serv_name [20];

Char proto [10];

Struct Server Far * svptr;

/ * Suppose Serv_name and Proto have first set up service name and communication protocol * /

Svptr = (Struct Server Far *) GetServbyName ((Char Far *) Serv_name, (Char Far

*) Proto)

Int serv_port;

Char proto [10];

Struct Server Far * svptr;

/ * Suppose Serv_Port and Proto have first set up the port number and communication protocols used by services * /

Svptr = (Struct Server Far *) GetServbyport (Htons (serv_port), (Char Far

*) Proto))

In the Winsock environment, we can query the service information is stored in Local.

"Services" file; this file is stored in Well-KNown service, basically we do not need

Go to change it. Readers can also add their services to this file, but the services you have

The information should be public, otherwise someone else's Services file is not your service information.

The final set of getProtobyname () and getProtobynumber () is used to get some

The information of "Agreement", such as TCP, UDP, IGMP, etc. In general, we will not be used.

◎ getProtobyname (): Obtaining the communication protocol in accordance with the name of the Communication Agreement (Protocol)

His information.

Format: Struct Protoent Far * Pascal Far

GetProtobyname (Const Char Far * Name);

Parameters: Name Communication Agreement Name

Passage value: success - a indicator pointing to the protoent structure

Failure - NULL (Call wsagetlasterror () can know the reason)

Description: Use the name of the communication agreement to learn the alias, number and other information of the communication agreement.

◎ getProtobynumber (): Other information on the communication agreement in accordance with the number of communication agreements.

Format: Struct Protoent Far * Pascal Far

GetProtobynumber (int Number);

Parameters: Number Arranged in Host Order Multiple Communication Agreement Number

Passage value: success - a indicator pointing to the protoent structure

Failure - NULL (Call wsagetlasterror () can know the reason)

Description: Use the number of communication agreements to learn the name, alias, etc. of the communication agreement.

The call mode in the program is as follows:

Struct Protoent Far * Ptptr;

Char proto_name [20];

/ * Suppose Proto_Name has set up a protocol name * /

PTPTR = (Struct Protoent Far *) GetProtobyname ((Char Far *) Proto_name)

Struct Protoent Far * Ptptr;

INT Proto_num;

/ * Suppose Proto_Num has set up Agreement No. * /

PTPTR = (Struct Protoent Far *) getProtobynumber (proto_num)

Winsock Stack For the application call getProtobyname () and getProtobynumber () information, is a "protocol" file from Local; if we don't need it, we don't have to change this file.

The content of the case.

(Figure 2) Hello program call synchronization library

[Non-synchronous database "

Winsock 1.1 provides relative to the six synchronous library functions described in front of the previous author

6 non-synchronous database functions, they are wsaasyncgethostbyname (),

WsaasyncgetHostbyAddr (), wsaasyncgetServbyName (),

WsaasyncgetServbyport (), wsaasyncgetprotobyname (),

WSaasyncGetProtobynumber ().

Since the information they have achieved is the same as the synchronous database, the author is only

WsaasyncgetHostByname () Take these non-synchronous functions, and tell you the same

Steps and non-synchronous database functions are different.

From the literal, "non-synchronization" means that when we issue a problem, it will not be immediately answered.

Waiting, and let us know when you get the information. That's right, these non-synchronous database functions

That's it. Like the WSaasyncselect (), we have to tell the Winsock system an acceptance

Inform us of the window and message code to inform us.

When we call the synchronous database, the return value is a temporary area that refers to the relative information, and this

The information tentative area is provided by the system; but when the call is an unacceptable database, we must

Prepare the information temporary area, and use the location of this temporary area as a parameter, pass it to the system so that the system is used

Save the information taken. Readers must pay special attention: success or failure in the system notification information

Before, you can delete the transfer of information transferred to the system, otherwise when the system gets information to be written

When the data area is no longer, it will lead to a machine. In addition, the size of the data temporary area must be enough.

Large, enough to allow the system to store the obtained information. (The recommended value in the Winsock specification is

MaxgetHoststruct 1024 Bytes size temporary storage area, the author thinks too big, 100 Byets

It's almost too enough?

When the call is a non-synchronous database, the resulting return is a code, which is represented by this code.

This is the number within the system; because it is not synchronized, we can still call before getting the answer.

Call wsacancelasyncRequest () in a letter to cancel the original call, this canceled action is profitable

Use this code. In addition, when we receive the result notification, the value of WPARAM is also this code; I

You can use WsageTasyncerror (LPARAM) to learn from the information to succeed or lost

Losing; if the reason for failure is that the original incoming temporary storage area is too small, we can also use

WsaasyncgetBuflen (LPARAM) is known enough to know how much the temporary area is.

◎ WSAAsyncGethostByname (): Use a Host's name to get the Host's capital

material. (Non-synchronous mode)

Format: Handle Pascal Far WsaasyncGethostByname (hwnd hwnd,

Unsigned int WMSG, Const Char Far * Name, Char Far * BUF, INT

BUFLEN);

Parameters: After the HWND action is completed, the window, HandleWMSG that accepts the message is sent back to the window.

Name Host Name

BUF Statement Zone for Hostent Information

BUFLEN BUF size

Passage: Success - Handle Code representing this non-synchronous action

Failure - 0 (Call wsagetlasterror () can know the reason)

Note: This file uses the HOST name to get other information, such as the address, alias,

Site, length, etc. When the user calls this one, it must be passed to the window handle to receive the information.

The information code, the storage address indicator of the data, etc., in order to get the information, you can notify the window to use the information.

material. When you call this card, you will go back to the user's call point and pass back a handle code. This code is

Can be used to distinguish this non-synchronous action or to cancel this non-synchronous action. When the information is obtained, the system will send one

A message to the user specified by the user.

◎ WSACANCELASYNCREQUEST (): Cancel a unfinished non-synchronization requirement.

Format: Int Pascal Far WsacanceLasyNCRequest (Handle

HasyncTaskHandle;

Parameters: HasyncTaskHandle To cancel Task Handle Code

Passage: Success - 0

Failure - Socket_ERROR (Call WsageTlasterror () can know the reason)

Note: This function is used to cancel the original call but has not yet completed WSaasyncGetxByy (), for example

Such as WSaasyncGethostByname (), the action. Parameters HasyncTaskHandle is called

The code value passed back when wsaasyncgetxbyy (). If the previous call is not completed,

It is unable to cancel.

(Figure 3) Hello program call non-synchronous database functions

[Conclusion]

The author has introduced the letter that will be used when the Winsock application design is designed.

Do you have anyone who knows whether the reader has started to practice yourself writing Winsock online programs? Next, author

The remaining form is introduced. This author is looking forward to using someone else's design.

In addition, everyone can practice some good network application software to make the rest of the world.

People know that Taiwan can also have people; I am willing to share.

转载请注明原文地址:https://www.9cbs.com/read-32236.html

New Post(0)