WinSock server not getting data from client

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This should be an echo connection,but the server doesnt get any data.
Server:
Header:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // wsChatclientDlg.h : header file
<span style="color:Green; //

#pragma once


<span style="color:Green; // CwsChatclientDlg dialog
<span style="color:Blue; class CwsChatclientDlg : <span style="color:Blue; public CDialogEx
{
<span style="color:Green; // Construction
<span style="color:Blue; public:
SOCKET m_s;
SOCKADDR_IN m_addr;
CwsChatclientDlg(CWnd* pParent = NULL); <span style="color:Green; // standard constructor

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_WSCHATCLIENT_DIALOG };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support


<span style="color:Green; // Implementation
<span style="color:Blue; protected:
HICON m_hIcon;

<span style="color:Green; // Generated message map functions
<span style="color:Blue; virtual BOOL OnInitDialog();
afx_msg <span style="color:Blue; void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg <span style="color:Blue; void OnPaint();
afx_msg <span style="color:Blue; long OnAsyncSelect(WPARAM wParam,LPARAM lParam);
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
<span style="color:Blue; public:
afx_msg <span style="color:Blue; void OnBnClickedOk();
};

[/code]


<br/>
The cpp file:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // wsChatclientDlg.cpp : implementation file
<span style="color:Green; //

#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "wsChatclient.h"
#include <span style="color:#A31515; "wsChatclientDlg.h"
#include <span style="color:#A31515; "afxdialogex.h"

#ifdef _DEBUG
#define <span style="color:Blue; new DEBUG_NEW
#endif


#define WM_USER_ASYNC_SELECT (WM_USER+1)

<span style="color:Green; // CAboutDlg dialog used for App About

<span style="color:Blue; class CAboutDlg : <span style="color:Blue; public CDialogEx
{
<span style="color:Blue; public:
CAboutDlg();

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_ABOUTBOX };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support

<span style="color:Green; // Implementation
<span style="color:Blue; protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

<span style="color:Blue; void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


<span style="color:Green; // CwsChatclientDlg dialog




