Complete one end of the voice with Winsock

zhaozj2021-02-08  255

Summary: In the Windows 95 environment, based on the TCP / IP protocol, one end of the voice is completed with Winsock. Double-socket technology is used, and the main function of the main function is expounded, as well as application methods based on asynchronous selection mechanisms. At the same time, the corresponding example program is given. 

Keywords: Windows 95, Voice Communication, TCP / IP, WINSOCK 

First, introduction

Windows 95 is fully integrated into network and communication functions, but also can establish "peer network" in pure Windows 95 environments, but also support multiple protocols, such as TCP / IP, IPX / SPX, NetBUI, etc. In the TCP / IP protocol group, TPC is a connection-oriented accepted, providing users with reliable, full-duplexed byte stream service, confirming, stream control, multiplexing, and synchronization functions, suitable for data transmission. The UDP protocol is unconnected, and each packet carries a complete destination address, and each packet is transmitted independently in the system. It does not guarantee the order of the group, and does not perform the recovery and retransmission of grouping errors. Therefore, it does not guarantee the reliability of the transmission, but it provides a data newspaper service of high transmission efficiency, suitable for real-time voice, image transmission, broadcast message, etc. network transmission. 

The Winsock interface provides a new means of communication between processes, which can not only be used between processes in the same machine, but also support network communication functions. With the launch of Windows 95. Winsock has been officially integrated into a Windows system while including 16-bit and 32-bit programming interfaces. Winsock's development tools can also be found in Borland C 4.0, Visual C 2.0 These C compilers, mainly consisting of header files and dynamic connection libraries Winsock.dll or WSODK32.dll, named Winsock.h. These two dynamic connectors are used for WIN16 and WIN32 applications. 

This article uses the UDP protocol to realize real-time network communication with the UDP protocol for the full duplex transmission requirements of the voice. With the Visualc 2.0 compilation environment, its dynamic connection is WSOCK32.DLL. 

Second, the main function of the main function

By establishing a double-socket, you can easily achieve full duplex network communication. 

1. Set of socket creation functions: 

Socket Socket (Int Family, Int Type, INT Protocol)

For the UDP protocol, write as: 

Sockret S; 

s = socket (AF_INET, SOCK_DGRAM, 0);

Or s = Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)

In order to establish two sockets, the repeated binding of the address must be achieved, that is, when a socket has been bound to a local address, in order to make another socket repeatedly use the address, it must be called bind () The function setsockopt () sets the SO_REUSEADDR socket option before the function is bound to the second socket. The socket option setting status is obtained by function getSockOpt (). It should be noted that the port number corresponding to the two sockets cannot be the same. 

In addition, it is also related to the setting problem of the socket buffer, according to the specified, the setting range of each zone is: not less than 512 bytes, large than 8K bytes, and 4K bytes are selected as needed. 

2. Set of the word binding function 

Int bind (socket s, struct sockaddr_in * name, int namelen)

S is a nice socket just created, Name points to a pointer to the structure of the communication object, Namelen is the length of the structure. The components in the structure include: IP address (corresponding name.sin_addr.s_addr), port number (Name.sin_Port), address type (name.sin_family, generally assigned AF_INET, indicating is an Internet address).  (1) Fill in the IP address: In the full duplex communication, the username corresponding to the user name is to be converted to the 32-bit long integer format IP address, using the INET_ADDR () function. 

(2) The port number is used to indicate different processes (applications) of the same computer (application), and there are two types: 1) Process allows the system to automatically assign one end slogan for socket, as long as it is called Bind The number is specified as 0. The port number automatically assigned by the system is between 1024 to 5000, and any TCP or UDP port between 1 to 1023 is reserved, and the system does not allow any process to use the reserved port, unless its valid user ID is zero ( root). 

2) The process can specify a specific port for the socket. This is useful for servers that need to be assigned to a set of portions. The specified range is between 1024 and 65536. Can be arbitrarily specified. 

In this program, the port number of the two sockets is specified as 2000 and 2001, the former corresponds to the socket, and the latter corresponds to the receiving socket. 

The port number is to convert from a 16-bit unsigned number (U_SHORT type) from host byte order into network byte order, use

