void CClientSocket::UpdateUserOline(CString strUserInfo,char from[20]) { HEADER head; head.type = MSG_UPDATE; head.SendLen = strUserInfo.GetLength()*2; memset(head.from, 0, sizeof(head.from)); memset(head.beizhu,0,sizeof(head.beizhu)); memset(head.to,0,sizeof(head.to)); strcpy(head.from,from); char *pSend = new char[head.SendLen]; memset(pSend, 0, head.SendLen); if( !WChar2MByte(strUserInfo.GetBuffer(0), pSend, head.SendLen)) { AfxMessageBox(_T("用户全部退出!")); delete pSend; return; } POSITION pss = m_List->GetHeadPosition(); //循环对客户端发送消息 while(pss!=NULL) { CClientSocket* pTemp = (CClientSocket*)m_List->GetNext(pss); //发送协议头 pTemp->Send((char*)&head, sizeof(head)); pTemp->Send(pSend,head.SendLen ); } delete pSend; }
int main() { CClientSocket Client; // Create the socket to communicate with the Server if (!Client.Create(AF_INET, SOCK_STREAM)) { cout << "Failed to create socket\n" ; return 0; } // Connect to the server if (SOCKET_ERROR == Client.Connect("127.0.0.1", "3000")) { cout << "Failed to connect to server. Was it running?\n"; return 0; } cout << "Connected to server.\n"; cout << "Type data to send, type quit to exit\n"; // Monitor the client socket for network events, such as data ready to receive Client.StartEvents(); // Send data to the server string s; for (;;) { // Infinite loop getline(cin, s); if (s == "quit") break; int i = Client.Send(s.c_str(), (int)s.length(), 0); cout << "Sending " << i << " characters\n"; } return 0; }
void CClientSocket::MSGTranslate(char* buff,int nlen,char to[20],char from[20],char beizhu[20]) { HEADER head; head.type = MSG_SEND; head.SendLen = nlen; strcpy(head.to,to); strcpy(head.from,from); memset(head.beizhu,0,20); POSITION ps = m_List->GetHeadPosition(); //取得,所有用户的队列 CString str(buff); int i = strcmp(head.to,"群聊"); while(ps!=NULL) { CClientSocket* pTemp = (CClientSocket*)m_List->GetNext(ps); //实现群聊和一对一关键就在于此 if(pTemp->m_strName==head.to || pTemp->m_strName==head.from || i==0 ) { pTemp->Send(&head,sizeof(HEADER)); //先发送头部 pTemp->Send(buff, nlen); //再发送内容 } } }
void CScheduleDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CString strAGCNo, strTargetNo, strOpt; m_comboAGCNo.GetWindowTextW(strAGCNo); m_comboTargetNo.GetWindowTextW(strTargetNo); m_comboOpt.GetWindowTextW(strOpt); if (strAGCNo.IsEmpty() || strTargetNo.IsEmpty() || strOpt.IsEmpty()) { AfxMessageBox(_T("请输入车号,目标点号和操作!")); return; } // 类型转换 BYTE agvno = _ttoi(strAGCNo); UINT16 targetno = _ttoi(strTargetNo); UINT16 opt = (UINT16)m_comboOpt.GetItemData(m_comboOpt.GetCurSel()); if (++MSG_TAG > 255) MSG_TAG = 0; // 任务编号 UINT16 taskno = ++MSG_TASK; // 还要接收E1消息,其中的M6与这条tag不一样要重发 m_m6.tag = MSG_TAG; m_m6.agvno = agvno; m_m6.target = targetno; m_m2.tag = MSG_TAG; m_m2.agvno = agvno; m_m2.taskno = taskno; m_m2.taskopcode = opt; m_m1.tag = MSG_TAG; m_m1.agvno = agvno; m_m1.taskno = taskno; // 段数,可行走段号 通过Dijkstra算法计算 // 获取小车当前点号 CObList& clientList = m_pDoc->m_pListenSocket->m_clientList; POSITION pos = clientList.GetHeadPosition(); CClientSocket* pClient = NULL; while (pos) { pClient = (CClientSocket*)clientList.GetNext(pos); if (pClient->m_e1.agvno == agvno) break; } // 计算最短路径 if (!pClient) { AfxMessageBox(_T("还没用任何小车连接!")); return; } pClient->m_targetPt = targetno; Dijkstra(pClient->m_e1.curPoint, targetno); auto& vecRoute = m_pDoc->m_vecRoute; m_m1.secnum = (BYTE)vecRoute.size() - 1; // 段数 // 可行走段 auto& mapSideNo = m_pDoc->m_sideNo; //<段,段号> auto it = vecRoute.begin(); int prevSide = *it; std::advance(it, 1); int i = 0; // 控制段索引 CString strSideNo, strTemp; // 输出 for (auto it2 = it; it2 != vecRoute.end(); ++it2) { unsigned sideNo = mapSideNo[make_pair(prevSide, *it2)]; prevSide = *it2; m_m1.secno[i++] = sideNo; strTemp.Format(_T("%d-"), sideNo); strSideNo += strTemp; } if (strSideNo.IsEmpty()) strSideNo = _T("''"); Msg_M1M2M6 m1m2m6; m1m2m6.m1 = m_m1; m1m2m6.m2 = m_m2; m1m2m6.m6 = m_m6; // 写入数据库 m_AdoConn.OnInitADOConn(); CTime tm = CTime::GetCurrentTime(); CString strTm; strTm.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond()); CString str; // str.Format(_T("%d,\'%s\',%d,%d,%d,%d,%d,%d,%d,\'%s\'"), taskno, strTm, agvno, 1, pClient->m_e1.curPoint, \ targetno, MSG_TAG, opt, m_m1.secnum, strSideNo); CString sql = _T("insert into tasklist (") \ _T("taskno,starttime,agvno,priority,startPt,endPt,msgtag,taskOptCode,sidenum,sideno) ") \ _T("values(") + str + _T(")"); m_AdoConn.ExecuteSQL((_bstr_t)sql); m_AdoConn.ExitConn(); // 给小车发送M1,M2,M6消息 pClient->Send(&m1m2m6, sizeof(m1m2m6)); CDialogEx::OnOK(); }
void CClientDlg::OnBnClickedStart() { CString strAddress; CString strPort; CString strTestTimes; CString strTestInterv; CString strThreadCount; CString strThreadInterv; CString strDataLen; m_Address.GetWindowText(strAddress); m_Port.GetWindowText(strPort); m_TestTimes.GetWindowText(strTestTimes); m_TestInterv.GetWindowText(strTestInterv); m_ThreadCount.GetWindowText(strThreadCount); m_ThreadInterv.GetWindowText(strThreadInterv); m_DataLen.GetWindowText(strDataLen); m_strAddress = strAddress.Trim(); m_usPort = (USHORT)_ttoi(strPort); m_iTestTimes = _ttoi(strTestTimes); m_iTestInterv = _ttoi(strTestInterv); m_iThreadCount = _ttoi(strThreadCount); m_iThreadInterv = _ttoi(strThreadInterv); m_iDataLen = _ttoi(strDataLen); if(!CheckParams()) return; SetAppState(ST_STARTING); m_dwBeginTickCount = 0; m_dwEndTickCount = 0; m_dwTimeconsuming = 0; m_llTotalReceived = 0; m_llTotalSent = 0; m_llExpectReceived = (LONGLONG)m_iTestTimes * (LONGLONG)m_iThreadCount * (LONGLONG)m_iDataLen; m_vtClients.Clear(); m_sendBuffer.Malloc(m_iDataLen, true); for(int i = 0; i < m_iThreadCount; i++) { smart_simple_ptr<CClientSocket> pSocket = new CClientSocket(this); if(pSocket->Start(m_strAddress, m_usPort)) m_vtClients->push_back(pSocket.release()); else { ::LogClientStartFail(pSocket->GetLastError(), pSocket->GetLastErrorDesc()); m_vtClients.Clear(); m_sendBuffer.Free(); SetAppState(ST_STOPED); return; } } ::LogClientStart(m_strAddress, m_usPort); DWORD dwSendDelay = 3; CString strMsg; strMsg.Format(_T(" *** willing to send data after %d seconds ..."), dwSendDelay); ::LogMsg(strMsg); ::WaitWithMessageLoop(dwSendDelay * 1000); SetAppState(ST_STARTED); m_dwBeginTickCount = ::GetTickCount(); BOOL bTerminated = FALSE; for(int i = 0; !bTerminated && i < m_iTestTimes; i++) { for(int j = 0; !bTerminated && j < m_iThreadCount; j++) { CClientSocket* pSocket = m_vtClients[j]; if(!pSocket->Send(pSocket->GetConnectionID(), m_sendBuffer, (int)m_sendBuffer.Size())) { ::LogClientSendFail(i + 1, j + 1, pSocket->GetLastError(), pSocket->GetLastErrorDesc()); bTerminated = TRUE; break; } if(m_iThreadInterv > 0 && j + 1 < m_iThreadCount) ::WaitWithMessageLoop(m_iThreadInterv); } if(m_iTestInterv > 0 && i + 1 < m_iTestTimes) ::WaitWithMessageLoop(m_iTestInterv); } }