Socket programming based on TCP (connection-oriented)
First, the client:
1. Open a socket (socket);
2, initiate connection request (connect);
3, if the connection is successful, then the data exchange (read, write, send, recv);
4, the data exchange is complete, close the connection (shutdown, close);
Second, server-side:
1. Open a socket (socket);
2. Bind the socket to the server address (BIND);
3, the designated socket is the server socket (listen), to prepare the connection request;
4, Waiting for connection request (connect);
5, if the connection request to, then the connection establishes, carries on the data exchange (read, write, send, recv);
6, the data exchange is complete, close the connection (shutdown, close);
UDP-based socket programming for non-connected
One, client \ server side:
1. Open a socket (socket);
2. Bind the socket to the specified server address and port (BIND);
3, the exchange of data (read, write, send, recv);
4, the data exchange is complete, close the connection (shutdown, close);
Third, MFC's support for sockets:
1, create CAsyncSocket object;
2. Send and receive Data report (SendTo, recvfrom);
3, connect the server (connect);
4, receive connection (Listen);
5, send and receive streaming data (send, receive);
6. Close the socket (close);
7, Error treatment (GetLastError).
Iv. Examples: web chat Tools
Client:
1 、。。。。
2. Use the Class Wizard to overload the CAsyncSocket class to generate a new Mysocket class.
3. Use the Class Wizard to overload CAsyncSocket's onreceive (int nerrorcode) and onsend (int nerrorcode), OnConnect (int nerrorcode) functions.
void mysocket::onreceive (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
Get the dialog pointer
ctestapp*papp= (ctestapp*) AfxGetApp ();
ctestdlg*pdlg= (ctestdlg*) papp->m_pmainwnd;
Insert a message into the edit box
Char *pbuf=new char[4096];
int ibufsize=4096;
int IRCVD;
CString STRRECVD;
Ircvd=receive (pbuf,ibufsize);
if (ircvd==socket_error)
{
Pdlg->messagebox ("Socket_error");
}
Else
{
Pbuf[ircvd]=null;
pdlg->m_recmsg+= "Server:";
pdlg->m_recmsg+=pbuf;
pdlg->m_recmsg+= "\ r \ n";
Pdlg->refreshscreen ();
}
Delete pbuf;
Casyncsocket::onreceive (Nerrorcode);
}
void mysocket::onsend (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
Casyncsocket::onsend (Nerrorcode);
}
void Mysocket::onconnect (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
ctestapp*papp= (ctestapp*) AfxGetApp ();
ctestdlg*pdlg= (ctestdlg*) papp->m_pmainwnd;
int Iresult=nerrorcode;
CString buffer;
int Namelen;
if (iresult!=0)
{
Buffer. Format ("Connection server failed. \ r \ n ");
pdlg->m_recmsg+=buffer;
}
Else
{
Namelen=sizeof (sockaddr_in);
Buffer. Format ("Successfully connected to server%s:%d.\r\n", pdlg->m_ipstr,pdlg->m_port);
pdlg->m_recmsg+=buffer;
Pdlg->getdlgitem (idc_send)->enablewindow (TRUE);
Pdlg->getdlgitem (IDOK)->enablewindow (TRUE);
}
Pdlg->refreshscreen ();
Casyncsocket::onconnect (Nerrorcode);
}
4, in the C*dlg class
In the header file:
#include "MySocket.h"
Mysocket M_socket;
CString M_ipstr;
. CPP file:
void Ctestdlg::onsend ()
{
Todo:add your control notification handler code here
int Ilen;
int isent;
UpdateData (TRUE);
if (m_msg!= "")
{
Ilen=m_msg. GetLength ();
Isent=m_socket. Send (LPCTSTR (m_msg), Ilen);
if (isent==socket_error)
{
MessageBox ("Connection failed, please try again!") ");
Connect=false;
}
Else
{
m_recmsg+= "client:" +M_MSG;
m_recmsg+= "\ r \ n";
UpdateData (FALSE);
}
}
}
void Ctestdlg::refreshscreen ()
{
UpdateData (FALSE);
}
void Ctestdlg::onconnect ()
{
if (! AfxSocketInit ())
{
AfxMessageBox ("idp_sockets_init_failed");
return;
}
GetDlgItemText (IDC_IPADDRESS1,M_IPSTR);
M_socket.m_hsocket=invalid_socket;
UpdateData (TRUE);
BOOL Flag=m_socket. Create ();
if (!flag)
{
AfxMessageBox ("SOCKET ERROR");
Return
}
M_socket. Connect (M_ipstr,m_port);
}
void Ctestdlg::onok ()
{
Todo:add Extra Validation here
M_socket. Close ();
Cdialog::onok ();
}
Server-side:
1 、。。。。
2. Use the Class Wizard to overload the CAsyncSocket class to generate a new Mysocket class.
3. Use the Class Wizard to reload CAsyncSocket onreceive (int nerrorcode) and onsend (int nerrorcode) functions.
void mysocket::onreceive (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
ctestapp*papp= (ctestapp*) AfxGetApp ();
ctestdlg*pdlg= (ctestdlg*) papp->m_pmainwnd;
Insert a message into the list box
Char *pbuf=new char[4096];
int ibufsize=4096;
int IRCVD;
CString STRRECVD;
Ircvd=receive (pbuf,ibufsize);
if (ircvd==socket_error)
{
Pdlg->messagebox ("Socket_error");
}
Else
{
Pbuf[ircvd]=null;
pdlg->m_msg+= "Client:";
pdlg->m_msg+=pbuf;
pdlg->m_msg+= "\ r \ n";
Pdlg->refreshscreen ();
}
Delete pbuf;
Casyncsocket::onreceive (Nerrorcode);
}
void mysocket::onsend (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
Casyncsocket::onsend (Nerrorcode);
}
4. Create a new myServerSocket class and add Mysocket * m_socket, which is the socket pointer after receiving the request.
Mysocket * M_socket;
void cmyserversocket::onaccept (int nerrorcode)
{
Todo:add your specialized code here and/or call the base class
ctestapp*papp= (ctestapp*) AfxGetApp ();
ctestdlg*pdlg= (ctestdlg*) papp->m_pmainwnd;
Show connection messages
pdlg->m_msg= "Client connection to Server";
pdlg->m_msg+= "\ r \ n";
Pdlg->refreshscreen ();
Pdlg->getdlgitem (idc_send)->enablewindow (TRUE);
Pdlg->getdlgitem (IDOK)->enablewindow (TRUE);
mysocket* psocket=new mysocket ();
if (Accept (*psocket))
{
Psocket->asyncselect (Fd_read);
M_socket=psocket;
}
Else
{
Delete Psocket;
}
Casyncsocket::onaccept (Nerrorcode);
}
5, the C*dlg class
void Ctestdlg::refreshscreen ()
{
UpdateData (FALSE);
}
void Ctestdlg::onlisten ()//Create server
{
Todo:add your control notification handler code here
UpdateData (TRUE);
if (! AfxSocketInit ())
{
AfxMessageBox ("idp_sockets_init_failed");
return;
}
BOOL Flag=m_serversocket. Create (M_port);
if (!flag)
{
AfxMessageBox ("SOCKET ERROR");
Return
}
Flag=m_serversocket. Listen (1);
if (!flag)
{
AfxMessageBox ("SOCKET ERROR");
Return
}
Setdlgitemtext (Idc_listen, "listening");
}
void Ctestdlg::onsend ()
{
Todo:add your control notification handler code here
int Ilen;
int isent;
UpdateData (TRUE);
if (m_sendmsg!= "")
{
Ilen=m_sendmsg. GetLength ();
Isent=m_serversocket.m_socket->send (LPCTSTR (m_sendmsg), Ilen);
if (isent==socket_error)
{
MessageBox ("Connection failed, please try again!") ");
}
Else
{
m_msg+= "server:" +M_SENDMSG;
m_msg+= "\ r \ n";
UpdateData (FALSE);
}
}
}
void Ctestdlg::onok ()
{
Todo:add Extra Validation here
m_sendmsg= "Server Exit";
UpdateData (FALSE);
OnSend ();
M_serversocket. Close ();
Cdialog::onok ();
}
VC: Developing applications using Windows Sockets