GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 4.67 average.
  #1  
Old 11-May-2004, 04:09
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough

[PROGRAM] Winsock Programming


Basic Socket Programming

License: None.

Brief Description:
This is another program I made when I was begining to learn MFC. This program Illustrates the simple usage of the CAsyncSocket class provided by MFC, which is useful for socket programming. The program will connect two PC in a network and enables chating by the two PC.

The CAsyncSocket class is a wrapper class for the asynchronous Winsock calls. It has a number of useful functions that facilitate using the Winsock API. Here I list Some of them:
  1. Accept :Handles an incoming connection on a listening socket, filling a new socket with the address information.
  2. Close :Closes the socket.
  3. Connect :Connects the socket to a remote address and port.
  4. Create :Completes the initialization process begun by the constructor.
  5. Listen :Instructs a socket to watch for incoming connections.
  6. OnAccept :Handles the Windows message generated when a socket has an incoming connection to accept (often overridden by derived classes).
  7. OnClose :Handles the Windows message generated when a socket closes (often overridden by derived classes).
  8. OnConnect :Handles the Windows message generated when a socket becomes connected or a connection attempt ends in failure (often overridden by derived classes).
  9. OnReceive :Handles the Windows message generated when a socket has data that can be read with Receive() (often overridden by derived classes).
  10. OnSend :Handles the Windows message generated when a socket is ready to accept data that can be sent with Send() (often overridden by derived classes).
  11. Receive :Reads data from the remote socket to which this socket is connected.
  12. Send :Sends data to the remote socket to which this socket is connected.

There are more function from this class, but I just listed functions used in this program.

The Code:

First, look at the MySocket class which is derived from the CAsyncSocket class and overides some of the function, which are:

CPP / C++ / C Code:
virtual void OnAccept(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnClose(int nErrorCode);
virtual void OnReceive(int nErrorCode);
virtual void OnSend(int nErrorCode); 

each of these function check if an errors occured, and call the appropriate function in the dialog class 'CSockDlg'. This CSockDlg is where most of the dialog message handling is done.

CPP / C++ / C Code:
CMySocket m_sConnectSocket;
CMySocket m_sListenSocket;

These two will represent the connecting socket and the listening socket.
And those two will be created and connected to the specified port and host address. The code below show how to create ,Listen and connect the sockets class. To connect, both the hostName and the port need to be given and the listening socket only needs the port number to listen.
Make sure that only one socket listen to a port at a time, which mean two sockets can't litsen to a port at the same time.

CPP / C++ / C Code:
         // create a default socket
         m_sConnectSocket.Create();
         m_sListenSocket.Create();

         // Open the connection to the server
         m_sConnectSocket.Connect(HostName,PortNum);
         // Listen for connection requests
         m_sListenSocket.Listen();

The Accept used by the listening socket to accept a new connection from another socket.

CPP / C++ / C Code:
// Accept the connection request
     m_sListenSocket.Accept(m_sConnectSocket);

Now the sockets are ready for sending and receiving data to each other.

CPP / C++ / C Code:
m_sConnectSocket.Send(data,dataLenght);
m_sConnectSocket.Receive(data, dataLenght);

Finally, when the application exit, we need to close the sockets.

CPP / C++ / C Code:
// Close the connected socket
     m_sConnectSocket.Close();


This program has been tested in a LAN network with Win98 and Win2K. I'm not sure if it runs ok in Windows XP.

Enjoy.
Regards.
Attached Files
File Type: zip Sock.zip (71.4 KB, 1308 views)
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #2  
Old 09-Mar-2007, 00:38
gowri gowri is offline
New Member
 
Join Date: Mar 2007
Posts: 1
gowri is on a distinguished road

Re: [PROGRAM] Winsock Programming


hi,
i am in very beginning state of winsock programming
your article is very useful for me
and i want to know how to program if the server socket has to accept more than one connection request at the same port number
pl. help me
awaiting for ur reply


regards
gowri
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Help with windows programming Mjkramer21 C++ Forum 12 16-Apr-2004 00:50
New to programming punkin111 Apache Web Server Forum 5 08-Mar-2004 06:09
I need programming help... dewil C Programming Language 10 04-Mar-2004 08:53
C programming help!!! sanman10535 C Programming Language 4 27-Feb-2004 17:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.