Standard MFC WinSock ActiveX Control Development Instance (II) Advanced articles

Source: Internet
Author: User
Tags date array integer range

Review: In the previous article "standard MFC WinSock ActiveX Control Development Example" we detailed the control development process, as well as interface and event

Add and respond to methods. Now we will continue to develop the controls that were not written last, and refine the functionality that should be available as a Winsock control.

Second, according to the knowledge mentioned in the previous article, we now add two new interfaces, respectively, SendData () and GetData (), which look as follows:

//网络数据发送,在指定的超时时间内进行发送然后返回,成功返回实际发送字节数,否则返回负数
long CMFCWinSockCtrl::SendData(const VARIANT FAR& Data,

const VARIANT FAR& DataType,

const VARIANT FAR& DataLength,

const VARIANT FAR& TimeOut)
{

// TODO: Add your dispatch handler code here

return 0;
}

//获取数据,并指定获取数据的超时时间,返回实际获取到的数据长度,否则返回负数
long CMFCWinSockCtrl::GetData(VARIANT FAR* Data,

const VARIANT FAR& DataType,

const VARIANT FAR& DataMaxLength,

const VARIANT FAR& TimeOut)
{

// TODO: Add your dispatch handler code here

return 0;
}

The parameters of the two interfaces are similar except for the first argument. SendData () is the data that is sent, does not require the data to be brought back, so the direct use of the variant, and GetData () is required to bring the data back to the caller, so defined as the VARIANT * type, the second argument datatype the name meaning is the definition of the transmitted or received data type, The third parameter is the length of the data to be transmitted or received, where the length is char as a length, and if the type is type int, the length is 4, and if the string is defined, a Chinese character occupies 2 lengths. The last parameter is the timeout time when the network sends or reads.

Third, add the source code for the Connect () interface, it looks like the following:

Network data is sent, sent in the specified timeout period, and then returned, successfully returns the actual number of bytes sent, otherwise negative returns
Long Cmfcwinsockctrl::senddata (const VARIANT far& Data,

Const VARIANT far& DataType,

Const VARIANT far& datalength,

Const VARIANT far& TimeOut)
{

Todo:add your dispatch handler code here

if (! Onlysock)

return-1;//Network has not started establishing a connection

int gdatatype = Varianttolong (DataType);

Long gdatalength = Varianttolong (datalength);

int gtimeout = Varianttolong (TimeOut);

if (Gdatatype < 0)

Return-2;

if (gdatalength <= 0)

Return-2;

if (Gtimeout < 0)

Return-2;

Switch (gdatatype)

{

Case 0://The default form, when the data is found to be an integer array, no conversions are made, and an int is transmitted directly to a char (which may overflow the range).

Case 1://When the value is specified 1 o'clock, when date is an array of long integers, a long is converted to four char transfer

Case 2://When the value is specified 2 o'clock, when date is an integer array, an int is converted to four char transfer

Case 3://When the value is specified 3 o'clock, when the date is an array of unsigned short integers, a unsigned is converted to two char transfer

Case 4://When the value is specified 4 o'clock, when date is a byte array, a byte is converted to a char transfer

Case 5://When the value is specified 5 o'clock, when date is an array of short integers, a shorter number is converted to two char transfer

Case 6://When the value is specified 6 o'clock, when date is an array of floating-point types, a float is converted to four char transfer

Case 7://When the value is specified 7 o'clock, when date is a double-precision array, a double is converted to eight char transfer

Break

default://if it is not in the value range above, it will be routed according to the current data corresponding type

Break

}

Timeval TV;

Fd_set Fdwrite;

int len = 0;

Long m = 0;

Long n = 0;

Long changetype = 0;//convert floating-point data to type and then transfer

VARIANT GData;

VariantInit (&gdata);

Send information to the server

Fd_zero (&fdwrite);

Tv.tv_sec = gtimeout;//return after specified time

tv.tv_usec = 0;

Fd_set (onlysock,&fdwrite);//Whether data can be sent

Select (0,NULL,&AMP;FDWRITE,NULL,&AMP;TV);

char *buffer = NULL;

if (Fd_isset (Onlysock,&fdwrite))

{

Switch (DATA.VT)

{

Case vt_bstr://sent As String

Buffer = _com_util::convertbstrtostring (data.bstrval);

Break

Case vt_byref| VT_UI1:
Send As Byte*

Buffer = new Char[gdatalength];

memcpy (buffer,data.pbval,gdatalength);

Break

Case vt_byref| vt_i1://Press char * send

Buffer = new Char[gdatalength];

memcpy (buffer,data.pcval,gdatalength);

Break

Case vt_array| VT_I4://is sent in a long array of integers

GDATA.VT = VT_I4;

if (gdatatype!=0)//long = char*4

{

sizeof (long), where the length of a long integer is 4 char

Buffer = new Char[gdatalength];

for (m=0,n=0; n<gdatalength/4; n++)

{


Safearraygetelement (Data.parray,&n,&gdata.lval);


buffer[m++] = (gdata.lval>>24) &0xff;


buffer[m++] = (gdata.lval>>16) &0xff;


buffer[m++] = (gdata.lval>>8) &0xff;


buffer[m++] = gdata.lval&0xff;

}

}

Else//long = char*1//Data may overflow

{

Buffer = new Char[gdatalength];

for (m=0,n=0; n<gdatalength; n++)

{


Safearraygetelement (Data.parray,&n,&gdata.lval);


Buffer[n] = (char) gdata.lval;

}

}

Break

Case vt_array| VT_INT://is sent as an integer array

GDATA.VT = Vt_int;

if (gdatatype!= 0)

{

An int equals four char

Buffer = new Char[gdatalength];

for (m=0,n=0; n<gdatalength/4; n++)

{


Safearraygetelement (Data.parray,&n,&gdata.intval);


buffer[m++] = (gdata.intval>>24) &0xff;


buffer[m++] = (gdata.intval>>16) &0xff;


buffer[m++] = (gdata.intval>>8) &0xff;


buffer[m++] = gdata.intval&0xff;

}

}

Else

{

Buffer = new Char[gdatalength];

for (n=0; n<gdatalength; n++)

{


Safearraygetelement (Data.parray,&n,&gdata.intval);


Buffer[n] = (char) gdata.intval;

}

}

Break

Case vt_array| vt_ui1://sent in byte array

GDATA.VT = vt_ui1;//a char equals a byte do not have to convert

Buffer = new Char[gdatalength];

for (n=0; n<gdatalength; n++)

{

Safearraygetelement (Data.parray,&n,&gdata.bval);

Buffer[n] = Gdata.bval;

}

Break

default://There are no other types listed here, and the rest will be processed by your data conversion, and I'll be lazy ^_^

return-3;//incoming data type not supported

}

Len = Send (onlysock, buffer, gdatalength, 0);/Send data

delete[] buffer;

buffer = NULL;

if (len<=0)/= = Socket_error)

{

return-101;//cannot send data, the other side may have disconnected

}

}

Else

{

return 0;//Network timeout

}

VariantClear (&gdata);

return Len;
}

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.