Implement Windows 95 Socket Programming with Visual C 4.0
Author: Li Daqi Windows 95 Socket provides a Microsoft Windows 95 carried out a programming interface to the network, which is based on Unix Socket evolved, not only retains the Unix Socket original style, but also into a suitable The new features of Windows 95, which allows users to communicate between processes and applications using the Windows 95 Socket API. Windows 95 defines a Socket write loop in the Internet branch domain, making the use of Socket to make the user's work in the network protocol without having to have an uncommon solution. In addition, this programming program can be rapidly placed into any network system that supports Socket. The CSocket class is provided in Microsoft Windows Class Library (MFC) to implement network communication. The CSOCKET class is given in Figure 1. The following face will be introduced to the CSocket class associated with Visual C 4.0 in Windows 95 (these member functions are actually successively from the CasyncSocket class). (1) BOOL Create (UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, LPCTSTR lpszSocketAddress = NULL) The function to establish Socket with. Where nsocketport is the selected Socket port, it is generally greater than 1023. If the parameter is 0, the system is selected, the default is 0; nsockettype is a socket type: SOCK_STREAM is expressed as a streamlined text, SOCK_DGRAM Indicates that the data is set, the default value is sock_stream; LEVENT identifies which work to complete, the default value is fd_read | fd_write | fd_oob | fd_close; lpszsockaddress is the network address information structure pointer, including the network address, The default is NULL. (2) Bool Bind (Uint NSocketport, LPCTSTR LPSZSOCKETDRESS = NULL) This function is to connect the Socket port with the network address. The parameters are in the same. (3) Bool Listen (int NConnectionbackLog = 5) This function is to wait for Socket request.
In this, the NConnec-TionBackLog represents the length of the queue, the default value is the maximum value 5. (4) Virtual Bool Accept (CasyncSocket & rConnectedSocket, SockAddr * lpsockaddr = null, int * lpsockaddrlen = null) This function is the first connected request on the queue and build a socket with the Socket. In this, the RConnectedSocket shows a new Socket. (5) BOOL Connect (LPCTSTSTR LPSZHOSTADDRESS, UINT NHOSTPORT) This function is to make requests. Among them, LPSZHOSTADDRESS and NHOSTPORT are the web address and socket port numbers that are connected to the requested process. (6) Virtual void close () The function of this function is to turn the Socket. There are two ways to communicate directly with the CSocket class: one is to implement the CSocketFile class and the Archive class, and the other is to implement the members of the CSocket Receive, Send, ReceiveFrom, Sendto, Listen, and Accept, etc. (these members) The function is actually inherited from the CasyncSocket class). The actual steps of the two methods are as follows: Server: construct-> creat-> bind -> listen-> accept-> send-> close; Cilent: Construct -> Creat-> Connect-> Receive-> Close. Next, I use Visual C 4.0's code segment to introduce how to use the above two methods to implement the SOCKET programming.
1. Implement (1) Server // Construct a socket csocket socksrvr; // crete the socket socksr.create (nport); // start); // start); // construct a new, empty socket CSocket sockRecv; // accept connection sockSrvr.Accept (sockRecv); // construct file object CSocketFile file (& sockRecv); // construct an archive CArchive arIn (& file, CArchive :: load); / * or * / _ CArchive arOut ( & file, carchive :: store); // Use the archive to pass data arin >> dwvalue; / * or * / arout << dwvalue; (2) Client // Construct a socket csocket sockclient; // Create The socket sockclient. Create (); // seek a connection sockClient.Connect (strAddr, nPort); // construct file object CSocketFile file (& sockClient); // construct an archive CArchive arIn (& file, CArchive :: load); / * or * / _Carchive AROUT (& File, CARCHIVE :: Store); // Use the archive; / * or * / arin >> dwvalue; The two modes of the client / Server model are used to end a data variable in two processes. Among them, NPORT is the port number of the socket, and Straddr is the IP address of the machine (e.g., 202.197.1.3 or ftp://redalert.com, etc.), these two variables are in Server and Client. When the Server process is running to Listen, it will be awake until the Client process executes Connect, and the latter two entries will start the data.
2. Implement (1) Server // Socket Initial if (! AFXSOCKET Initial Failed! "," Send ", MB_ICONSTOP); RETURN;} // Construct Two Socket Csocket Chatsend , Server; // Creat a socket if (! chatsend.create (nport)) // nport = 1025 MessageBox ("SendSocket Create Failed!", "Send", MB_ICONSTOP); Else {// Associates a local address with the socket Chatsend.bind (nprot, straddr); // straddr = "202.196.111.1" // start listenging chatsend.listen (); // creat a new socket and accepts a connection on // the socket chatsend.accept (server); } // send a cstring server.sendto (cssendtext, cscounts, nport, straddr); // close the two socket server.close (); chatsend.close (); (2) Client // Socket Initial if (! AFXSocketinit) )) {MessageBox ("WindowsSocket Initial Failed!", "Receive", MB_ICONSTOP); RETURN;} // Construct A Socket Csocket ChatRecieve; // Creat A Socket IF (! C hatReceive.Create ()) {MessageBox ( "ReceiveSocket create failed!", "Receive", MB_ICONSTOP); return;} else {// Establishes a connection to a peer socket ChatReceive.Connect (strAddr, nPort);} // Receive The CSRING CHATRECEIVE.ReceiveFrom (CSReceiveText, Cscounts, straddr, nport); // close the socket chatnessive.close (); the above two processes is completed by: Send a string by the Server process, the Client process is received.