Write server-forwarded chat rooms by VC

Source: Internet
Author: User
Tags sendmsg

Write server-forwarded chat rooms by VC

This document provides examples in Visual C ++ 6.0 complete self-learning manual.

1. process description
Client
Create a streaming socket on the client
Call connection to send a connection request to the server
After the connection is successful, create the associated csocketfile object and carchive object.
Use the carchive object to transmit data between the client and server
Release the csocketfile object, carchive object, and csocket object.

Server
The server creates a streaming socket
Start listening for connection requests
A connection request calls the accpet function to accept the request and create a new client socket.
After the connection is successful, create the associated csocketfile object and carchive object.
Use the carchive object to transmit data between the client and the server.
After receiving the data sent from a client, the server sends the data to each client connected to the server.
Release the csocketfile object, carchive object, and csocket object.

Two-Way overload Function Description
Client clientsocket needs to overload virtual void onreceive (INT nerrorcode); and virtual void onclose (INT nerrorcode); Function
The server needs to overload virtual void onaccept (INT nerrorcode); Function

Three implementations
1. serialized message text operations
Header file msg. h
Class cmsg: Public cobject
{
Public:
Void serialize (carchive & AR );
Cstring m_strmsg;
Cmsg ();
Virtual ~ Cmsg ();

};
Implementation file MSG. cpp
# Include "stdafx. H"
# Include "MSG. H"
Cmsg: cmsg ()
{
M_strmsg = _ T ("");
}
Cmsg ::~ Cmsg ()
{
}

Void cmsg: serialize (carchive & AR)
{
If (AR. isstoring ())
Ar <m_strmsg;
Else
Ar> m_strmsg;
}

2 client socket class
Header file clientsocket. h
# Include "stdafx. H"
# Include "afxsock. H"
Class cmsg;
Class cclientsocket: Public csocket
{
Public:
Csocketfile * m_psocketfile;
Carchive * m_parchivein;
Carchive * m_parchiveout;
Bool m_bclose;
Public:
Cclientsocket ();
Virtual ~ Cclientsocket ();
Public:
Void sendmsg (cmsg * PMSG );
Void Init ();
Public:
Virtual void onreceive (INT nerrorcode );
Virtual void onclose (INT nerrorcode );
Protected:
};
Implement clientsocket. cpp
# Include "stdafx. H"
# Include "clientsocket. H"
# Include "MSG. H"
Cclientsocket: cclientsocket ()
{
M_parchivein = NULL;
M_parchiveout = NULL;
M_psocketfile = NULL;
M_bclose = false;
}

Cclientsocket ::~ Cclientsocket ()
{
If (m_parchivein)
Delete m_parchivein;
If (m_parchiveout)
Delete m_parchiveout;
If (m_psocketfile)
Delete m_psocketfile;
M_parchivein = NULL;
M_parchiveout = NULL;
M_psocketfile = NULL;
M_bclose = true;
}

 

Void cclientsocket: Init ()
{
M_psocketfile = new csocketfile (this );
M_parchivein = new carchive (m_psocketfile, carchive: load );
M_parchiveout = new carchive (m_psocketfile, carchive: Store );
M_bclose = false;

}

Void cclientsocket: onreceive (INT nerrorcode)
{
 

Do
{
Cmsg MSG;
MSG. serialize (* m_parchivein );
Here we mainly Process accepted data
 
}
While (! M_parchivein-> isbufferempty ());
Csocket: onreceive (nerrorcode );
}

Void cclientsocket: sendmsg (cmsg * PMSG)
{
If (m_parchiveout! = NULL)
{
PMSG-> serialize (* m_parchiveout );
M_parchiveout-> flush ();
}
}
Void cclientsocket: sendmsg (cmsg * PMSG)
{
If (m_parchiveout! = NULL)
{
PMSG-> serialize (* m_parchiveout );
M_parchiveout-> flush ();
}
}
Void cclientsocket: onclose (INT nerrorcode)
{
M_bclose = true;
Csocket: onclose (nerrorcode );
}
3. The server must include not only client sockets, but also a listener socket for serialized data.
Header file serversocket. h
# Pragma once
# Include "stdafx. H"
# Include "afxsock. H"
Class cserverdlg;

Class cserversocket: Public csocket
{
Public:
Cserversocket ();
Virtual ~ Cserversocket ();
Public:
Void Init (uint port );
Uint m_uport;
Public:
Virtual void onaccept (INT nerrorcode );

};
Implement file serversocket. cpp
# Include "stdafx. H"
# Include "serversocket. H"
# Include "clientsocket. H"
# Include "afxsock. H"
# Include "MSG. H"
Cserversocket: cserversocket ()
{
}

Cserversocket ::~ Cserversocket ()
{
}

Void cserversocket: Init (uint port)
{
M_uport = port;

This-> Create (m_uport); // create a server socket
This-> listen (); // starts listening for connection requests.

}

Void cserversocket: onaccept (INT nerrorcode)
{

// Send the Database Change Notification to each server or use the callback function for processing.
Csocket: onaccept (nerrorcode );
}

Send messages to each client in the onaccept processing function
Cptrlist m_plconn; // client socket accepted by the Linked List class Storage
Cserversocket m_listensocket;
Cclientsocket * psocket = new cclientsocket (); // create a client socket
If (m_listensocket.accept (* psocket) // accept the connection request
{
Cmsg MSG;
MSG. m_strmsg = l "Receive message ";
For (position Pos = m_plconn.getheadposition (); pos! = NULL ;)
{// Send a message object to the client socket
Cclientsocket * s = (cclientsocket *) m_plconn.getnext (POS );
S-> sendmsg (& MSG );
}
Psocket-> Init ();/new client socket Initialization
M_plconn.addtail (psocket); // Add it to the list of connected sockets
 
}
Else
{
Delete psocket;
}
}

The above program has been debugged in Visual C ++ 2005

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.