![]() |
|
#1
|
||||
|
||||
[PROGRAM] Winsock ProgrammingBasic 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:
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:
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:
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:
The Accept used by the listening socket to accept a new connection from another socket. CPP / C++ / C Code:
Now the sockets are ready for sending and receiving data to each other. CPP / C++ / C Code:
Finally, when the application exit, we need to close the sockets. CPP / C++ / C Code:
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. __________________
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
|
|||
|
|||
Re: [PROGRAM] Winsock Programminghi,
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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