?? client.cpp
字號:
// Client.cpp: implementation of the CClient class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Example1.h"
#include "Client.h"
#include "Example1Dlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CClient::CClient()
{
m_hSocket =NULL;
}
CClient::~CClient()
{
}
BOOL CClient::InitAndConnet(HWND hwnd,UINT port,CString strserver)
{
m_hWnd=hwnd;
m_uPort=port;
m_strServer=strserver;
if(m_hSocket != NULL)
{
//如果原來打開這套接字先關閉
closesocket(m_hSocket);
m_hSocket = NULL;
}
if(m_hSocket == NULL)
{
//創建新的流套接字
m_hSocket = socket(AF_INET, SOCK_STREAM,0);
ASSERT(m_hSocket != NULL);
ClientInit();
}
//準備服務器的信息,這里需要指定服務器的地址
m_addr.sin_family = AF_INET;
m_addr.sin_addr.S_un.S_addr = inet_addr(m_strServer.GetBuffer(0));
m_addr.sin_port = htons(m_uPort); //改變端口號的數據格式
//這里主動連接服務器,該過程將等待一定時間
int ret = 0;
int error = 0;
ret = connect(m_hSocket, (LPSOCKADDR)&m_addr, sizeof(m_addr));
if(ret == SOCKET_ERROR)
{//連接失敗
if(GetLastError()!=WSAEWOULDBLOCK)
{
AfxMessageBox(_T("請確認服務器確實已經打開并工作在同樣的端口!"));
return FALSE;
}
}
return TRUE;
}
void CClient::SendString(CString a)
{
if(send(m_hSocket,a.GetBuffer(0),a.GetLength(),0)==SOCKET_ERROR)
{
AfxMessageBox("Client Send data error");
}
}
void CClient::GetString(CString &str)
{
recv(m_hSocket,str.GetBuffer(0),1024,MSG_DONTROUTE);
}
void CClient::ClientInit()
{
if(WSAAsyncSelect(m_hSocket,m_hWnd,CLI_MESSAGE,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
{
AfxMessageBox("Error in select");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -