Server:
#include "stdafx.h"
#include "施政server.h"
#include "施政serverDlg.h"
#include "winsock.h"
#pragma comment(lib,"wsock32")
#define WM_CLIENT_ACCEPT WM_USER+101//自定义的消息触发异步通信事件
#define WM_CLIENT_SENDDATA WM_USER+102//自定义的消息触发异步通信事件
SOCKET szsocket,s;
sockaddr_in szsockaddr;
ON_MESSAGE(WM_CLIENT_ACCEPT,Accept)//消息对应的函数
ON_MESSAGE(WM_CLIENT_SENDDATA,senddata)//消息对应的函数
OnButton1()// 对应 初始按钮.
OnButton2()// 对应 发送按钮
senddata(WPARAM wParam, LPARAM lParam) //对应消息MAP: ON_MESSAGE(WM_CLIENT_SENDDATA,senddata)
Accept(WPARAM wParam,LPARAM lParam) //对应消息MAP: ON_MESSAGE(WM_CLIENT_ACCEPT,Accept)
主代码如下,资源文件略
void CServerDlg::OnButton1() //初始化程序并给出状态
{
WSADATA szDATA;
WORD version=MAKEWORD(2,0);
WSAStartup(version,&szDATA);
int nStatus;
szsocket=socket(AF_INET,SOCK_STREAM,0);
nStatus=WSAAsyncSelect(szsocket,m_hWnd,WM_CLIENT_ACCEPT,FD_ACCEPT);
if(nStatus==SOCKET_ERROR)
{
SetDlgItemText(IDC_EDIT1,"非阻塞模式设置失败!/r/n");
AutoLastLine(IDC_EDIT1);
}
else
{
SetDlgItemText(IDC_EDIT1,"非阻塞模式设置成功!/r/n");
AutoLastLine(IDC_EDIT1);
}
szsockaddr.sin_family=AF_INET;
szsockaddr.sin_addr.S_un.S_addr=INADDR_ANY;
szsockaddr.sin_port=htons(3000);
int backdata=0;
backdata=bind(szsocket,(LPSOCKADDR)&szsockaddr,sizeof(szsockaddr));
if(backdata==SOCKET_ERROR)
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"绑定协议出现错误!!/r/n");
AutoLastLine(IDC_EDIT1);
}
else
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"绑定协议成功!!/r/n");
AutoLastLine(IDC_EDIT1);
}
backdata=listen(szsocket,1);
if(backdata==SOCKET_ERROR)
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"监听端口失败!/r/n");
AutoLastLine(IDC_EDIT1);
}
else
{
char hostname[256];
CString str;
GetDlgItemText(IDC_EDIT1,str);
gethostname(hostname,sizeof(hostname));
hostent* hn;
hn=gethostbyname(hostname);//根据本机主机名得到本机ip
CString strIPAddr;
strIPAddr=inet_ntoa(*(struct in_addr*)hn->h_addr_list[0]);
SetDlgItemText(IDC_EDIT1,str+"监听端口成功!/r/n本机IP地址:"+strIPAddr+"/r/n");
AutoLastLine(IDC_EDIT1);
}
}
LRESULT CServerDlg::Accept(WPARAM wParam,LPARAM lParam)
{
if(WSAGETSELECTEVENT(lParam) == FD_ACCEPT)
{
s=accept(szsocket,NULL,NULL);
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"IP连接成功!/r/n");
AutoLastLine(IDC_EDIT1);
}
if(WSAAsyncSelect(s,m_hWnd,WM_CLIENT_SENDDATA,FD_READ)==SOCKET_ERROR)
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"数据传送消息失败!/r/n");
AutoLastLine(IDC_EDIT1);
return 0;
}
else
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"准备接收数据传送!/r/n");
AutoLastLine(IDC_EDIT1);
}
return 0;
}
LRESULT CServerDlg::senddata(WPARAM wParam, LPARAM lParam)//显示接到的数据
{
if(WSAGETSELECTEVENT(lParam) == FD_READ)
{
char buff[1024];
recv(s,buff,sizeof(buff),0);
CString str1,str2;
str2=&buff[0];
GetDlgItemText(IDC_EDIT2,str1);
SetDlgItemText(IDC_EDIT2,str1+"client:"+str2+"/r/n");
}
AutoLastLine(IDC_EDIT2);
return 0;
}
void CServerDlg::OnButton2()//发送数据
{
char buff[1024];
CString str1,str2;
GetDlgItemText(IDC_EDIT3,str1);
GetDlgItemText(IDC_EDIT2,str2);
SetDlgItemText(IDC_EDIT2,str2+"server:"+str1+"/r/n");
AutoLastLine(IDC_EDIT2);
strcpy(buff,str1);
send(s,buff,sizeof(buff),0);
str1.Empty();
SetDlgItemText(IDC_EDIT3,str1);
}
void CServerDlg::AutoLastLine(int nID)
{
CEdit *pEdit=(CEdit *)GetDlgItem(nID);
int nLen=pEdit->GetWindowTextLength();
pEdit->SetFocus();
pEdit->SetSel(nLen,nLen);
}
Client:
#include "stdafx.h"
#include "施政client.h"
#include "施政clientDlg.h"
#include "winsock.h"
#pragma comment(lib,"wsock32")
SOCKET datasocket;
sockaddr_in datasockaddr;
#define WM_SENDDATA WM_USER + 101//自定义的消息用于触发异步通信事件
ON_MESSAGE(WM_SENDDATA,senddata)//消息和消息触发的函数
OnButton1()//对应按钮发送
OnButton2()//对应按钮连接
senddata(WPARAM wParam, LPARAM lParam) //对应消息ON_MESSAGE(WM_SENDDATA,senddata)
void CClientDlg::OnButton1()
{
char buff[1024];
CString str1,str2;
GetDlgItemText(IDC_EDIT2,str1);
GetDlgItemText(IDC_EDIT1,str2);
SetDlgItemText(IDC_EDIT1,str2+"client:"+str1+"/r/n");
AutoLastLine(IDC_EDIT1);
strcpy(buff,str1);
send(datasocket,buff,sizeof(buff),0);
str1.Empty();
SetDlgItemText(IDC_EDIT2,str1);
}
LRESULT CClientDlg::senddata(WPARAM wParam, LPARAM lParam)//接受并显示数据
{
if(WSAGETSELECTEVENT(lParam) == FD_READ)
{
char buff[1024];
recv(datasocket,buff,sizeof(buff),0);
CString str1,str2;
str2=&buff[0];
GetDlgItemText(IDC_EDIT1,str1);
SetDlgItemText(IDC_EDIT1,str1+"server:"+str2+"/r/n");
AutoLastLine(IDC_EDIT1);
}
return 0;
}
void CClientDlg::OnButton2() //初始化SOCKET 和服务器ip地址
{
WSADATA szDATA;
WORD version=MAKEWORD(2,0);
WSAStartup(version,&szDATA);
datasocket=socket(AF_INET,SOCK_STREAM,0);
datasockaddr.sin_family=AF_INET;
CString ipaddr1,ipaddr2,ipaddr3,ipaddr4;
GetDlgItemText(IDC_EDIT3,ipaddr1);
GetDlgItemText(IDC_EDIT4,ipaddr2);
GetDlgItemText(IDC_EDIT5,ipaddr3);
GetDlgItemText(IDC_EDIT6,ipaddr4);
datasockaddr.sin_addr.S_un.S_addr=inet_addr(ipaddr1+"."+ipaddr2+"."+ipaddr3+"."+ipaddr4);
datasockaddr.sin_port=htons(3000);
int backdata=0;
backdata=connect(datasocket,(LPSOCKADDR)&datasockaddr,sizeof(datasockaddr));
if(backdata==SOCKET_ERROR)
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"链接失败!/r/n");
}
else
{
CString str;
GetDlgItemText(IDC_EDIT1,str);
SetDlgItemText(IDC_EDIT1,str+"IP连接成功!/r/n");
}
WSAAsyncSelect(datasocket,m_hWnd,WM_SENDDATA,FD_READ);//异步通信并注册消息事件
}
void CClientDlg::AutoLastLine(int nID)//滚动条始终处于最后一行
{
CEdit *pEdit=(CEdit *)GetDlgItem(nID);
int nLen=pEdit->GetWindowTextLength();
pEdit->SetFocus();
pEdit->SetSel(nLen,nLen);
}