adsl能建聊天程序吗
我用的是网通的ADSL宽带上网,想编个聊天程序,在Internet网上用,不知能行吗?怎么实现?
BOOL CChatDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here InitSocket();//套接字创建绑定初始化 RECVPARAM *pRecvParam=new RECVPARAM; pRecvParam->sock=m_socket; pRecvParam->hwnd=m_hWnd; //创建接收线程 HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL); //关闭该接收线程句柄,释放其引用计数 CloseHandle(hThread); //设置默认服务器地址 SetDlgItemText(IDC_SEV_ADDR,"sosh.vicp.cc"); return TRUE; // return TRUE unlesBOOL CChatDlg::InitSocket() { //创建套接字 m_socket=socket(AF_INET,SOCK_DGRAM,0); if (INVALID_SOCKET==m_socket) { MessageBox("套接字创建失败!"); 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; //绑定套接字 retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR)); if (SOCKET_ERROR==retval) { closesocket(m_socket); MessageBox("绑定失败!"); return FALSE; } return TRUE; } DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter) { // 获取主线程传递的套接字和窗口句柄 SOCKET sock=((RECVPARAM*)lpParameter)->sock; HWND hwnd=((RECVPARAM*)lpParameter)->hwnd; SOCKADDR_IN addrFrom; int len=sizeof(SOCKADDR); char recvBuf[1000]; char tempBuf[2000]; int retval; while (TRUE) { //接收数据 retval=recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len); if (SOCKET_ERROR==retval) { break; } CString ipstr=inet_ntoa(addrFrom.sin_addr); if (ipstr=="127.0.0.1") { sprintf(tempBuf,"我 说: %s",recvBuf); } else { sprintf(tempBuf,"%s 说: %s",inet_ntoa(addrFrom.sin_addr),recvBuf); } ::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf); } return 0; } void CChatDlg::OnRecvData(WPARAM wParam,LPARAM lParam) { //取出接收到的数据 CString str=(char*)lParam; CString strTemp; //获取当前系统时间 ctime=CTime::GetCurrentTime(); //格式化获取的时间 strtime=ctime.Format("发送时间(%H:%M:%S)"); //获得已有数据 GetDlgItemText(IDC_EDIT_RECV,strTemp); /* str+="\r\n"; str+=strTemp; */ strTemp+=strtime; strTemp+="\r\n"; strTemp+=str; strTemp+="\r\n\r\n"; //显示所有接收到的数据 SetDlgItemText(IDC_EDIT_RECV,strTemp); //编辑框滚动条控制 CEdit* pEdit = (CEdit*)this->GetDlgItem(IDC_EDIT_RECV); if (pEdit != NULL) { pEdit->LineScroll(pEdit->GetLineCount()); } } void CChatDlg::OnBtnSend() { // TODO: Add your control notification handler code here //获取对方IP /* DWORD dwIP; ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->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); //发送数据 sendto(m_socket,strSend,strSend.GetLength()+1,0, (SOCKADDR*)&addrTo,sizeof(SOCKADDR)); sendto(m_socket,strSend,strSend.GetLength()+1,0, (SOCKADDR*)&addrToMe,sizeof(SOCKADDR)); //清空发送编辑框中的内容 SetDlgItemText(IDC_EDIT_SEND," "); GetDlgItem(IDC_EDIT_SEND)->SetFocus(); } void CChatDlg::OnServerl() { // TODO: Add your control notification handler code here CString Sertext,Ipaddr; GetDlgItemText(IDC_SERVERL,Sertext); if (Sertext=="登陆服务器[未连接]")//登陆服务器获取服务器IP { CString strServerlName; HOSTENT* pHost; addrTo.sin_family=AF_INET; addrToMe.sin_family=AF_INET; addrTo.sin_port=htons(6000); addrToMe.sin_port=htons(6000); GetDlgItemText(IDC_SEV_ADDR,strServerlName); pHost=gethostbyname(strServerlName); addrTo.sin_addr.S_un.S_addr=*((DWORD*)pHost->h_addr_list[0]); addrToMe.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); SetDlgItemText(IDC_SERVERL,"服务器[已经连接]"); } else { MessageBox("服务器已经重新链接!"); CString strServerlName; HOSTENT* pHost; addrTo.sin_family=AF_INET; addrTo.sin_port=htons(6000); GetDlgItemText(IDC_SEV_ADDR,strServerlName); pHost=gethostbyname(strServerlName); addrTo.sin_addr.S_un.S_addr=*((DWORD*)pHost->h_addr_list[0]); if (strServerlName=="sosh.vicp.cc") { SetDlgItemText(IDC_SERVERL,"服务器[已经连接]"); } else { SetDlgItemText(IDC_SERVERL,"目标地址[已经连接]"); } } }s you set the focus to a control