Use Winsock for full-duplex voice communication

Source: Internet
Author: User

[Article Information]Author: Zhang Xiaoming, Yang Jianhua, Qian minhai time: Source: PCVC responsibility Editor: Ark[Document Introduction]In Windows 95, based on the TCP/IP protocol, Winsock is used to transmit one end-end voice.

Abstract: In Windows 95, end-to-end voice transmission is completed using Winsock based on TCP/IP protocol. The use of the main functions and the Application Methods Based on the asynchronous selection mechanism are described based on the dual-socket technology. At the same time, the corresponding instance program is provided.

I. Introduction

Windows 95, as a microcomputer operating system, has fully integrated the network and communication functions. It can not only establish a "peer-to-peer network" in a Windows 95-only environment, but also support multiple protocols, such as TCP/IP, IPX/SPX, and NETBUI. In TCP/IP protocol groups, TPC is a connection-oriented collaboration that provides users with reliable and full-duplex byte stream services, it has functions such as validation, flow control, multiplexing and synchronization, and is suitable for data transmission. The UDP protocol is connectionless. Each group carries a complete Destination Address and each group is transmitted independently in the system. It does not guarantee the order of groups, and does not recover and retransmit packet errors. Therefore, it does not guarantee the transmission reliability. However, it provides highly efficient datagram services, suitable for real-time voice, image transmission, broadcast message and other network transmission.

The Winsock interface provides a new method for inter-process communication. It can be used not only for inter-process communication on the same machine, but also for network communication. With the launch of Windows 95. Winsock has been formally integrated into the Windows system, including 16-bit and 32-bit programming interfaces. Winsock development tools can also be found in Borland C ++ 4.0 and Visual C ++ 2.0 C compilers, mainly named winsock. h header file and dynamic Connection Library winsock. dll or wsodk32.dll, these two dynamic connection libraries are used for Win16 and Win32 applications respectively.

In this paper, we use UDP protocol to implement real-time network communication for full-duplex voice transmission. Use the Visual C ++ 2.0 compiling environment, and its dynamic Connection Library name is wsock32.dll.

Ii. Usage of Main Functions

By establishing dual sockets, you can easily implement full-duplex network communication.

1. Socket creation function:

SOCKET socket(int family,int type,int protocol)
For UDP, write as follows:

SOCKRET s;
S = socket (AF_INET, SOCK_DGRAM, 0 );
Or s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)

To create two sockets, you must bind the address repeatedly. That is, when a socket has been bound to a local address, you must call bind () before binding a second socket to a function, use setsockopt () to set the SO_REUSEADDR socket option for the socket. You can use the getsockopt () function to obtain the socket option setting status. Note that the port numbers of the two sockets cannot be the same. In addition, the socket buffer settings are also involved. according to regulations, the setting range of each zone is: no less than 512 bytes, which is larger than 8 K Bytes. 4 K bytes are selected as needed.

2. Socket binding function

int bind(SOCKET s,struct sockaddr_in*name,int namelen)
S is the socket just created. name points to the pointer of the struct describing the communication object. namelen is the length of the struct. The components in this struct include: IP address (corresponding to name. sin_addr.s_addr), Port Number (name. sin_port), address type (name. sin_family, generally assigned to AF_INET, indicating internet address ).

(1) Fill in the IP Address: In full duplex communication, convert the point-to-point representation address corresponding to the user name to a 32-bit long integer IP address, using the inet_addr () function.

(2) The port number is used to indicate different processes (applications) of the same computer. There are two ways to allocate the port number: 1) the process allows the system to automatically assign a port number to the socket, you only need to specify the port number as 0 before calling bind. The port number automatically allocated by the system is located in 1024 ~ Between 5000, and 1 ~ Any TCP or UDP port between 1023 is retained, and the system does not allow any process to use the retained port unless its valid user ID is zero (Super User ).

2) A process can specify a specific port for the socket. This is useful for servers that need to allocate all ports to the socket. The value range is between 1024 and 65536. It can be specified at will.

In this program, the port numbers of the two sockets are set to 2000 and 2001. The former corresponds to the sending socket, and the latter corresponds to the receiving socket.