Htons () function. 

Depending on the above two functions, the two-socket segmentation and the binding program can be given;

// Set the relevant global variable 

Socket SR, SS; 

HPSTR SOCKBUFFERS, SOCKBUFFERR;

Handle Hsenddata, HRECEIVEDATA;

DWROD DWDATASIZE = 1024 * 4; 

Struct SockAddr_in there there2;

#Define local_host_addr 200.200.200.201 

#Define remote_host-addr 200.200.200.202 

#Define local_host_port 2000 

#Define local_host_port 2001 

// Sack of the text creation function 

Bool make_skt (hwnd hwnd) 

{

Struct socketdr_in here, here1; 

SS = Socket (AF_INET, SOCK_DGRAM, 0);

SR = Socket (AF_INET, SOCK_DGRAM, 0);

IF ((ss == invalid_socket) || (SR == Invalid_socket))

{

Messagebox (hwnd, "socket is built!", "", Mb_ok);

Return (false); 

}

Here.sin_family = AF_INET; 

Here.sin_addr.s_addr = inet_addr (local_host_addr);

Here.sin_port = HTONS (Lical_Host_Port);

// aNother Socket

Herel.sin_family = AF_INET; 

Herel.sin_addr.s_addr (local_host_addr);

Herel.sin_port = htons (local_host_port1);

SocketBuffer (); / / / / SLR buffer lock setting 

Setsockopt (SS, SOL_Socket, SO_SNDBUF, (Char Far *) sockbuffrs, dwdatasize;

IF (Bind (SS, (LPSOCKADDR) & here, sizeof (here)) {

MessageBox (hwnd, "send socket binding failed!", "", Mb_ok);

Return (False);

}

Setsockopt (SR SQL_Socket, SO_RCVBUF | SO_REUSEADDR, (Char Far *)

SockBufferr, DwDataSize;

IF (Bind (Sr, (LPSOCKADDR) & here1, sizeof (here1))))

{

MessageBox (hwnd, "receiving socket binding failed!", "", Mb_ok);

Return (False);

}

Return (TRUE);

}

// Socker buffer settings

Void Sockbuffer (Void)

{

Hsenddata = GLOBALLOC (GMEM_MOVEABLE | GMEM_SHARE, DWDATASIZE);

IF (! hsenddata)

{

Messagebox (HWND, "Send Socket Buffer Location Failed!", NULL,

MB_OK | MB_ICONEXCLAMATION;

Return;

}

IF ((SockBuffers = Globalock (Hsenddata) == NULL)

{

Messagebox (hwnd, "send socket buffer lock failed!", NULL,

MB_OK | MB_ICONEXCLAMATION;

GlobalFree (HRecorddata [0];

Return;

}

HRECEIVEDATA = GLOBALLOC (GMEM_MOVEABLE | GMEM_SHARE, DWDATASIZE);

IF (! hreceivedata)

{

MessageBox (hwnd, "" Receive Sockets Buffer Location! ", NULL

MB_OK | MB_ICONEXCLAMATION;

Return;

}

IF ((SockBuffert = Globalock (HRECEIVEDATA)) = NULL)

Messagebox (hwnd, "send socket buffer lock failed!", NULL,

MB_OK | MB_ICONEXCLAMATION;

GlobalFree (HRecorddata [0]);

Return;

}

{

3. Data send and receive functions;

Int Sendto (Socket S.char * BUF, INT LEN, INT FLAGS, STRUCT SOCKADDR_IN TO, INT

TOLEN); 

Int Recvfrom (Socket S.char * BUF, INT LEN, INT FLAGS, STRUCT SOCKADDR_IN

Fron, int * fromlen

Where parameter flags generally take 0. 

The RECVFROM () function actually reads a packet sent by the sendto () function. When the read data byte is less than the number of received received, the data is received, and the number of data that is actually received by the actual number of bytes. When the data read is more than the specified value, excess data will be discarded in the data packet. In the flow mode, the remaining data is read from the underfrom (). In order to send and receive data, a data transmission buffer and a data receiving buffer must be established. It is stipulated that a datagon of the IP layer has a maximum of no more than 64K (including data headers). When the buffer is set too much, when it is too large, the socket is often failed due to insufficient memory. After reducing the buffer, the error disappears. After experiment, 4k bytes were selected in the text. 

In addition, it should also be noted that the write method of the last parameters in these two functions is an integer value for the last parameter of Sendto (), and RECVFROM () is a pointer to an integer value. 

4. Set of texture Closed function: CloseSocket (Socket S) 

At the end of the communication, you should close the specified socket to interpret the resources associated with it. 

When closing the socket, you should release the locked buffers. Its program is: 

Void CloseSocket (Void)

{

GlobalUnlock (hsenddata); 

GlobalFree (hsenddata);

GlobalUnlock (HRECEIVEDATA);

GlobalFree (HRECEIVEDAVA);

IF (WSAYSNCSELECT (SS, HWND, 0, 0) = Socket_ERROR) 

{

Messagebos (hwnd, "send socket closure failed!", "", Mb_ok); 

Return; 

}

IF (WSAAYSNCSELECT (SR, HWND, 0, 0) == Socket_ERROR) 

{

Messagebox (hwnd, "receiving socket closing failed!", "", Mb_ok); 

Return; 

}

Wsacleanup (); 

CloseSockent (SS);

CloseSockent (SR);

Return; 

}

Third, Winsock programming features and asynchronous selection mechanisms 

1 blocking and its processing method

In network communication, due to the excessive amount of data of network crowding or one transmission, the data that is often exchanged in a short period of time cannot be transferred, and the transceiver data cannot be returned, which is called blocking. Winsock provides two ways to block the possible blockage: blocking and non-blocking methods. In blocking mode, the function of sending and receiving data has been transferred or errors after being called. During the blocking period, the transformer of the bar does not break the system function getMessage () to keep the message loop. For non-blocking methods, the function returns immediately after the function is called, sending a preceding message to the program by Winsock after the transfer is completed. 

When programming, you should use a non-blocking method as much as possible. Because the user may try to close the program during the waiting process of the user, the message loop is still acting, so the program's window may be turned off, so when the function returns from the Winsock's dynamic connection library, the main program It has been deleted from memory, which is obviously extremely dangerous. 

2 Use of asynchronous selection function WSAASYNCSELECT () 

Winsock automatically sets the socket in non-blocking through WSaasyncSelect (). The key to implementing Windows Network programming using Windows Sockets is that it provides network events for web events based on messaging for registering applications. It requests the Windows Sockets DLL to send a message to the window when detecting a network event that occurs on the socket. For the UDP protocol, these online events are: 

FD_READ expects to receive notifications when the socket receives data (ie read ready);

FD_WRITE expects to receive notifications when the number of sockets can be sent (ie, write ready);

FD_CLOSE expects to connect to the callback notification when the socket is closed

Message Variable WPARAM Indicates the socination of network events, and the network event that occurs in the low byte description of the variable 1PARAM, the high character contains an error code. Add a branch in the message loop in the window function: 

Int Ok = SizeOf (SockAddr);

Case WMSG;

Switch (1PARAM)

{

Case FD_READ:

// Socket read data 

IF (RECVFROM (Sr.LpplayData, 0, (Struct Sockaddr Far *) & Twhere1,

(int Far *) & ok) == Socket_error0 {

Messagebox) hwnd, "Data Receive Failed!", "", MB_OK); 

Return (false); 

}

Case FD_WRITE:

// Sockets write data 

}

Break; 

In the preparation of the program, WSAAsYNCSELECT () corresponds to the corresponding message loop as needed, and other descriptions can be found in the literature [1]. Moreover, it should be noted that the message box in the above program block is mainly set to be convenient for program debugging, and no longer appear in the official product. At the same time, according to the comparable error design, a special fault-tolerant handler should be established. Various errors that may occur in the program will be processed by this function, and several different processing measures are established based on the degree of harm of errors. In this way, it can guarantee the smoothness and reliability of both parties. 

Fourth, conclusion

This article is one of the important contents of the multimedia network transmission project. Currently, the full-duplex communication of the voice has been successfully implemented in conjunction with the hardware full-duplex card. The content of the entire multimedia transmission system design will be further described.

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

New Post(0)