TCP短连接

来源:互联网 发布:女生 投行 知乎 编辑:程序博客网 时间:2024/06/08 10:05

客户端部分

class MySocket : public CSocket{public:MySocket();virtual ~MySocket();};
// MySocket//利用构造函数和析构函数自动连接和关闭连接MySocket::MySocket(){this->Create();if(!this->Connect("192.168.1.100",8668)){Close();::AfxMessageBox("连接服务器失败,请检查网络");}}MySocket::~MySocket(){//Close();基类析构函数已执行关闭::AfxMessageBox("MySocket关闭");}
OnInitDlg()

BOOL CclientDlg::OnInitDialog(){CDialogEx::OnInitDialog();/*//长连接方式被注释 将换为短连接this->m_sock.Create();if(!this->m_sock.Connect("192.168.1.100",8668)){::AfxMessageBox("连接失败:无法连接服务器,请检查你的网络连接");this->EndDialog(IDCANCEL);return FALSE;}*/this->m_list.InsertColumn(0,"工号",0,100);this->m_list.InsertColumn(1,"姓名",0,100);this->m_list.InsertColumn(2,"工资",0,100);this->OnRefresh();//自定义函数return TRUE;  // return TRUE  unless you set the focus to a control}
void CclientDlg::OnRefresh(void){//发送完协议编号后,先接收数据总数再逐条接收每条具体数据MySocket sock;                          //构造函数中已完成指向IP和端口的连接if(sock.m_hSocket==INVALID_SOCKET)return;int nCmd=INF_BROW;sock.Send(&nCmd,sizeof(nCmd));          //[发送]int nCount=0;int i=0;sock.Receive(&nCount,sizeof(nCount));   //[接收]this->m_list.DeleteAllItems();SData data;CString str;while(i<nCount){//从服务器端接收每条数据,并将内容插入到列表控件中显示sock.Receive(&data,sizeof(data));   <span style="font-family: Arial, Helvetica, sans-serif;">//[接收]</span>str.Format("%d",data.nNumb);m_list.InsertItem(i,str);m_list.SetItemText(i,1,data.sName);str.Format("%0.2f",data.fSala);this->m_list.SetItemText(i,2,str);i++;}}
void CclientDlg::OnBnClickedAdd(){// TODO: Add your control notification handler code here//先发送协议编号再发送一条信息数据到服务器端MySocket sock;if(sock.m_hSocket==INVALID_SOCKET)return;int nCmd=INF_ADD;sock.Send(&nCmd,sizeof(nCmd));             //[发送]SData data={this->GetDlgItemInt(IDC_NUMB)};//工号this->GetDlgItemTextA(IDC_NAME,data.sName,sizeof(data.sName));//姓名CString str;this->GetDlgItemTextA(IDC_SALA,str);data.fSala=(float)atof(str);//工资sock.Send(&data,sizeof(data));             //[发送]this->OnRefresh();}


服务器部分

serve.h中

public:CList<SData,SData> m_list;
private:CListenSocket m_sock;
BOOL CserverDlg::OnInitDialog(){CDialogEx::OnInitDialog();if(this->m_sock.Create(8668)){this->m_sock.Listen();::AfxMessageBox("服务器创建成功");}else{CString str;str.Format("Socket创建失败 错误:%d",::GetLastError());::AfxMessageBox(str);}return TRUE; }
ListenSocket.h

虚函数OnAccept()

#include "ClientSocket.h"void CListenSocket::OnAccept(int nErrorCode){CClientSocket* pSock=new CClientSocket;if(!this->Accept(*pSock))delete pSock;::AfxMessageBox("新连入一个客户端");CSocket::OnAccept(nErrorCode);}
ClientSocket.h

class CClientSocket : public CSocket{public:CClientSocket();virtual ~CClientSocket();virtual void OnAccept(int nErrorCode);virtual void OnClose(int nErrorCode);virtual void OnReceive(int nErrorCode);void AddData(void);void Browse(void);};
void CClientSocket::OnClose(int nErrorCode){// TODO: Add your specialized code here and/or call the base classdelete this;::AfxMessageBox("关掉一个客户经理");CSocket::OnClose(nErrorCode);}
void CClientSocket::OnReceive(int nErrorCode){// TODO: Add your specialized code here and/or call the base classint nCmd=0;int nLen=0;if(this->Receive(&nCmd,sizeof(nCmd))<=0)return;switch(nCmd){case INF_ADD:this->AddData();//读取客户端数据写入服务器break;case INF_BROW://浏览this->Browse();//将服务器数据发给客户端break;}CSocket::OnReceive(nErrorCode);}
extern CserverApp theApp;void CClientSocket::AddData(void){SData data;if(this->Receive(&data,sizeof(data))<=0)return;theApp.m_list.AddTail(data);}
void CClientSocket::Browse(void){int nCount=theApp.m_list.GetCount();this->Send(&nCount,sizeof(nCount));POSITION pos=theApp.m_list.GetHeadPosition();while(pos){SData data=theApp.m_list.GetNext(pos);this->Send(&data,sizeof(data));}}

0 0