我是靠谱客的博主 酷炫微笑,最近开发中收集的这篇文章主要介绍multi thread,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

DWORD WINAPI PThread(LPVOID a);


DWORD WINAPI tx_PThread(LPVOID a);


int main(int argc, char* argv[])
{
DWORD tx_ThreadId;


int i=0;


HANDLE hThread=CreateThread(NULL,NULL,tx_PThread,LPVOID(i),0,&tx_ThreadId);


for(;;);


return 0;
}






DWORD WINAPI tx_PThread(LPVOID a)
{
WSADATA wsaData;
    WORD sockVersion = MAKEWORD(2, 2);


//加载winsock库
if(WSAStartup(sockVersion, &wsaData) != 0)
return 0;


while(1)
{
//osd_data_set(value_cnt++);
//lz_tmp_start
ScanfData[1][22*16]  = '0'+value_cnt/100000000%10;
ScanfData[1][22*16+1] = '0'+value_cnt/10000000%10;
ScanfData[1][22*16+2] = '0'+value_cnt/1000000%10;
ScanfData[1][22*16+3] = '0'+value_cnt/100000%10;
ScanfData[1][22*16+4] = '0'+value_cnt/10000%10;
ScanfData[1][22*16+5] = '0'+value_cnt/1000%10;
ScanfData[1][22*16+6] = '0'+value_cnt/100%10;
ScanfData[1][22*16+7] = '0'+value_cnt/10%10;
ScanfData[1][22*16+8] = '0'+value_cnt%10;


value_cnt++;
//lz_tmp_end


Sleep(1000);


for(int send_num=0; send_num<CNT; send_num++)
{
if(value_cnt>1 && data_back_flag==1)
{
send_num=1;
}


//if(value_cnt==1 && send_num==0)
if(!data_back_flag)
{
// 创建套节字
printf("init socketn");

closesocket(sClient);


int nNetTimeout=1000;//1秒,


sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sClient == INVALID_SOCKET)
{
printf("socket errorn");
data_back_flag = 0;
break;
//return 0;
}


setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&nNetTimeout,sizeof(int));


// 在sockaddr_in结构中装入服务器端地址信息
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(1400);
servAddr.sin_addr.S_un.S_addr =inet_addr("192.168.1.209");//服务器端ip


//连接服务器端
if(connect(sClient,(sockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR)
{
printf("connect errorn");
closesocket(sClient);
data_back_flag = 0;
break;
//return 0;
}
}


DWORD ThreadId;


int i=0;
data_rev_flag=0;
HANDLE hThread=CreateThread(NULL,NULL,PThread,LPVOID(i),0,&ThreadId);


send(sClient,ScanfData[send_num],data_len[send_num],0);


//WaitForSingleObject(hThread,INFINITE);
int time_out=0;
do
{
Sleep(500);
}while(time_out++<10 && data_rev_flag==0);


if(time_out>=10)
{
TerminateThread(hThread,0);
data_back_flag = 0;

printf("ERROR! TerminateThread(hThread,0)!n");
}
}


}


closesocket(sClient);


WSACleanup();


return 0;
}




DWORD WINAPI PThread(LPVOID a)
{
int i;


while(TRUE)
{
int ret=recv(sClient,RevData,255,0);


if(ret>0)
{
//RevData[ret]=0x00;
printf("Receive(%d):n", value_cnt-1);


for(i=0; i<ret; i++)
{
printf("%02x ",(unsigned char )RevData[i]);
}


printf("n");


//检测数据回桢




if((unsigned char)RevData[0]==0xb0 && (unsigned char)RevData[3]==0x58)
{
if((unsigned char)RevData[9]==0x08 && (unsigned char)RevData[10]==0x01 )
{
data_back_flag = 1;
}
else if((unsigned char)RevData[9]==0x04 && (unsigned char)RevData[10]==0x01)
{
data_back_flag = 0;
printf("ERROR! PThread->data_back_flag ERROR1!n");
}
else
{
printf("ERROR! PThread->data_back_flag ERROR2!n");
}
}


break;


}
}


data_rev_flag = 1;


return 0;
}

最后

以上就是酷炫微笑为你收集整理的multi thread的全部内容,希望文章能够帮你解决multi thread所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部