首页 新闻 会员 周边 捐助

多线程是用全局变量,为什么线程只能执行一次?

0
[已关闭问题] 关闭于 2012-12-19 08:59

我想用多线程实现4个端口的数据收发,定义了一个端口数组跟两个全局变量在线程中进行端口的选择,可是,收数据线程就执行一次,再就不执行收线程了,收到的数据是正确的,但是将收到的数据发出去时,接收到的却不完整,希望大家帮帮忙啊我想用多线程实现4个端口的数据收发,定义了一个端口数组跟两个全局变量在线程中进行端口的选择,可是,收数据线程就执行一次,再就不执行收线程了,收到的数据是正确的,但是将收到的数据发出去时,接收到的却不完整,希望大家帮帮忙啊

  1 #include<windows.h>
  2 #include<iostream.h>
  3 #pragma comment(lib, "Ws2_32.lib")
  4 #define AMOUNT 4    
  5 short m_port[AMOUNT] = {6000,6001,6002,6003};//端口号
  6 char sendip[AMOUNT][16] =
  7 {
  8     "127.0.0.1","127.0.0.1","127.0.0.1","127.0.0.1"
  9 };
 10 
 11 
 12 int SIndex = 0,RIndex = 0;
 13 SOCKET sock[AMOUNT];
 14 BOOL UDPIni();
 15 
 16 DWORD WINAPI RecvProc(LPVOID lpParameter);
 17 DWORD WINAPI SendProc(LPVOID lpParameter);
 18 
 19 HANDLE hThreadRecv;
 20 HANDLE hThreadSend;
 21 HANDLE g_hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
 22 
 23 BOOL UDPIni()
 24 {
 25     WORD wVersionRequested;
 26     WSADATA wsaData;
 27     int err;
 28     wVersionRequested=MAKEWORD(2,2); 
 29     err=WSAStartup(wVersionRequested,&wsaData);
 30     if(err != 0)
 31     {
 32         cout<<GetLastError()<<endl;
 33         cout<<"Winsock Library Failed Loading!"<<endl;
 34         return FALSE;
 35     } 
 36     if((LOBYTE(wsaData.wVersion) != 2) ||(HIBYTE(wsaData.wVersion) != 2))
 37     {
 38         cout<<"No Available Winsock library!"<<endl;
 39         WSACleanup();
 40         return TRUE;
 41     }
 42 
 43     SOCKADDR_IN WaitFor_Addr[AMOUNT];
 44     int len = sizeof(SOCKADDR);
 45     for(int count = 0; count < AMOUNT; count++)
 46     {
 47         sock [count] = socket(AF_INET, SOCK_DGRAM, 0);
 48         if(INVALID_SOCKET == sock[count])
 49         {
 50             cout<<"Failed Creating Socket!"<<endl;
 51             WSACleanup();
 52             return FALSE;
 53         }
 54 
 55         WaitFor_Addr[count].sin_addr.S_un.S_addr = htonl(INADDR_ANY);
 56         WaitFor_Addr[count].sin_family = AF_INET;
 57         WaitFor_Addr[count].sin_port = htons(m_port[count]);        
 58         int retvl=bind(sock[count],(SOCKADDR*)&WaitFor_Addr[count],len);    
 59         if(SOCKET_ERROR == retvl)
 60         {
 61             cout<<GetLastError()<<endl;
 62             cout<<"Failed Binding Socket!"<<endl;
 63             WSACleanup();
 64             return FALSE;
 65         }
 66     }
 67     
 68     hThreadRecv=CreateThread(NULL,0,RecvProc,NULL,0,NULL);
 69     hThreadSend=CreateThread(NULL,0,SendProc,NULL,0,NULL);
 70     CloseHandle(hThreadRecv);
 71     CloseHandle(hThreadSend);
 72     return TRUE;
 73 }
 74 
 75 char * Receive(int index)
 76 {
 77     char recvBuf[1000] = "";
 78     SOCKADDR_IN addrConnect;
 79     int len = sizeof(SOCKADDR);
 80     int retvl = recvfrom(sock[index],recvBuf,sizeof(recvBuf) + 1,0,
 81     (SOCKADDR*)&addrConnect,&len);
 82     if(SOCKET_ERROR == retvl)
 83     {
 84         cout<<"Failed Receiving Data!"<<endl;
 85         closesocket(sock[index]);
 86         WSACleanup();
 87         return NULL;
 88     }
 89     return recvBuf;
 90 }
 91 
 92 
 93 BOOL Send(int index, char * m_msg)
 94 {
 95     int sendretvl;
 96     SOCKADDR_IN addrConnect;
 97     addrConnect.sin_addr.S_un.S_addr = inet_addr(sendip[index]);
 98     addrConnect.sin_family = AF_INET;
 99     addrConnect.sin_port = htons(m_port[index]);    
100     sendretvl = sendto(sock[index],m_msg,strlen(m_msg) + 1,0,
101     (SOCKADDR*)&addrConnect,sizeof(SOCKADDR));
102 
103     if(SOCKET_ERROR == sendretvl)
104     {
105         cout<<"Failed Sending Data!"<<endl;
106         closesocket(sock[index]);
107         WSACleanup();
108         cout<<"send error: "<<GetLastError()<<endl;
109         return NULL;
110     }
111     return TRUE;
112 }
113 
114 
115 DWORD WINAPI RecvProc(LPVOID lpParameter)
116 {    
117     while(TRUE)
118     {
119         char strtmp[1000] ="";
120         WaitForSingleObject(g_hEvent,3000);        
121         strcpy(strtmp, Receive(RIndex));
122         if(0 != strlen(strtmp))
123         {
124             cout<<strtmp<<endl<<endl;
125         }
126         if(++RIndex >= AMOUNT)
127             RIndex =0;
128         cout<<"RIndex: "<<RIndex<<endl;
129         SetEvent(g_hEvent);
130     }
131     return 0;
132 }
133 
134 DWORD WINAPI SendProc(LPVOID lpParameter)
135 {    
136     while(TRUE)
137     {
138         WaitForSingleObject(g_hEvent,3000);            
139         
140         if(++SIndex >= AMOUNT)
141             SIndex =0;
142         cout<<"SIndex: "<<SIndex<<endl;
143         SetEvent(g_hEvent);
144     }
145     return 0;
146 }
147 
148 
149 void main()
150 {
151     UDPIni();
152     while(1);
153 }
hfc001的主页 hfc001 | 菜鸟二级 | 园豆:202
提问于:2012-12-18 23:34
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册