超强的图形界面聊天软件.rar

源代码在线查看: connectdialog.cpp

软件大小: 463 K
上传用户: wanghao891207
关键词: 图形 软件
下载地址: 免注册下载 普通下载 VIP

相关代码

				// ConnectDialog.cpp : implementation file
				//
				
				#include "stdafx.h"
				#include "pictalk.h"
				#include "ConnectDialog.h"
				
				#ifdef _DEBUG
				#define new DEBUG_NEW
				#undef THIS_FILE
				static char THIS_FILE[] = __FILE__;
				#endif
				
				/////////////////////////////////////////////////////////////////////////////
				// CConnectDialog dialog
				
				
				CConnectDialog::CConnectDialog(CWnd* pParent /*=NULL*/)
					: CDialog(CConnectDialog::IDD, pParent)
				{
					//{{AFX_DATA_INIT(CConnectDialog)
					m_PortAddress = 700;
					m_ServerName = _T("");
					m_strSelSer = _T("");
					//}}AFX_DATA_INIT
				}
				
				
				void CConnectDialog::DoDataExchange(CDataExchange* pDX)
				{
					CDialog::DoDataExchange(pDX);
					//{{AFX_DATA_MAP(CConnectDialog)
					DDX_Control(pDX, IDC_COMBO1, m_ComSer);
					DDX_Control(pDX, IDC_ROOM_LIST, m_RoomList);
					DDX_Text(pDX, IDC_PORT, m_PortAddress);
					DDX_Text(pDX, IDC_SERVER, m_ServerName);
					DDX_CBString(pDX, IDC_COMBO1, m_strSelSer);
					//}}AFX_DATA_MAP
				}
				
				
				BEGIN_MESSAGE_MAP(CConnectDialog, CDialog)
					//{{AFX_MSG_MAP(CConnectDialog)
					ON_BN_CLICKED(IDC_GETROOM_WEB, OnGetroomWeb)
					ON_THREAD_MESSAGE(ID_ACCEPT_SERVER,OnAcceptServer)
					ON_NOTIFY(NM_CLICK, IDC_ROOM_LIST, OnClickRoomList)
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CConnectDialog message handlers
				
				void CConnectDialog::OnGetroomWeb() 
				{
					// TODO: Add your control notification handler code here
					UpdateData(TRUE);
					g_strServerName1=m_WebList[m_ComSer.GetCurSel()].m_Web;
					g_strGetFile=m_WebList[m_ComSer.GetCurSel()].m_get;
					AfxBeginThread(GetRoomWinInetThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
				}
				void CConnectDialog::OnAcceptServer(UINT nID, LPARAM param)
				{
					m_cs.Lock();
					int i,j;
					int JustnowItem;
					j=SerArray.GetSize();
					for(i=0;i					{
						JustnowItem=m_RoomList.InsertItem(0,SerArray[0].m_RoomName);
						m_RoomList.SetItemText(JustnowItem,1,SerArray[0].m_ServerName);
						m_RoomList.SetItemText(JustnowItem,2,SerArray[0].m_ServerPort);
						SerArray.RemoveAt(0);
						UpdateData(FALSE);
					}
					m_cs.Unlock();
				}
				
				CString g_strGetFile="getroom.asp";
				CString g_strServerName1="www.devie.com";
				CString g_strServerIP1="";
				volatile UINT g_nPort1 = 80;
				
				CArray SerArray;
				
				UINT GetRoomWinInetThreadProc(LPVOID pParam)
				{
					CInternetSession session;
					CHttpConnection* pConnection = NULL;
					CHttpFile* pFile1 = NULL;
					char* buffer = new char[2];
					UINT nBytesRead = 0;
					DWORD dwStatus;
					CString g_str;
					try {
						// username/password doesn't work yet
						if(!g_strServerName1.IsEmpty()) {
							pConnection = session.GetHttpConnection(g_strServerName1,
									(INTERNET_PORT) g_nPort1);
						}
						else {
							pConnection = session.GetHttpConnection(g_strServerIP1,
									(INTERNET_PORT) g_nPort1);
						}
						pFile1 = pConnection->OpenRequest(0, g_strGetFile, NULL, 1, NULL, NULL, // GET request
								INTERNET_FLAG_RELOAD); // needed for NT Challenge/Response authentication
								// INTERNET_FLAG_RELOAD forces reload from the server (bypasses client's cache)
						pFile1->SendRequest();
						pFile1->QueryInfoStatusCode(dwStatus);
						TRACE("QueryInfoStatusCode = %d\n", dwStatus);
						while(1)
						{
							g_str="";
							while(1)
							{
								nBytesRead = pFile1->Read(buffer, 1);
								buffer[nBytesRead] = '\0';
								if(buffer[0]==13)
									break;
								g_str+=buffer[0];
							}
							if(g_str=="endtrans")
								break;
							if(g_str!=CString("headtou"))
								AfxMessageBox("主服务器不可用",MB_OK);
							
							serverInfo sf;
							g_str="";
							while(1)
							{
								nBytesRead = pFile1->Read(buffer, 1);
								buffer[nBytesRead] = '\0';
								if(buffer[0]==13)
									break;
								g_str+=buffer[0];
							}
							if(g_str=="endtrans")
								break;
							sf.m_ServerName=g_str;
				
							g_str="";
							while(1)
							{
								nBytesRead = pFile1->Read(buffer, 1);
								buffer[nBytesRead] = '\0';
								if(buffer[0]==13)
									break;
								g_str+=buffer[0];
							}
							if(g_str=="endtrans")
								break;
							sf.m_ServerPort=g_str;
							
							g_str="";
							while(1)
							{
								nBytesRead = pFile1->Read(buffer, 1);
								buffer[nBytesRead] = '\0';
								if(buffer[0]==13)
									break;
								g_str+=buffer[0];
							}
							if(g_str=="endtrans")
								break;
							sf.m_RoomName=g_str;
				
							g_str="";
							while(1)
							{
								nBytesRead = pFile1->Read(buffer, 1);
								buffer[nBytesRead] = '\0';
								if(buffer[0]==13)
									break;
								g_str+=buffer[0];
							}
							if(g_str=="endtrans")
								break;
							if(g_str!=CString("tailwei"))
								AfxMessageBox("主服务器不可用",MB_OK);
							SerArray.Add(sf);
				
							::PostMessage((HWND)pParam,ID_ACCEPT_SERVER,0,0);
						}
						// could use existing pFile1 to SendRequest again if we wanted to
					}
					catch(CInternetException* e) {
				//		LogInternetException(pParam, e);
						e->Delete();
					}
					// could call OpenRequest again on same connection if we wanted to 
					if(pFile1) delete pFile1; // does the close -- prints a warning
					if(pConnection) delete pConnection; // why does it print a warning?
					delete [] buffer;
					return 0;
				}
				
				BOOL CConnectDialog::OnInitDialog() 
				{
					CDialog::OnInitDialog();
					
					// TODO: Add extra initialization here
					m_RoomList.InsertColumn(0,"聊天室名",LVCFMT_LEFT,100,-1);
					m_RoomList.InsertColumn(1,"地址",LVCFMT_LEFT,160,-1);
					m_RoomList.InsertColumn(2,"端口",LVCFMT_LEFT,80,-1);
					
					CString filename="web.txt";
					CStdioFile file;
					if(file.Open(filename,CFile::modeRead)==0){
						AfxMessageBox("读取文件失败",MB_OK);
					}
					CString str;
					while(1){
						WebServer web;
						file.ReadString(web.m_Webname);
						if(web.m_Webname=="")
							break;
						file.ReadString(web.m_Web);
						file.ReadString(web.m_show);
						file.ReadString(web.m_get);
						m_WebList.Add(web);
					}
					file.Close();
				
					int i;
					for(i=0;i						m_ComSer.AddString(m_WebList[i].m_Webname);
					m_ComSer.SetCurSel(0);
					return TRUE;  // return TRUE unless you set the focus to a control
					              // EXCEPTION: OCX Property Pages should return FALSE
				}
				
				void CConnectDialog::OnClickRoomList(NMHDR* pNMHDR, LRESULT* pResult) 
				{
					// TODO: Add your control notification handler code here
					UpdateData(TRUE);
					POSITION pos = m_RoomList.GetFirstSelectedItemPosition();
					if (pos != NULL)
					{
						int nItem = m_RoomList.GetNextSelectedItem(pos);
						CString str;
						m_ServerName=m_RoomList.GetItemText(nItem,1);
						str=m_RoomList.GetItemText(nItem,2);
						unsigned un;
						sscanf(str,"%u",&un);
						m_PortAddress=(UINT)un;
					}
					UpdateData(FALSE);
					*pResult = 0;
				}
							

相关资源