Windows 95 Socket provides an interface for web programming in Microsoft Windows 95, which is developed on the basis of UNIX Socket, not only retains the original style of UNIX Socket, but also integrates into Windows 95. New features, which allows users to communicate with the application and application 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)
This function is used to build a Socket. 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 LPSZSSOCKETDRESS = NULL)
The function of this function is to connect the Socket port to the network address. The parameters are in the same.
(3) BOOL Listen (int NConnectionbackLog = 5)
The function of 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)
The function of this function is that the first connection request on the queue and establishes a socket with the same characteristics with the Socket. In this, the RConnectedSocket shows a new Socket.
(5) Bool Connect (LPCTSTSTR LPSZHOSTADDRESS, UINT NHOSTPORT)
The function of this function is to make a request. 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 off 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 implementation of the two methods is 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, use the CSocketFile class and the Archive class
(1) Server
// construct a socket
Csocket Socksrvr;
// Create the socket
Socksrvr.create (nPort);
// start listening
Socksrvr.listen ();
// 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 to pass data
AROUT << DWVALUE;
/ * 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, use CSocket's member function
(1) Server
// Socket Initial
IF (! AFXSocketinit ()) {
Messagebox ("Windowssocket 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 listening
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 (! chatReceive.create ()) {
MessageBox ("ReceiveSocket Create
Failed! "," Receive ", MB_ICONSTOP);
Return;
}
Else {
// establishes a connection to a peer socket
ChatReceive.Connect (straddr, nport);
}
// receive the cstring
CHATRECEIVE.RECeiveFrom (CSReceiveText, Cscounts, straddr, nport);
// Close the socket
CHATRECEIVE.CLOSE ();
The completed work of the above two is: send a string string by the Server process, and the client is connected. The meaning of Straddr and NPort is the same as the method 1; CSsendText and CSReceiveText are sending and receiving strings; CSCounts is a string length, which requires a length of less than or equal to transmission length, otherwise it will result in data. Transmission errors. In addition, in the program, it is necessary to add the header article AFXSock.h because it is in AfXSock.h because of the relevant state of the CSocket class.
It is not difficult to develop from the above two methods, and the method 1 is suitable for the communication of multiple different types of data, and the method is suitable for the communication of the string, and what kind of method for choosing the need for the need for a specific application.