socket programming (d) - the server instance

  // Server server-side Server.cpp 
1
// custom console application entry point. 2 // . 3 . 4 #include " the stdafx.h " . 5 #include <winsock2.h> . 6 #include <the iostream> . 7 #pragma Comment (lib, "ws2_32.lib") . 8 the using namespace STD; . 9 int main () 10 { . 11 WORD wVersion; 12 is WSADATA WSADATA; 13 is int ER; 14 // 1. initialization version information 15 wVersion MAKEWORD = ( . 1 ,1 ); 16 // loading socket library . 17 ER = WSAStartup (wVersion, & WSADATA); 18 is IF (ER =! 0 ) . 19 { 20 is return - 1 ; 21 is } 22 is // detected the version information of the socket 23 is IF (! LOBYTE (wsaData.wVersion) = . 1 ! || hibyte (wsaData.wVersion) = . 1 ) 24 { 25 WSACleanup (); 26 is return - 2 ; 27 } 28 29 //2. Create a server-side socket 30 SOCKET sockSer = Socket (AF_INET, SOCK_STREAM, 0 ); 31 is / * 32 Prototype: SOCKET Socket (AF int, int type, int Protocol) 33 is Parameters: 34 is af: denotes a network address family now there is only one valid value that AF_INET, on behalf of internet address family. 35 type: represents the network protocol type, SOCK_DGRAM UDP protocol on behalf of, SOCK_STREAM on behalf of the TCP protocol.   36 protocol: specify a network address family special agreement, currently useless, assigned to 0. 37 * / 38 // 3. Bind the socket 39 / * 40 NT the bind (SOCKET S, const struct the sockaddr FAR * name, int the namelen); 41 is Parameters: 42 is s: bound socket 43 name: sockaddr structure is a pointer to this structure contains the address and port to bind. 44 is namelen: a length of the second parameter name; 45 if the user do not care value or port address, then the address may be set to INADDR_ANY, and Port 0. 46 For a multi-host interface to use INADDR_ANY to specify a wildcard address, so that any IP address of the host matches. 47 * / 48 SOCKADDR_IN Addr_In; 49 addr_in.sin_family = AF_INET; // address family 50 addr_in.sin_port = the htons ( 7000 ); // port 51 is addr_in.sin_addr.S_un.S_addr = htonl (INADDR_ANY); // the IP 52 is the bind (sockSer, (the sockaddr *) & Addr_In, the sizeof (Addr_In)); 53 is 54 is //4. listening socket is ready to accept a request sent by a client 55 / * 56 is int the listen (SOCKET S, int backlog); 57 is Parameters: 58 s: the need for listening the Socket; 59 backlog: the maximum number of connections; 60 * / 61 is the listen (sockSer, . 5 ); 62 is // 5. the definition of the structure, the client accepts IP address and port number 63 is / * 64 SOCKET accept (SCOKET S, the sockaddr struct FAR * addr, int addrlen FAR *); 65 parameters: 66 s: listening socket 67 addr: address of the client is connected to the store port information; 68 addrlen: addr of length 69 * / 70 SOCKADDR_IN client_addr; 71 is int len = the sizeof (client_addr); 72 the while ( to true ) 73 is { 74 // waiting for client requests 75 COUT << " waits for a client requests .......... " << endl; 76 SOCKET = Accept socClient (sockSer, (a SOCKADDR *) & client_addr, & len); 77 char sendBuf [ 1024 ]; 78 // formatted string 79 sprintf (sendBuf, " % S C ++ tutorial " , inet_ntoa (client_addr.sin_addr)) ; 80 //6. The transmission data 81 Send (socClient, sendBuf, strlen (sendBuf) + . 1 , 0 ); 82 / * 83 int Send (S SOCKET, FAR const char * buf, int len, int the flags); 84 Parameters: 85 s : is a socket connection has been established. 86 buf: points to a buffer which contains the data to be delivered. 87 len: length of the buffer. 88 flags: receive data identifying the way, if no special requirements may be set to 0. 89 90 calls the send function to send data to the client, pay attention to the use of this function requires the use of a socket socket connection that has been established, and 91 instead of the socket for listening. 92 * / 93 // 7 receives the data 94 char recvbuf [1024 ]; 95 the recv (socClient, recvbuf, strlen (recvbuf) + . 1 , 0 ); 96 / * 97 int the recv (SOCKET S, FAR char * buf, int len, int the flags); 98 Parameters: 99 s: establishing ready to receive data after the socket connection. 100 buf: pointer to the buffer, to store the received data. 101 len: length of the buffer. 102 flags: receive data identifying the way, if no special requirements may be set to 0. 103 after completing transmission of data may also receive data from the client, which may be used recv function, to be noted that the first parameter of the function that should be established after the connection socket, 104 and defining a character array recvbuf, for store the received data. 105 * / 106 COUT << " the received data:" << recvbuf << endl; 107 // 8. The socket is closed 108 the closesocket (socClient); 109 } 110 111 return 0 ; 112 }

 

Guess you like

Origin www.cnblogs.com/506941763lcj/p/11032236.html