The principle + code of TCP implementation of asynchronous socket programming in "turn" VC

Source: Internet
Author: User
Tags switch loop

The so-called asynchronous socket programming is called the following function

WSAAsyncSelect sets the state of the socket to async, and I'll describe the function in detail below ...

Asynchronous sockets solve the problem of clogging during socket programming .... What is clogging? Take a look below

You may have had this experience in VC writing interface-based network program, call Recv Recvfrom accept and other functions when the main thread of the entire program into the blocked state until there is a connection or the arrival of information.  We can use multi-threaded approach to solve the problem of the main thread blockage, but if a program needs multiple sockets then we need to create multiple threads, the creation of the thread back will cause some unnecessary inconvenience and operational inconveniences. We use non-blocking sockets can solve this problem, now understand, I will tell you the asynchronous socket implementation of the client service side of the communication code I'll upload to csdn everyone can download ....

When a connection comes in I assume that this is a TCP-based server then I want to accept the wait, but in an asynchronous socket this wait is shifted. Let's do it. Acept a message in response to call only when some of the conditions we want to happen will call accept then this is the only time that the client connect will call accept so that the thread will never block.

There may be some ambiguity. Here is a TCP server-side write order:

1. First load the socket font and include the necessary files

#include "Winsock2.h"

#pragma comment (lib, "Ws2_32.lib")

2. Load socket font to server side and client library version we're going to be a 2.0 socket version here.

Wsadata WSA;

WSAStartup (Makeword (2,0), &WSA);

3. Create sockets and send Wm_socket messages (our custom messages) when a network event occurs with WSAAsyncSelect set to asynchronous sockets

#deinfe wm_socket wm_user+1, the corresponding message response to be added yourself will not download the source code I gave

int WSAAsyncSelect (

SOCKET s ,//socket

hwnd hwnd,//form to send a message hwnd

unsigned int wmsg,//wm_socket

Long lEvent ,/// Note that this is the network event we need. Specifically, we use only Fd_read fd_accept is the network read event and network connection event

 );

4. Configure the address structure body sockaddr_in and call bind bind address struct and call listen Listen * (be sure to call bind before listen)

When we're done, we don't have to accept it. Because this needs to be called in the message response function, because we're interested in a variety of network events, we need the message response function

Use a switch loop to determine which network event part code is as follows:

void Csynsocketsrvdlg::onconnect (WPARAM WPARAM, LPARAM LPARAM)
{

Switch (LParam)
{
Case FD_ACCEPT:
{
SOCKADDR addr;
int Len;
Len=sizeof (SOCKADDR);
GetDlgItem (IDC_EDIT1)->setwindowtext ("New connection coming .... \ n");
Tem=::accept (M_socket, (sockaddr*) &addr,&len);
break;
}
Case Fd_read:
{
Char buf[100] = "";
Recv (tem,buf,100,0);
GetDlgItem (IDC_EDIT1)->setwindowtext (BUF);
break;
}

}

}

Client

1. First load the socket font and include the necessary files

#include "Winsock2.h"

#pragma comment (lib, "Ws2_32.lib")

2. Load socket font to server side and client library version we're going to be a 2.0 socket version here.

Wsadata WSA;

WSAStartup (Makeword (2,0), &WSA);

3. Create sockets and send Wm_socket messages (our custom messages) when a network event occurs with WSAAsyncSelect set to asynchronous sockets

#deinfe wm_socket wm_user+1, the corresponding message response to be added yourself will not download the source code I gave

int WSAAsyncSelect (

SOCKET s ,//socket

hwnd hwnd,//form to send a message hwnd

unsigned int wmsg,//wm_socket

Long lEvent ,/// Note that this is the network event that we need to specifically see MSDN we generally only use Fd_read is the network read event );

4, then we can send the data sent is send

Receives the message and the service side is implemented in the message response function

Code

http://download.csdn.net/detail/yue7603835/3771590

Reference: http://blog.csdn.net/yue7603835/article/details/6949817

The principle + code of TCP implementation of asynchronous socket programming in "turn" VC

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.