处事器与客户机互传动静
副标题#e#
一、处事器措施
建设一个名为“server"的项目,单文档界面。
在serverview.h中插手代码:
#include“winsock.h"
添加变量:
CSize sizeTotal;// 控 制 滚 动 条
int count;// 信 息 条 数
CString m_data[1000];// 信 息 存 放
char Hostname[260];
char Hostaddress[20];// 主 机IP 地 址
SOCKET m_sock;
HANDLE m_hListenThread;// 线 程
BOOL m_bInitialized;// 是 否 初 始 化
WSADATA WSAData;
BOOL flag;
SOCKADDR_IN saClnt;
int saClntLen;
BOOL Isconnect;// 是 否 连 接
在serverview.cpp中重载CServerView()结构器,建设并绑定嵌套字:
CServerView::CServerView()
{ // TODO: add construction code here
Isconnect=FALSE;
flag=FALSE;
sizeTotal.cy=350;
sizeTotal.cx=300;
m_hListenThread;
count=5;
int status;
WSADATA wsaData;
m_data[0]=“initializing Windows Sockets DLL....";
if((status=WSAStartup(0x0101, &wsaData))==0)
{ m_data[0] +=“Succeeded";
m_bInitialized=TRUE;
}
else
{ m_bInitialized=FALSE;
}
m_sock=socket(AF_INET,SOCK_DGRAM,0);
m_data[1]=“Creating socket....";
if(m_sock==INVALID_SOCKET)
{ m_data[1] +=“Failed";
}
m_data[1] +=“Succeeded";
m_data[2]=“Binding socket....";
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
sa.sin_port=htons(5050);
if(bind(m_sock,(PSOCKADDR)
&sa,sizeof(sa))==SOCKET_ERROR)
{ m_data[2] +=“Failed";
closesocket(m_sock);
}
m_data[2] +=“Succeeded";
m_data[3]=“Creating listener thread....";
unsigned long idThread;
m_hListenThread=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)Listen,
(void if(m_hListenThread)
{ m_data[3] +=“Succeeded";
m_data[4] +=“Listening....";
}
else
m_data[4] +=“Failed";
}
#p#副标题#e#
在析构函数中完成必须的排除操纵:
CServerView:: ~CServerView()
{ if(m_bInitialized)
WSACleanup();
closesocket(m_sock);
if(m_hListenThread)
::TerminateThread(m_hListenThread,0);
}
界说吸收和处理惩罚动静的线程:
long WINAPI Listen(CServerView *pView)
{ char msg[2000]=“";
int nchar;
SOCKADDR_IN saClnt;
int saClntLen;
while(1)
{ saClntLen=sizeof(saClnt);
nchar=recvfrom(pView ->m_sock,msg,1024,0,
(PSOCKADDR) &saClnt, &saClntLen);
if(nchar<0)
{ pView ->m_data[pView ->count + +] +
=“Error in recvfrom\n";
pView ->InvalidateRect(NULL);
}
else
{switch(msg[0])
{
case ‘A':
wsprintf(msg,“A:Client from %s attached\n",
inet_ntoa(saClnt.sin_addr));
pView ->m_data[pView ->count + +]=msg;
pView ->flag=TRUE;
pView ->InvalidateRect(NULL);
pView ->Isconnect=TRUE;
pView ->saClnt=saClnt;
pView ->saClntLen=saClntLen;
sendto(pView ->m_sock,msg,1024,0,(PSOCKADDR)
&saClnt,saClntLen);
break;
case ‘D':
wsprintf(msg,“D: Client form %s detached\n",
inet_ntoa(saClnt.sin_addr));
pView ->m_data[pView ->count + +]=msg;
pView ->flag=TRUE;
pView ->InvalidateRect(NULL);
pView ->Isconnect=FALSE;
sendto(pView ->m_sock,msg,1024,0,(PSOCKADDR)
&saClnt,saClntLen);
break;
case ‘R':
saClntLen=sizeof(saClnt);
pView ->m_data[pView ->count + +]=msg;
pView ->flag=TRUE;
pView ->InvalidateRect(NULL);
break;
default:
break;
}
}
}
return(0);
}
在措施菜单项中添加“本机IP地点":
#p#分页标题#e#
void CServerView::OnIp()
{int WSAReturn;
WSAReturn=WSAStartup( 0x0101, &WSAData );
if( WSAReturn == 0 ){
gethostname( Hostname, 260 );
struct hostent *pHostEnt;
pHostEnt = gethostbyname( Hostname);
if( pHostEnt != NULL ){
wsprintf( Hostaddress, “ %d. %d. %d. %d",
( pHostEnt ->h_addr_list[0][0] & 0x00ff ),
( pHostEnt ->h_addr_list[0][1] & 0x00ff ),
( pHostEnt ->h_addr_list[0][2] & 0x00ff ),
( pHostEnt ->h_addr_list[0][3] & 0x00ff ) );
CString out;
out.Format(Hostaddress);
AfxMessageBox(out);
}
}
}
在措施菜单中添加“发送动静":
void CServerView::OnSendmessage()
{// TODO: Add your command handler code here
char msg[2000];
Csend Sendmessage;
if(Sendmessage.DoModal()==IDOK
& &!Sendmessage.m_Message.IsEmpty())
{ wsprintf(msg,“R: " +Sendmessage.m_Message);
sendto(m_sock,msg,1024,0,(PSOCKADDR)
&saClnt,saClntLen);
m_data[count + +]=Sendmessage.m_Message;
flag=TRUE;
InvalidateRect(NULL);
}
}
为发送动静项添加一个对话框的类,名为send,有一个文本框,用来发送动静,并为文本框添加CStringm_Message变量,在ServerView.cpp中添加#include“send.h"
为发送动静项添加一个判定函数:
void CServerView::OnUpdateSendmessage(CCmdUI *
pCmdUI)
{// TODO: Add your command update UI handler code here
pCmdUI ->Enable(FALSE);
if(Isconnect)
pCmdUI ->Enable(TRUE);
}
再窗口显示动静:
void CServerView::OnDraw(CDC * pDC)
{ if(flag)
{sizeTotal.cy +=20;
for(int j=65;j
TextOut(10,y,m_data[i]);
y +=20;}
// TODO: add draw code for native data here
}
在Project中点击Settings中选择Link项添加wsock32.lib。
—-最后编译措施,就可以获得Server.exe措施。
二、客户机措施
建设一个名为“client"的项目,单文档界面。
在clientview.h中插手代码:
#include “winsock.h"
添加变量:
CString m_data[1000];
HANDLE m_hListenThread;
SOCKET m_sock;
SOCKADDR_IN m_saSrvr;
BOOL Isconnect;
int count;
CSize sizeTotal;
BOOL flag;
在结构函数中初始化变量:
CClientView::CClientView()
{ // TODO: add construction code here
Isconnect=FALSE;
sizeTotal.cy=350;
sizeTotal.cx=300;
flag=FALSE;
}
在析构函数中完成排除操纵,代码如上。
在菜单中添加“拨号"项:
void CClientView::OnDial()
{// TODO: Add your command handler code here
count=5;
if(m_bInitialized)
{ AfxMessageBox(“Already dialing");
return;}
Cdial dial;
if(dial.DoModal()==IDOK
& &!dial.m_HostAddress.IsEmpty())
{m_saSrvr.sin_family=AF_INET;
m_saSrvr.sin_addr.S_un.S_addr=htonl
(INADDR_ANY);
m_saSrvr.sin_addr.S_un.S_addr=inet_addr
(dial.m_HostAddress);
m_saSrvr.sin_port=htons(5050);
int status;
WSADATA wsaData;
m_data[0]=“initializing Windows Sockets DLL....";
if((status=WSAStartup(0x0101, &wsaData))==0)
{ m_data[0] +=“Succeeded";
m_bInitialized=TRUE;}
else
{ m_bInitialized=FALSE;}
m_sock=socket(AF_INET,SOCK_DGRAM,0);
m_data[1]=“Creating socket....";
if(m_sock==INVALID_SOCKET)
{ m_data[1] +=“Failed";}
m_data[1] +=“Succeeded";
m_data[2]=“Binding socket....";
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
sa.sin_port=htons(0);
if(bind(m_sock,(PSOCKADDR)
&sa,sizeof(sa))==SOCKET_ERROR)
{ m_data[2] +=“Failed";
closesocket(m_sock);}
m_data[2] +=“Succeeded";
m_data[3]=“Creating listener thread....";
unsigned long idThread;
m_hListenThread=CreateThread(NULL 0,
(LPTHREAD_START_ROUTINE)Listen,
(void *)this,0, &idThread);
if(m_hListenThread)
{ m_data[3] +=“Succeeded";
m_data[4] +=“Waiting....";}
else
m_data[4] +=“Failed";
InvalidateRect(NULL);
}
}
添加一个拨号对话框,名为dial,有一个文本框用来写IP地点.并在clientview.cpp中添加代码:
#include dial.h
在拨号项添加一个判定函数:
#p#分页标题#e#
void CClientView::OnUpdateDial(CCmdUI *
pCmdUI)
{// TODO: Add your command update UI handler code here
pCmdUI ->Enable(TRUE);
if(Isconnect)
pCmdUI ->Enable(FALSE);
}
添加吸收与发送动静的线程:
long WINAPI Listen(CClientView *pView)
{ char msg[2000];
pView ->m_data[5]=“Sending ATTACH command";
pView ->InvalidateRect(NULL);
wsprintf(msg,“A: ");
sendto(pView ->m_sock,msg,1024,0,(PSOCKADDR)
&pView ->m_saSrvr,sizeof(pView ->m_saSrvr));
int saSrvrLen ,nchar;
while(1)
{saSrvrLen=sizeof(pView ->m_saSrvr);
nchar=recvfrom(pView ->m_sock,msg,1024,0,
(PSOCKADDR) &pView ->m_saSrvr, &saSrvrLen);
if(nchar<0)
{ pView ->m_data[pView ->count + +]=
“Error in recvform";
pView ->InvalidateRect(NULL);}
else
{ pView ->m_data[pView ->count + +]=msg;
pView ->Isconnect=TRUE;
pView ->flag=TRUE;
pView ->InvalidateRect(NULL);
}
}
return(0);
}
同主措施一样做一个发送动静项,代码如上。
显示措施也与主措施一样,代码如上。
在Project中点击Settings中选择Link项添加wsock32.lib。
编译措施便可获得client.exe措施。
—-server.exe和client.exe做完后,就可以在具有TCP/IP协议下的网络中执行。