基于wince OS客户端与基于window OS的服务器端之间的SOCKET通讯
1、开发工具(1)客户端:EVC POCKET 2003 运行平台:WINCE
(2)服务端:VC + SQL SERVER 2000 运行平台:WINDOWS XP/2000
客户端SOCKET部分相关代码:
m_list.DeleteAllItems(); UpdateData(TRUE);
SOCKET sock; sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0) { return ; } int err;
char ansiRemoteHost[255]; ZeroMemory(ansiRemoteHost,255);
CString host = _T("192.168.0.88"); CString strSql; int count;//ÓÃÀ´¼Ç¼'#'×Ö·ûµÄ¸öÊý£¬ÒòΪ·þÎñÆ÷¶Ï·µ»ØµÄ¸ñʽΪ1@2@3#4@5@6# int ncount;//ÓÃÀ´¼Ç¼'@'×Ö·ûµÄ¸öÊý£¬ÒòΪ·þÎñÆ÷¶Ï·µ»ØµÄ¸ñʽΪ1@2@3#4@5@6# int j;//jÓÃÀ´¼ÇÊýµÄ wchar_t *q; char recbuf[1000]; char sendBuf[400]; CString identify; SOCKADDR_IN addr; addr.sin_family = AF_INET; addr.sin_port = htons(10000); WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,host,-1,ansiRemoteHost,wcslen(host),NULL,NULL); addr.sin_addr.s_addr = inet_addr(ansiRemoteHost); err = connect(sock,(struct sockaddr *)&addr,sizeof(addr)); //err_code = WSAGetLastError(); // CString test; //test.Format(_T("%d"),err_code); //AfxMessageBox(test);
if (err == SOCKET_ERROR) { AfxMessageBox(_T("Connect Server error!~")); return ; } if (m_condition == _T("CusName")) { strSql = _T("CusQue:select * from clienttbl where ClientName = '") + m_result + _T("'&"); }
if (m_condition == _T("CusID")) { strSql = _T("CusQue:select * from clienttbl where IdCard = '") + m_result + _T("'&"); }
if (m_condition == _T("DiningStatus")) { strSql = _T("CusQue:select * from clienttbl where DiningStatus = '") + m_result + _T("'&"); }
//AfxMessageBox(strSql); WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strSql,-1,sendBuf,wcslen(strSql),NULL,NULL); send(sock,sendBuf,sizeof(sendBuf),0); recv(sock,recbuf,sizeof(recbuf),0);
wchar_t *pwText; DWORD dwNum = MultiByteToWideChar(CP_ACP,0,recbuf,-1,NULL,0); pwText = new wchar_t[dwNum*2]; mbstowcs(pwText,recbuf,dwNum*2); closesocket(sock); if(0 == wcscmp(pwText,_T("false"))) { AfxMessageBox (_T("Sorry!~,No data Search")); return; }
/*ÏÂÃæС¶Î´úÂëÓÃÀ´²âÊÔpwTextÖÐ'#'µÄ¸öÊý*/ wchar_t *p = pwText; count = 0; ncount = 0; //int count;//ÓÃÀ´¼Ç¼'#'×Ö·ûµÄ¸öÊý£¬ÒòΪ·þÎñÆ÷¶Ï·µ»ØµÄ¸ñʽΪ1@2@3#4@5@6# //int ncount;//ÓÃÀ´¼Ç¼'@'×Ö·ûµÄ¸öÊý£¬ÒòΪ·þÎñÆ÷¶Ï·µ»ØµÄ¸ñʽΪ1@2@3#4@5@6# for(int i = 0 ;i < wcslen(pwText);i++,p++) { if(*p == _T('#')) count++; }
for(i = 0; i < count; i++) { q = wcstok(pwText,_T("#")); pwText = wcstok(NULL,_T("")); j = 0; while(q) { m_list.InsertItem(i,_T("")); p = wcstok(q,_T("@")); q = wcstok(NULL,_T("")); while(p) { m_list.SetItemText(i,j,p); p = wcstok(q,_T("@")); q = wcstok(NULL,_T("")); j++; } }
} delete pwText; return;
}
服务器端:SOCKET部分相关代码:
char* splitString(char *buff)//·ÖÀë×Ö·û´® { char *p; char *head; head = strtok(buff,":"); p = strtok(NULL,""); head = strtok(p,"&"); return head; }
BOOL ExecSql(char *sql) { int n = 0; try { CoInitialize(NULL);//ÕâÀïΪʲôһ¶¨Òª³õʼ»¯»·¾³£¬Ç°ÃæÒѾ³õʼ»¯ÁË°¡£¿ _ConnectionPtr m_pcon; _RecordsetPtr m_Rs; _CommandPtr m_C; m_pcon.CreateInstance(_uuidof(Connection)); m_pcon->Open("OrderDishSysOdbc","sa","",NULL); m_C.CreateInstance(_uuidof(Command)); m_C->ActiveConnection = m_pcon; m_C->CommandText= _bstr_t(sql); m_C->CommandType=adCmdText; m_C->Parameters->Refresh(); //m_Rs = m_C->Execute(NULL,NULL,adCmdUnknown); n = m_C->Execute(NULL,NULL,adCmdUnknown); } catch(_com_error e) // [*]---->[&] { //AfxMessageBox("¸üÐÂÊý¾Ý¿âʧ°Ü~"); return FALSE; }
if (0 == n) { return FALSE; } return TRUE; }
BOOL QuerySql(char *sql,char* &result,char *head) { CoInitialize(NULL);//ÕâÀïΪʲôһ¶¨Òª³õʼ»¯»·¾³£¬Ç°ÃæÒѾ³õʼ»¯ÁË°¡£¿ _ConnectionPtr m_CP; _RecordsetPtr m_RS;
_variant_t vFieldValue; CString strFieldValue; char tmp[500]; memset(tmp,0,sizeof(tmp)); int AllCount = 0; try { m_CP.CreateInstance(_uuidof(Connection)); if(m_CP->GetState() != adStateOpen) { m_CP->Open("OrderDishSysOdbc","sa","",NULL); } m_RS.CreateInstance(_uuidof(Recordset)); if(m_RS->State != adStateOpen) { //Ҫͨ¹ým_R->GetRecordCount()»ñÈ¡¼Ç¼µÄ×ÜÌõÊý£¬±ØÐëÉèÖÃÓαêµÄÊôÐÔ£¬²¢ÇÒÒªÓÃmovenext()±éÀúÊý¾Ý¿â²ÅÄÜ»ñÈ¡µ½×ܼǼÊý m_RS->CursorType = adOpenStatic; m_RS->CursorLocation = adUseClient; m_RS->Open(sql,m_CP.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); } if(VARIANT_FALSE != m_RS->FirstOfFile) { return FALSE; } m_RS->MoveNext(); while(VARIANT_FALSE == m_RS->EndOfFile) { m_RS->MoveNext(); } // m_P->MoveNext(); AllCount = m_RS->GetRecordCount(); m_RS->MoveFirst(); for(int count = 0; count < m_RS->GetRecordCount(); count++) { if(VARIANT_FALSE == m_RS->EndOfFile) { //¸ù¾ÝÍ·²¿°üµÄ±êʾ²»Í¬À´·µ»Ø²»Í¬µÄ½á¹û if(0 == strcmp(head,"Load")) { return TRUE;//˵Ã÷¸ÃÓû§ÃûºÍÃÜÂ붼ÕýÈ· } if(0 == strcmp(head,"CusQue")) { vFieldValue = m_RS->GetCollect("TableNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("ClientName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Sex"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Age"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("IdCard"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("DiningBeginTime"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("DiningStatus"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); } if(0 == strcmp(head,"TbQue")) { vFieldValue = m_RS->GetCollect("TableNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("TableName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("HoldMax"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Statue"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("RepastTime"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("IsBooking"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("BookingTime"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); } if(0 == strcmp(head,"TbBook")) { vFieldValue = m_RS->GetCollect("TableNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("TableName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("HoldMax"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); } if(0 == strcmp(head,"TBARRA")) { vFieldValue = m_RS->GetCollect("TableNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("TableName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("HoldMax"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); }
if(0 == strcmp(head,"TableNo")) { vFieldValue = m_RS->GetCollect("TableNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#"); vFieldValue.Clear(); }
if(0 == strcmp(head,"findFoodMaker")) { vFieldValue = m_RS->GetCollect("FoodMaker"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); vFieldValue.Clear(); } if(0 == strcmp(head,"findFoodMakerId")) { vFieldValue = m_RS->GetCollect("Chefno"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); vFieldValue.Clear(); }
if(0 == strcmp(head,"OrderFood")) { vFieldValue = m_RS->GetCollect("FoodNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodCls"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("FoodName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("Price"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("IsSpecial"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); }
if(0 == strcmp(head,"FoodDetail")) { vFieldValue = m_RS->GetCollect("FoodNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodCls"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("FoodConstitute"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("Nutrition"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Price"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("IsSpecial"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("FoodMaker"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#");//¸÷Ìõ¼Ç¼֮¼äÓÃ"#"ºÅ¸ô¿ª vFieldValue.Clear(); }
if(0 == strcmp(head,"Ordered")) { vFieldValue = m_RS->GetCollect("FoodNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodCls"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Price"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#"); vFieldValue.Clear(); }
if(0 == strcmp(head,"Servered")) { vFieldValue = m_RS->GetCollect("FoodNo"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodCls"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("FoodName"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear(); vFieldValue = m_RS->GetCollect("Price"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"@"); vFieldValue.Clear();
vFieldValue = m_RS->GetCollect("ServingTime"); strFieldValue = (char *)_bstr_t(vFieldValue); strcat(tmp,strFieldValue); strcat(tmp,"#"); vFieldValue.Clear(); }
} m_RS->MoveNext(); } } catch (_com_error e) { //AfxMessageBox("²éѯʧ°Ü~~£¡"); return FALSE; } result = tmp; m_RS->Close(); m_CP->Close(); CoUninitialize(); return TRUE; } void DealRequest(void *s) { BOOL m_bool; SOCKET m_sock = (SOCKET)s; char buff[500]; char *sql; char *p; char result[500]; //char sendbuff[200];
recv(m_sock,buff,sizeof(buff),0); sql = splitString(buff); //m_bool = execSql(sql,p,buff); // strcpy(p,result); if(0 == strcmp(buff,"Load"))//Ö´Ðеǽ²Ù×÷ { m_bool = QuerySql(sql,p,buff); memset(result,0,sizeof(result)); if(m_bool) { strcpy(result,"true"); // memcpy("true",result,5); } else { strcpy(result,"false"); // memcpy("false",result,6); } send(m_sock,result,strlen(result)+1,0); }
if(0 == strcmp(buff,"CusReg"))//Ö´Ðпͻ§µÇ¼Ç { char *p; p = strtok(sql,"@"); while(p) { m_bool = ExecSql(p); if (!m_bool) { break; } sql = strtok(NULL,""); p=strtok(sql,"@"); }
if(m_bool) { strcpy(result,"true"); // memcpy("true",result,5); } else { strcpy(result,"false"); // memcpy("false",result,6); } send(m_sock,result,strlen(result)+1,0);
}
if(0 == strcmp(buff,"entiApp"))//Ö´Ðл·¾³ÆÀ¼Û { m_bool = ExecSql(sql); if(m_bool) { strcpy(result,"true"); // memcpy("true",result,5); } else { strcpy(result,"false"); // memcpy("false",result,6); } send(m_sock,result,strlen(result)+1,0); }
if(0 == strcmp(buff,"foodApp"))//Ö´ÐÐÆÀ¼Û { m_bool = ExecSql(sql); if(m_bool) { strcpy(result,"true"); // memcpy("true",result,5); } else { strcpy(result,"false"); // memcpy("false",result,6); } send(m_sock,result,strlen(result)+1,0); }
if(0 == strcmp(buff,"serApp"))//Ö´ÐзþÎñÆÀ¼Û { m_bool = ExecSql(sql); if(m_bool) { strcpy(result,"true"); // memcpy("true",result,5); } else { strcpy(result,"false"); // memcpy("false",result,6); } send(m_sock,result,strlen(result)+1,0); }
if(0 == strcmp(buff,"CusQue"))//Ö´Ðпͻ§²éѯ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"TbQue"))//Ö´ÐвÍ×À²éѯ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"TbBook")) { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"TbBook2"))//Ö´ÐÐÔ¤¶¨²Ù×÷ { char *p; p = strtok(sql,"@"); while(p) { m_bool = ExecSql(p); if (!m_bool) { break; } sql = strtok(NULL,""); p=strtok(sql,"@"); }
if(m_bool) { strcpy(result,"true"); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"CurrentReg"))//Ö´ÐÐÕýʽµÇ¼Ç²Ù×÷ { char *p; p = strtok(sql,"@"); while(p) { m_bool = ExecSql(p); if (!m_bool) { break; } sql = strtok(NULL,""); p=strtok(sql,"@"); } if(m_bool) { strcpy(result,"true"); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"TBARRA"))//Ö´ÐвÍ×À°²ÅŲÙ×÷ { char *p; p = strtok(sql,"@"); while(p) { m_bool = ExecSql(p); if (!m_bool) { break; } sql = strtok(NULL,""); p=strtok(sql,"@"); } if(m_bool) { strcpy(result,"true"); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"TableNo"))//ÈÃÊý¾Ý¿âÖеIJÍ×À±àÂëÌî³äµ½¿Ø¼þÖÐ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"OrderFoodInit"))//Ö´Ðеã²Ë²Ù×÷£¬·µ»Ø²ËʽÐÅÏ¢±íÖеÄËùÓÐÊý¾ÝÒÔ¼°½«²Í×ÀÐÅÏ¢±íÖеIJÍ×ÀºÅÈ«²¿ËÑË÷³öÀ´£¬ÌîÈ˵½·þÎñÆ÷¿Ø¼þÖÐ { char *q; q = strtok(sql,"@"); m_bool = QuerySql(q,p,"OrderFood"); sql = strtok(NULL,""); strcat(p,"|"); strcpy(result,p); m_bool = QuerySql(sql,p,"TableNo"); strcat(result,p); if(!m_bool) { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"FoodDetail"))//Ö´Ðеã²Ë²Ù×÷£¬·µ»Ø²ËʽÐÅÏ¢±íÖеÄijһÖÖ²Ëʽ±àÂëµÄÈ«²¿ÐÅÏ¢ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"OrderFood"))//ÈÃÊý¾Ý¿âÖеIJÍ×À±àÂëÌî³äµ½Áбí¿Ø¼þÖÐ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"ExecOrder"))//Ö´Ðеã²Ë²Ù×÷,ÔÚÕâÒ»²½ÖУ¬ÒªÍ¨¹ý²Ëʽ±àÂë²éÕÒµ½³øʦÃû³Æ£¬ÔÙ¸ù¾Ý³øʦÃû³Æ²éÕÒµ½³øʦ±àÂ룬ÔÚÖ´ÐвåÈë²Ù×÷ { char *p; char tmpsql[200] = "select FoodMaker from FoodInfoTbl where FoodNo = '"; char *tmp; p = strtok(sql,"@"); strcat(tmpsql,p); strcat(tmpsql,"'"); m_bool = QuerySql(tmpsql,tmp,"findFoodMaker"); if (0 != strcmp(tmp,"")) { memset(tmpsql,0,sizeof(tmpsql)); strcpy(tmpsql,"select ChefNo from ChefLoadTbl where ChefName like '%"); strcat(tmpsql,tmp); strcat(tmpsql,"%'"); tmp = NULL; m_bool = QuerySql(tmpsql,tmp,"findFoodMakerId"); memset(tmpsql,0,sizeof(tmpsql)); sql = strtok(NULL,""); strcpy(tmpsql,sql); strcat(tmpsql,tmp); strcat(tmpsql,"')"); //strSql1 = _T("insert into values('") + m_tableno + _T("','") + strFoodNo + _T("','0','','"); } else { sql = strtok(NULL,""); strcpy(tmpsql,sql); strcat(tmpsql,"')"); } m_bool = ExecSql(tmpsql); if(m_bool) { strcpy(result,"true"); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; } if(0 == strcmp(buff,"Ordered"))//¸ù¾ÝÑ¡Ôñ²Í×ÀºÅÀ´²éѯÒѾµãÁ˵IJˣ¬ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
if(0 == strcmp(buff,"Servered"))//¸ù¾ÝÑ¡Ôñ²Í×ÀºÅÀ´²éѯÒѾÉÏÁ˵IJˣ¬ { m_bool = QuerySql(sql,p,buff); if(m_bool) { strcpy(result,p); } else { strcpy(result,"false"); } //AfxMessageBox(result); send(m_sock,result,strlen(result)+1,0); p = NULL; }
return; }
ULONG WINAPI DealSocketProcess() { SOCKET m_sock; //int err; sockaddr_in address; int rtnbind; memset(&address,0,sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(10000); address.sin_addr.s_addr = inet_addr("192.168.0.88"); m_sock = socket(AF_INET,SOCK_STREAM,0); rtnbind = bind(m_sock,(struct sockaddr *)&address,sizeof(address)); if(0 != rtnbind) { AfxMessageBox("°ó¶¨Ê§°Ü"); //PostQuitMessage(0); exit(0); }
while(!listen(m_sock,5)) { SOCKET m_sockClient; sockaddr_in addrClient; WORD ThreadChildId; int len; //CString str = "hehe"; len = sizeof(addrClient); memset(&addrClient,0,sizeof(addrClient)); m_sockClient = accept(m_sock,(sockaddr *)&addrClient,&len); //recv(m_sockClient,buff,sizeof(buff) + 1,0); // AfxMessageBox(buff); // send(m_sockClient,str,str.GetLength() + 1,0); hThreadChild = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DealRequest,(void *)m_sockClient,0,(LPDWORD)&ThreadChildId); } return 0; }
BOOL CServerProgrameApp::InitInstance() { if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; }
AfxEnableControlContainer();
// Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.
#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
// Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CServerProgrameDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CServerProgrameView)); AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE;
// The one and only window has been initialized, so show and update it. //m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED); m_pMainWnd->UpdateWindow(); m_pMainWnd->SetWindowText("ÎÞÏß²ÍÒûµã²Ëϵͳ·þÎñÆ÷¶Ë"); // CString sql; /************************************************************************/ /* ³õʼ»¯Êý¾Ý¿â»·¾³£¬ */ /************************************************************************/ ::CoInitialize(NULL); DWORD ThreadId;
hThread = ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DealSocketProcess,0,0,&ThreadId); return TRUE; }