文件名称:基于VC++的简单聊天程序
文件大小:29.07MB
文件格式:RAR
更新时间:2018-01-19 06:02:23
c++ socket 线程 聊天程序
基于对话框的聊天程序代码: 1.加载套接字库: 在InitInstance函数中 if (!AfxSocketInit()) { AfxMessageBox(L"load socket lib error!"); return FALSE; } 2.初始化套接字 BOOL CChatDlg::InitSocket() { m_socket = socket(AF_INET, SOCK_DGRAM, 0); if (INVALID_SOCKET == m_socket) { MessageBox(L"套接字创建失败!"); return FALSE; } //创建套接字 SOCKADDR_IN addrSock; addrSock.sin_family = AF_INET; addrSock.sin_port = htons(6000); addrSock.sin_addr.S_un.S_addr = htonl(INADDR_ANY); int retval = bind(m_socket, (SOCKADDR*)&addrSock, sizeof(SOCKADDR)); if (SOCKET_ERROR == retval) { closesocket(m_socket); MessageBox(L"绑定失败!"); return FALSE; } return TRUE; } 3.创建线程来接收信息 //线程的创建 RecvParam* pRecvParam = new RecvParam; pRecvParam->sock = m_socket; pRecvParam->hwnd = m_hWnd; pRecvParam->hMutex = m_hMutex; HANDLE hThread = ::CreateThread(NULL, 0, RecvFunc, (LPVOID)pRecvParam, 0 , 0); CloseHandle(hThread); 线程接收函数 DWORD WINAPI RecvFunc(LPVOID lpParam)// 线程函数 实现数据接收 { SOCKET sock = ((RecvParam*)lpParam)->sock; HWND hwnd = ((RecvParam*)lpParam)->hwnd; HANDLE hMutex = ((RecvParam*)lpParam)->hMutex; delete lpParam; SOCKADDR_IN addrFrom; int len = sizeof(SOCKADDR); char recvBuf[200]; char tempBuf[300]; int retval; while(TRUE) { WaitForSingleObject(hMutex,INFINITE); retval = recvfrom(sock, recvBuf, 200, 0,(SOCKADDR*)&addrFrom, &len); if (SOCKET_ERROR == retval) { break; } sprintf_s(tempBuf, ("%s 说: %s"), inet_ntoa(addrFrom.sin_addr), recvBuf); ::PostMessage(hwnd, WM_RECVDATA,(WPARAM)tempBuf, NULL); ReleaseMutex(hMutex); } return 0; } 4. 消息的发送与接收显示 void CChatDlg::OnBnClickedButtonSend() { // TODO: Add your control notification handler code here //获取对方IP CIPAddressCtrl* pIPAddress = ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_SEND)); if (pIPAddress->IsBlank()) { MessageBox(L"请先输入对方IP地址!"); return; } DWORD dwIP; pIPAddress->GetAddress(dwIP); SOCKADDR_IN addrTo; addrTo.sin_family = AF_INET; addrTo.sin_port = htons(6000); addrTo.sin_addr.S_un.S_addr = htonl(dwIP); CString strSend; GetDlgItemText(IDC_Edit_Send, strSend); int len =WideCharToMultiByte(CP_ACP,0,strSend,-1,NULL,0,NULL,NULL); char *ptxtTemp =new char[len +1]; WideCharToMultiByte(CP_ACP,0,strSend,-1,ptxtTemp,len,NULL,NULL ); //发送数据 sendto(m_socket, ptxtTemp , strlen(ptxtTemp) + 1, 0, (SOCKADDR*)&addrTo, sizeof(SOCKADDR)); delete[] ptxtTemp; if (m_chatHeight++ >= 12) { m_chatContent.SetWindowText(L""); m_chatHeight = 0; } //发送我说的内容 WaitForSingleObject(m_hMutex, INFINITE); CString strTemp(""); m_chatContent.GetWindowText(strTemp); CString strMyContent = L"我说:" + strSend; if (strTemp != "") { strTemp += "\r\n"; } strTemp += strMyContent; m_chatContent.SetWindowText(strTemp); ReleaseMutex(m_hMutex); SetDlgItemText(IDC_Edit_Send, L""); } 代码可能粘贴不完整
【文件预览】:
Chat
----Release()
--------Chat.pdb(4.06MB)
--------Chat.exe(87KB)
----Chat.sln(878B)
----Chat()
--------stdafx.cpp(206B)
--------Chat.vcproj(5KB)
--------Release()
--------Chat.aps(102KB)
--------res()
--------stdafx.h(2KB)
--------ChatDlg.h(947B)
--------Chat.cpp(2KB)
--------targetver.h(1KB)
--------Chat.vcproj.TCGROUP.leiweijun.user(1KB)
--------resource.h(722B)
--------Chat.h(501B)
--------Debug()
--------ChatDlg.cpp(6KB)
--------ReadMe.txt(4KB)
--------Chat.rc(5KB)
----Debug()
--------Chat.pdb(4.49MB)
--------Chat.exe(154KB)
--------Chat.ilk(1.04MB)
----Chat.suo(27KB)
----Chat.ncb(28.62MB)