CwsChatclientDlg::CwsChatclientDlg(CWnd* pParent <span style="color:Green; /*=NULL*/)
: CDialogEx(CwsChatclientDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

<span style="color:Blue; void CwsChatclientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CwsChatclientDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_MESSAGE(WM_USER_ASYNC_SELECT,OnAsyncSelect)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CwsChatclientDlg::OnBnClickedOk)
END_MESSAGE_MAP()


<span style="color:Green; // CwsChatclientDlg message handlers

BOOL CwsChatclientDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

WORD wVersionRequested;
WSADATA wsaData;
<span style="color:Blue; int err;

wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
<span style="color:Blue; if (err != 0) {
<span style="color:Green; /* Tell the user that we could not find a usable */
<span style="color:Green; /* Winsock DLL. */
MessageBox(L<span style="color:#A31515; "Error",L<span style="color:#A31515; "Error");

}
<span style="color:Blue; else
MessageBox(L<span style="color:#A31515; "The Winsock 2.2 dll was found okayn",L<span style="color:#A31515; "Done");




<span style="color:Blue; wchar_t pszMessage[100]; <span style="color:Green; //informational message



m_s = socket(AF_INET,SOCK_DGRAM,0);
<span style="color:Blue; if(m_s == INVALID_SOCKET)
{

MessageBox(L<span style="color:#A31515; "Error at initialization of socket",L<span style="color:#A31515; "Error");

}
<span style="color:Blue; else
{
<span style="color:Green; //fill out the name this server will read data from
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(2050);
m_addr.sin_addr.s_addr = htonl(INADDR_ANY);

<span style="color:Green; //bind the name to the socket
<span style="color:Blue; if(bind(m_s, (LPSOCKADDR)&m_addr, <span style="color:Blue; sizeof(m_addr)) == SOCKET_ERROR)
{
MessageBox(L<span style="color:#A31515; "Error at socket binding",L<span style="color:#A31515; "Error");
}
<span style="color:Blue; else
{
SOCKADDR_IN addr;
<span style="color:Blue; int nAddrLen = <span style="color:Blue; sizeof(addr);

<span style="color:Blue; if(getsockname(m_s,(LPSOCKADDR)&addr,&nAddrLen) == SOCKET_ERROR)
MessageBox(L<span style="color:#A31515; "Datagram echo server could not get sockets port",L<span style="color:#A31515; "ERROR");
<span style="color:Blue; else
{
wsprintf(pszMessage,L<span style="color:#A31515; "Datagram echo server using port %d",
ntohs(addr.sin_port));

<span style="color:Blue; if(WSAAsyncSelect(m_s,m_hWnd,WM_USER_ASYNC_SELECT,
FD_READ |FD_WRITE) == SOCKET_ERROR)
MessageBox(L<span style="color:#A31515; "Datagram echo server could not do async select");
}
}}





<span style="color:Green; // Add "About..." menu item to system menu.

<span style="color:Green; // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
<span style="color:Blue; if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
<span style="color:Blue; if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

<span style="color:Green; // Set the icon for this dialog. The framework does this automatically
<span style="color:Green; // when the applications main window is not a dialog
SetIcon(m_hIcon, TRUE); <span style="color:Green; // Set big icon
SetIcon(m_hIcon, FALSE); <span style="color:Green; // Set small icon

<span style="color:Green; // TODO: Add extra initialization here

<span style="color:Blue; return TRUE; <span style="color:Green; // return TRUE unless you set the focus to a control
}

<span style="color:Blue; void CwsChatclientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
<span style="color:Blue; if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
<span style="color:Blue; else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

<span style="color:Green; // If you add a minimize button to your dialog, you will need the code below
<span style="color:Green; // to draw the icon. For MFC applications using the document/view model,
<span style="color:Green; // this is automatically done for you by the framework.

<span style="color:Blue; void CwsChatclientDlg::OnPaint()
{
<span style="color:Blue; if (IsIconic())
{
CPaintDC dc(<span style="color:Blue; this); <span style="color:Green; // device context for painting

SendMessage(WM_ICONERASEBKGND, <span style="color:Blue; reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

<span style="color:Green; // Center icon in client rectangle
<span style="color:Blue; int cxIcon = GetSystemMetrics(SM_CXICON);
<span style="color:Blue; int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
<span style="color:Blue; int x = (rect.Width() - cxIcon + 1) / 2;
<span style="color:Blue; int y = (rect.Height() - cyIcon + 1) / 2;

<span style="color:Green; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
<span style="color:Blue; else
{
CDialogEx::OnPaint();
}
}

<span style="color:Green; // The system calls this function to obtain the cursor to display while the user drags
<span style="color:Green; // the minimized window.
HCURSOR CwsChatclientDlg::OnQueryDragIcon()
{
<span style="color:Blue; return <span style="color:Blue; static_cast<HCURSOR>(m_hIcon);
}

<span style="color:Blue; long CwsChatclientDlg::OnAsyncSelect(WPARAM wParam,LPARAM lParam)
{
<span style="color:Blue; wchar_t pszMessage[100];
<span style="color:Blue; static <span style="color:Blue; wchar_t pBuf[101];
<span style="color:Blue; int nBytesRecv;
<span style="color:Blue; int nBytesSent;
<span style="color:Blue; static <span style="color:Blue; int nBytesToSend = 0;
<span style="color:Blue; int nError;
<span style="color:Blue; static SOCKADDR_IN addrFrom;
<span style="color:Blue; static <span style="color:Blue; int nAddrFromLen = <span style="color:Blue; sizeof(addrFrom);
<span style="color:Blue; static IN_ADDR inFrom;

<span style="color:Blue; if(WSAGETSELECTERROR(lParam) != 0)
{
wsprintf(pszMessage,L<span style="color:#A31515; "Datagram echo server async select got error %d",
WSAGETSELECTERROR(lParam));
MessageBox(pszMessage,L<span style="color:#A31515; "Error");
<span style="color:Blue; return 0L;
}

<span style="color:Blue; switch(WSAGETSELECTEVENT(lParam))
{
<span style="color:Blue; case FD_WRITE:
<span style="color:Blue; if(nBytesToSend != 0)
{
nBytesSent = sendto(m_s,(<span style="color:Blue; char*)pBuf,nBytesToSend,0,
(LPSOCKADDR)&addrFrom,nAddrFromLen);

<span style="color:Blue; if(nBytesSent == SOCKET_ERROR)
{
nError = WSAGetLastError();
<span style="color:Blue; if(nError != WSAEWOULDBLOCK)
{
wsprintf(pszMessage,L<span style="color:#A31515; "Error %d sending data to %s, %d",
nError,inet_ntoa(inFrom),ntohs(addrFrom.sin_port));
MessageBox(pszMessage,L<span style="color:#A31515; "Error");

nBytesToSend = 0;

PostMessage(WM_USER_ASYNC_SELECT,m_s,WSAMAKESELECTREPLY(FD_READ,0));
}
}
<span style="color:Blue; else
{
wsprintf(pszMessage,L<span style="color:#A31515; "Data sent (%s) to %s, %d",
pBuf,inet_ntoa(inFrom),ntohs(addrFrom.sin_port));
MessageBox(pszMessage,L<span style="color:#A31515; "Data Sent");
nBytesToSend = 0;
}
}
<span style="color:Blue; break;

<span style="color:Blue; case FD_READ:

<span style="color:Blue; if(nBytesToSend == 0)
{
nBytesRecv = recvfrom(m_s,(<span style="color:Blue; char*)pBuf,100,0,(LPSOCKADDR)&addrFrom,&nAddrFromLen);

<span style="color:Blue; if(nBytesRecv == SOCKET_ERROR)
{
nError = WSAGetLastError();
<span style="color:Blue; if(nError != WSAEWOULDBLOCK);
{
wsprintf(pszMessage,L<span style="color:#A31515; "Error %d receiving data", nError);
MessageBox(pszMessage,L<span style="color:#A31515; "Error");
}
}
<span style="color:Blue; else
{
memcpy(&inFrom,&addrFrom.sin_addr.s_addr,4);
nBytesToSend = nBytesRecv;
pBuf[nBytesToSend] = (<span style="color:Blue; wchar_t)L<span style="color:#A31515; "";
wsprintf(pszMessage,L<span style="color:#A31515; "Data received (%s) from %s, %d",
pBuf,inet_ntoa(inFrom),ntohs(addrFrom.sin_port));
MessageBox(pszMessage,L<span style="color:#A31515; "Data received");

PostMessage(WM_USER_ASYNC_SELECT,m_s,WSAMAKESELECTREPLY(FD_WRITE,0));
}

}
<span style="color:Blue; break;
<span style="color:Blue; default:
<span style="color:Blue; break;
}
<span style="color:Blue; return 0L;
}



<span style="color:Blue; void CwsChatclientDlg::OnBnClickedOk()
{
<span style="color:Green; // TODO: Add your control notification handler code here


<span style="color:Blue; wchar_t pszMessage[100]; <span style="color:Green; //informational message
SOCKET s;
SOCKADDR_IN addr;
#define BUFSIZE (100)
<span style="color:Blue; wchar_t pszBuf[BUFSIZE];
<span style="color:Blue; int nBytesRecv;
<span style="color:Blue; int nError;
SOCKADDR_IN addrFrom;
<span style="color:Blue; int nAddrFromLen = <span style="color:Blue; sizeof(addrFrom);
IN_ADDR inFrom;

s = socket(AF_INET,SOCK_DGRAM,0);
<span style="color:Blue; if(s == INVALID_SOCKET)
{
nError = WSAGetLastError();
}
<span style="color:Blue; else
{
<span style="color:Green; //fill out the name this server will read data from
addr.sin_family = AF_INET;
addr.sin_port = htons(2050);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

<span style="color:Green; //bind the name to the socket
<span style="color:Blue; if(bind(s, (LPSOCKADDR)&addr, <span style="color:Blue; sizeof(addr)) == SOCKET_ERROR)
{
nError = WSAGetLastError();
}
<span style="color:Blue; else
{
<span style="color:Blue; wchar_t text;
<span style="color:Blue; wchar_t texttmp;
nBytesRecv = recvfrom(s,(<span style="color:Blue; char*)pszBuf,100,0,
(LPSOCKADDR)&addrFrom,&nAddrFromLen);


lstrcat((LPWSTR)text,pszBuf);
<span style="color:Blue; if(nBytesRecv == SOCKET_ERROR)
{
nError = WSAGetLastError();
}
<span style="color:Blue; else
{
<span style="color:Green; //get some data...

<span style="color:Green; //copy the four byte IP addres into an IP address structure
memcpy(&inFrom,&addrFrom.sin_addr.s_addr,4);

<span style="color:Green; //print an informational message
wsprintf(pszMessage,
L<span style="color:#A31515; "server received %d bytes from %s, port is %d",
nBytesRecv,inet_ntoa(inFrom),ntohs(addrFrom.sin_port));

MessageBox(pszMessage, L<span style="color:#A31515; "Datagram Server Info");
MessageBox((LPCTSTR)text, L<span style="color:#A31515; "Datagram Server Info");
}
}
closesocket(s);

}
}
[/code]

<br/>
CLIENT:
Header:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // wsChatrealClientDlg.h : header file
<span style="color:Green; //

#pragma once


<span style="color:Green; // CwsChatrealClientDlg dialog
<span style="color:Blue; class CwsChatrealClientDlg : <span style="color:Blue; public CDialogEx
{
<span style="color:Green; // Construction
<span style="color:Blue; public:
CwsChatrealClientDlg(CWnd* pParent = NULL); <span style="color:Green; // standard constructor
SOCKET m_s;
SOCKADDR_IN m_addr;
IN_ADDR m_in;
<span style="color:Blue; int m_nBytesToSend;
<span style="color:Blue; wchar_t m_pBuf[101];

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_WSCHATREALCLIENT_DIALOG };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support


<span style="color:Green; // Implementation
<span style="color:Blue; protected:
HICON m_hIcon;

<span style="color:Green; // Generated message map functions
<span style="color:Blue; virtual BOOL OnInitDialog();
afx_msg <span style="color:Blue; void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg <span style="color:Blue; void OnPaint();
afx_msg <span style="color:Blue; long OnAsyncSelect(WPARAM wParam,LPARAM lParam);
afx_msg HCURSOR OnQueryDragIcon();
afx_msg <span style="color:Blue; void OnTimer(UINT nIDEvent);
DECLARE_MESSAGE_MAP()
<span style="color:Blue; public:

afx_msg <span style="color:Blue; void OnBnClickedOk();
};

[/code]

<br/>
C++ file:

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // wsChatrealClientDlg.cpp : implementation file
<span style="color:Green; //

#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "wsChatrealClient.h"
#include <span style="color:#A31515; "wsChatrealClientDlg.h"
#include <span style="color:#A31515; "afxdialogex.h"

#ifdef _DEBUG
#define <span style="color:Blue; new DEBUG_NEW
#endif

#define WM_USER_ASYNC_SELECT (WM_USER+1)
<span style="color:Green; // CAboutDlg dialog used for App About

<span style="color:Blue; class CAboutDlg : <span style="color:Blue; public CDialogEx
{
<span style="color:Blue; public:
CAboutDlg();

<span style="color:Green; // Dialog Data
<span style="color:Blue; enum { IDD = IDD_ABOUTBOX };

<span style="color:Blue; protected:
<span style="color:Blue; virtual <span style="color:Blue; void DoDataExchange(CDataExchange* pDX); <span style="color:Green; // DDX/DDV support

<span style="color:Green; // Implementation
<span style="color:Blue; protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

<span style="color:Blue; void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


<span style="color:Green; // CwsChatrealClientDlg dialog




CwsChatrealClientDlg::CwsChatrealClientDlg(CWnd* pParent <span style="color:Green; /*=NULL*/)
: CDialogEx(CwsChatrealClientDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

<span style="color:Blue; void CwsChatrealClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CwsChatrealClientDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_USER_ASYNC_SELECT,OnAsyncSelect)
ON_WM_TIMER()
END_MESSAGE_MAP()


<span style="color:Green; // CwsChatrealClientDlg message handlers

BOOL CwsChatrealClientDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

WORD wVersionRequested;
WSADATA wsaData;
<span style="color:Blue; int err;

wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
<span style="color:Blue; if (err != 0) {
<span style="color:Green; /* Tell the user that we could not find a usable */
<span style="color:Green; /* Winsock DLL. */
MessageBox(L<span style="color:#A31515; "Error",L<span style="color:#A31515; "Error");

}
<span style="color:Blue; else
MessageBox(L<span style="color:#A31515; "The Winsock 2.2 dll was found okayn",L<span style="color:#A31515; "Done");


SOCKET s;
SOCKADDR_IN addr;
#define BUFSIZE (100)
<span style="color:Blue; wchar_t pszBuf[BUFSIZE];
<span style="color:Blue; int nBufLen;
<span style="color:Blue; int nBytesSent;
<span style="color:Blue; int nError;

m_s = socket(AF_INET,SOCK_DGRAM,0);
<span style="color:Blue; if(m_s == INVALID_SOCKET)
{
nError = WSAGetLastError();
MessageBox((LPCTSTR)WSAGetLastError,L<span style="color:#A31515; "Error");
}
<span style="color:Blue; else
{
addr.sin_family = AF_INET;
addr.sin_port = htons(2050);
addr.sin_addr.s_addr = inet_addr(<span style="color:#A31515; "192.168.2.4");

memcpy(&m_in,&m_addr.sin_addr.s_addr,4);

<span style="color:Blue; if(WSAAsyncSelect(m_s,m_hWnd,WM_USER_ASYNC_SELECT,
FD_READ | FD_WRITE) == SOCKET_ERROR){

}

<span style="color:Blue; else{
SetTimer(1,5000,NULL);
}

}




<span style="color:Green; // Add "About..." menu item to system menu.

<span style="color:Green; // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
<span style="color:Blue; if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
<span style="color:Blue; if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

<span style="color:Green; // Set the icon for this dialog. The framework does this automatically
<span style="color:Green; // when the applications main window is not a dialog
SetIcon(m_hIcon, TRUE); <span style="color:Green; // Set big icon
SetIcon(m_hIcon, FALSE); <span style="color:Green; // Set small icon

<span style="color:Green; // TODO: Add extra initialization here

<span style="color:Blue; return TRUE; <span style="color:Green; // return TRUE unless you set the focus to a control
}

<span style="color:Blue; void CwsChatrealClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
<span style="color:Blue; if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
<span style="color:Blue; else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

<span style="color:Green; // If you add a minimize button to your dialog, you will need the code below
<span style="color:Green; // to draw the icon. For MFC applications using the document/view model,
<span style="color:Green; // this is automatically done for you by the framework.

<span style="color:Blue; void CwsChatrealClientDlg::OnPaint()
{
<span style="color:Blue; if (IsIconic())
{
CPaintDC dc(<span style="color:Blue; this); <span style="color:Green; // device context for painting

SendMessage(WM_ICONERASEBKGND, <span style="color:Blue; reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

<span style="color:Green; // Center icon in client rectangle
<span style="color:Blue; int cxIcon = GetSystemMetrics(SM_CXICON);
<span style="color:Blue; int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
<span style="color:Blue; int x = (rect.Width() - cxIcon + 1) / 2;
<span style="color:Blue; int y = (rect.Height() - cyIcon + 1) / 2;

<span style="color:Green; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
<span style="color:Blue; else
{
CDialogEx::OnPaint();
}
}

<span style="color:Green; // The system calls this function to obtain the cursor to display while the user drags
<span style="color:Green; // the minimized window.
HCURSOR CwsChatrealClientDlg::OnQueryDragIcon()
{
<span style="color:Blue; return <span style="color:Blue; static_cast<HCURSOR>(m_hIcon);
}

<span style="color:Blue; long CwsChatrealClientDlg::OnAsyncSelect(WPARAM wParam,LPARAM lParam)
{
<span style="color:Blue; wchar_t pszMessage[100]; <span style="color:Green; // informational message
<span style="color:Blue; static <span style="color:Blue; wchar_t pBuf[101]; <span style="color:Green; //send/recv buffer
<span style="color:Blue; int nBytesRecv;
<span style="color:Blue; int nBytesSent;
<span style="color:Blue; int nError;

<span style="color:Blue; switch (WSAGETSELECTEVENT(lParam))
{
<span style="color:Blue; case FD_WRITE:
<span style="color:Blue; if(m_nBytesToSend != 0)
{
nBytesSent = sendto(m_s,(<span style="color:Blue; char*)m_pBuf,m_nBytesToSend,0,
(LPSOCKADDR)&m_addr,<span style="color:Blue; sizeof(m_addr));

<span style="color:Blue; if(nBytesSent == SOCKET_ERROR)
{
nError = WSAGetLastError();
MessageBox(L<span style="color:#A31515; "Error at sending",L<span style="color:#A31515; "Error");
<span style="color:Blue; if(nError != WSAEWOULDBLOCK){
m_nBytesToSend = 0;

}
}
<span style="color:Blue; else
{
wsprintf(pszMessage,L<span style="color:#A31515; "Data sent (%s) to %s,%d",
m_pBuf,inet_ntoa(m_in),ntohs(m_addr.sin_port));
m_nBytesToSend = 0;
MessageBox(pszMessage,L<span style="color:#A31515; "Info");
}
}
<span style="color:Blue; break;

<span style="color:Blue; case FD_READ:

nBytesRecv = recvfrom(m_s,(<span style="color:Blue; char*)pBuf,100,0,NULL,NULL);
<span style="color:Blue; if(nBytesRecv == SOCKET_ERROR)
{
nError = WSAGetLastError();
<span style="color:Blue; if(nError != WSAEWOULDBLOCK)
{
wsprintf(pszMessage, L<span style="color:#A31515; "Error %d receiving data",nError);
MessageBox(pszMessage,L<span style="color:#A31515; "Info");
}
}
<span style="color:Blue; else
{
pBuf[nBytesRecv] = L;
wsprintf(pszMessage,L<span style="color:#A31515; "Data received (%s) from %s, %d",
pBuf,inet_ntoa(m_in),ntohs(m_addr.sin_port));
MessageBox(pszMessage,L<span style="color:#A31515; "Info");
}
<span style="color:Blue; break;
<span style="color:Blue; default:
<span style="color:Blue; break;
}
<span style="color:Blue; return 0L;
}


<span style="color:Blue; void CwsChatrealClientDlg::OnBnClickedOk()
{

}
<span style="color:Blue; void CwsChatrealClientDlg::OnTimer(UINT nIDEvent)
{
<span style="color:Blue; static <span style="color:Blue; int nSendCount = 1;

<span style="color:Blue; if(m_nBytesToSend == 0)
{
wsprintf(m_pBuf,L<span style="color:#A31515; "Hello %d",nSendCount);
++nSendCount;
m_nBytesToSend = lstrlen(m_pBuf);
PostMessage(WM_USER_ASYNC_SELECT,m_s,WSAMAKESELECTREPLY(FD_WRITE,0));
MessageBox(L<span style="color:#A31515; "MessagePosted - ToWrite",L<span style="color:#A31515; "Info");
}
}



[/code]

<br/>
Im ussing a router.
If I wanna see my ip from the web is shows the same ip on both 2 computers Im testing the server and client.The ip I use in the client to connect to the server was taken from ipconfig /all

Please tell me why the connection fails.

View the full article
 
Back
Top