// TCP_CLIENTDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TCP_CLIENT.h"
#include "TCP_CLIENTDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
struct RECVDATA
{
HWND hwnd;
SOCKET socket;
};
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCP_CLIENTDlg dialog
CTCP_CLIENTDlg::CTCP_CLIENTDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTCP_CLIENTDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCP_CLIENTDlg)
m_recv = _T("");
m_send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTCP_CLIENTDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCP_CLIENTDlg)
DDX_Control(pDX, IDC_EDIT_SEND, m_edit_send);
DDX_Text(pDX, IDC_EDIT_RECV, m_recv);
DDX_Text(pDX, IDC_EDIT_SEND, m_send);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTCP_CLIENTDlg, CDialog)
//{{AFX_MSG_MAP(CTCP_CLIENTDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_BTN_SEND, OnBtnSend)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCP_CLIENTDlg message handlers
BOOL CTCP_CLIENTDlg::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
// Init_socket();
RECVDATA *lpParameter=new RECVDATA;
lpParameter->hwnd=m_hWnd;
// lpParameter->socket=socket_client;
HANDLE handle = CreateThread(NULL,0,Recv,(LPVOID) lpParameter,0,0);
if(NULL==handle)
{
MessageBox("CreateThread error !");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTCP_CLIENTDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTCP_CLIENTDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
m_edit_send.SetFocus();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTCP_CLIENTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CTCP_CLIENTDlg::Init_socket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return FALSE;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return FALSE;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET socket_client= socket(AF_INET,SOCK_STREAM,0);
if( INVALID_SOCKET == socket_client)
{
MessageBox("socket_client error !");
return FALSE;
}
return TRUE;
}
BOOL CTCP_CLIENTDlg::OnBtnSend()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return FALSE;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return FALSE;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET socket_temp = socket(AF_INET,SOCK_STREAM,0);
if( INVALID_SOCKET == socket_temp)
{
MessageBox("socket error !");
}
SOCKADDR_IN sock_addr;
sock_addr.sin_addr.S_un.S_addr= inet_addr("127.0.0.1");
sock_addr.sin_family = AF_INET;
sock_addr.sin_port =htons(6000);
connect(socket_temp,(SOCKADDR *)&sock_addr,sizeof(SOCKADDR));
char *buf;
UpdateData(TRUE);
buf=m_send.GetBuffer(m_send.GetLength()+1);
//send(socket_temp,"我是 client",strlen("我是 client")+1,0);
send(socket_temp,buf,strlen(buf)+1,0);
closesocket(socket_temp);
WSACleanup( );
m_recv+="我说 : "+m_send;
m_recv+="\r\n";
m_send.Empty();
UpdateData(FALSE);
// m_edit_send.SetFocus();
return TRUE;
}
DWORD WINAPI CTCP_CLIENTDlg::Recv(LPVOID lpParameter)
{
/*
SOCKET socket=((RECVDATA *)lpParameter)->socket;
HWND hwnd=((RECVDATA *)lpParameter)->hwnd;
char buf[1000];
char temp[1000];
CString str="";
int len=sizeof(SOCKADDR_IN);
SOCKADDR_IN sock_addr;
sock_addr.sin_addr.S_un.S_addr= inet_addr("127.0.0.1");
sock_addr.sin_family = AF_INET;
sock_addr.sin_port =htons(6000);
connect(socket,(SOCKADDR *)&sock_addr,sizeof(SOCKADDR));
recv(socket,buf,1000,0);
sprintf(temp,"%s说%s\r\n",inet_ntoa(sock_addr.sin_addr),buf);
str=(char *)temp;
//AfxMessageBox(str);
closesocket(socket);
::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM )temp);
return 0;
*/
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return FALSE;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return FALSE;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET socket_client= socket(AF_INET,SOCK_STREAM,0);
if( INVALID_SOCKET == socket_client)
{
AfxMessageBox("socket_client create error !");
return FALSE;
}
char buf[200];
char temp[200];
CString str="";
int len=sizeof(SOCKADDR_IN);
SOCKADDR_IN sock_addr;
sock_addr.sin_addr.S_un.S_addr= inet_addr("127.0.0.1");
sock_addr.sin_family = AF_INET;
sock_addr.sin_port =htons(6000);
connect(socket_client,(SOCKADDR *)&sock_addr,sizeof(SOCKADDR));
recv(socket_client,buf,200,0);
sprintf(temp,"server : %s\r\n"/*,inet_ntoa(sock_addr.sin_addr)*/,buf);
str=(char *)temp;
AfxMessageBox(temp);
closesocket(socket_client);
HWND hwnd=((RECVDATA *)lpParameter)->hwnd;
::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM )temp);
return 0;
}
void CTCP_CLIENTDlg::OnRecvData(WPARAM wParam, LPARAM lParam)
{
CString str=(char *)lParam ;
m_recv+=str;
m_recv+="\r\n";
UpdateData(FALSE);
}