The port number must be converted from a 16-bit unsigned number (u_short type number) to a network byte sequence using the htons () function.

Based on the above two functions, we can provide a piece of program for the establishment and binding of dual sockets.

// Set related global variables
SOCKET sr, ss;
HPSTR sockBufferS, sockBufferR;
HANDLE hSendData, hReceiveData;
DWROD dwDataSize = 1024*4;
Struct sockaddr_in therel. 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
// Socket-based function Creation
BOOL make_skt (HWND hwnd)
{
Struct sockaddr_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 creation failed !", "", 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 (); // lock settings of the socket buffer
Setsockopt (ss, SOL_SOCKET, SO_SNDBUF, (char FAR *) sockBufferS, dwDataSize );
If (bind (ss, (LPSOCKADDR) & here, sizeof (here )))
{
MessageBox (hwnd, "sending 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, "failed to receive socket binding !", "", MB_ OK );
Return (FALSE );
}
Return (TRUE );
}
// Socket buffer settings
Void sockBuffer (void)
{
HSendData = GlobalAlloc (GMEM_MOVEABLE | GMEM_SHARE, dwDataSize );
If (! HSendData)
{
MessageBox (hwnd, "failed to locate sending Socket buffer !", NULL,
MB_ OK | MB_ICONEXCLAMATION );
Return;
}
If (sockBufferS = GlobalLock (hSendData) = NULL)
{
MessageBox (hwnd, "sending Socket buffer lock failed !", NULL,
MB_ OK | MB_ICONEXCLAMATION );
GlobalFree (hRecordData [0];
Return;
}
HReceiveData = globalAlloc (GMEM_MOVEABLE | GMEM_SHARE, dwDataSize );
If (! HReceiveData)
{
MessageBox (hwnd, "" receive Socket buffer locating failed !", NULL
MB_ OK | MB_ICONEXCLAMATION );
Return;
}
If (sockBufferT = Globallock (hReceiveData) = NULL)
MessageBox (hwnd, "sending Socket buffer lock failed !", NULL,
MB_ OK | MB_ICONEXCLAMATION );
GlobalFree (hRecordData [0]);
Return;
}
{

3. data sending and receiving 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)

The flags parameter is generally set to 0.

The recvfrom () function actually reads a data packet sent by the sendto () function. When the number of data bytes read is less than the specified number of received data bytes, all data is received, the number of actually received bytes is returned. When more data is read than the specified value, excess data is discarded in the data packet mode. In the stream mode, the remaining data is read from the lower recvfrom. To send and receive data, you must create a data sending buffer and a data receiving buffer. Rule: A datagram at the IP layer cannot exceed 64 KB (including data headers ). When the buffer is set too much or too large, the establishment of socket fails due to insufficient memory. This error disappears after the buffer is reduced. 4 K bytes are used in the experiment.

In addition, pay attention to the writing of the final parameters in the two functions. The final parameter for sendto () is an integer, while the recvfrom () is a pointer to an integer.

4. SOCKET close function: closesocket (SOCKET s)

When the communication ends, the specified socket should be closed to release the relevant resources.

When closing the socket, release the locked buffer. The program segment is:

Void CloseSocket (void)
{
GlobalUnlock (hSendData );
GlobalFree (hSenddata );
GlobalUnlock (hReceiveData );
GlobalFree (hsf-edava );
If (WSAAysncSelect (ss, hwnd, 0, 0) = SOCKET_ERROR)
{
MessageBos (hwnd, "failed to close the sending socket !", "", MB_ OK );
Return;
}
If (WSAAysncSelect (sr, hwnd, 0, 0) = SOCKET_ERROR)
{
MessageBox (hwnd, "failed to close the receiving socket !", "", MB_ OK );
Return;
}
WSACleanup ();
Closesockent (ss );
Closesockent (sr );
Return;
}

Iii. Programming features and asynchronous selection mechanism of Winsock

1. Blocking and Processing Method

In network communication, due to network congestion or a large amount of data to be sent at a time, data exchange is often unable to be transmitted in a short period of time. Therefore, functions for sending and receiving data cannot be returned.

Related Article

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.