2 socket
1. socket programming server programs based on TCP (connection oriented): 1. create a socket ). 2. bind the socket to a local address and port (BIND ). 3. set the socket to the listening mode and prepare to receive the client request (Listen ). 4. wait for the customer request to arrive. When the request arrives, accept the connection request and return a new socket (accept) corresponding to the connection ). 5. send/Recv ). 6. return, waiting for another customer request. 7. close the set of words. client Program: 1. create a socket ). 2. send a connection request to the server ). 3. send/Recv ). 4. close the socket. 2. socket programming server based on UDP (for non-connection): 1. create a socket ). 2. bind the socket to a local address and port (BIND ). 3. waiting for receiving data (recvfrom ). 4. close the socket. client: 1. create a socket ). 2. send data (sendto) to the server ). 3. close the socket. 3. project Instance socket programming instance based on TCP (connection oriented) 1) related function ----------- load socket library begin ------------- wsastartup --- load socket library, the version negotiation makeword of the socket library --- create a word value wsadata by specifying a value at the same time --- fill in the version information obtained by wsastartup in the wsadata struct lobyte --- get the word low byte hibyte --- get the word height byte wsacleanup --- terminate the use of WinSock, release resources ----------- load socket library end certificate create socket begin ------------- socket --- create a socket ----------- create socket end ------------------------------ bind socket begin ------------- bind --- associate a local address using a socket --- convert to u_long from host byte order to TCP/IP network byte order htons --- convert to a u_short from host byte order to TCP/IP network byte order ----------- bind socket end ------------------------ socket listener begin --------------- listen --- set the socket to the listening mode, listening for connection requests ----------- socket listening end -------------------------- waiting for client connection request begin ------------- accept --- waiting for client connection request ----------- waiting for client connection request end users to send request data begin --------------- send --- to connect the data inet_ntoa of the request sent by the socket --- accept the in_addr type parameter, returns a string of IP addresses in dotted-decimal format ----------- sending request data to the client end -------------------------- receiving data begin --------------- Recv --- receiving data from a connected socket or a bound to an unconnected socket ----------- receive data end ------------- int wsastartup (// The wsastartup function initiates use of the Winsock DLL by a process. word wversionrequested, lpwsadata // [out] pointer to the wsadata structure that is to receive details of the Windows Sockets implementation .); wversionrequested: Specifies the version of the Winsock library to be loaded. the secondary version of the database specified by the high byte, and the primary version specified by the low byte. makeword (X, Y) is available (where X is a low byte (main version), and Y is a high byte (secondary version) (Note: The original handout here is: X is a high byte, and Y is a low byte. For details, refer to the msdn library-Visual Studio 2008 SP1 and modify it) to obtain the correct value of wversionrequested. lpwsadata: a pointer to the wsadata structure. wsadata receives detailed information about the implementation of Windows Sockets. (wsastartup is filled in this structure with information related to the library version it loads.) After wsastartup is called successfully, a wsacleanup call is called at the end to release the resources allocated to the application. int wsacleanup (void); // The wsacleanup function terminates use of the WinSock 2 DLL (ws2_32.dll ). word makeword (// The makeword macro creates a word value by concatenating the specified values. byte blow, // specifies the low-order byte of the new value. byte bhigh // specifies the high-order byte of the new value .); typedef struct wsadata {// The wsadata structure contains information about the Windows Sockets implementation. word wversion; Word whighversion; char szdescription [bytes + 1]; char szsystemstatus [bytes + 1]; unsigned short imaxsockets; unsigned short imaxudpdg; char far * lpvendorinfo;} wsadata, * lpwsadata; byte lobyte (// The lobyte macro retrieves the low-order byte from the specified value. word wvalue); byte hibyte (// The hibyte macro retrieves the high-order byte from the given 16-bit value. word wvalue); -------------------------------------------------- Socket socket (// This function creates a socket that is bound to a specific service provider. int AF, // [in] address family specification. int type, // [in] type specification for the new socket. sock_stream | sock_dgram | sock_raw int protocol // [in] Protocol to be used with the socket that is specific to the indicated address family .); the following list shows the possible value of protocol type: ipproto_ip ipproto_ipv6 ipproto_tcp ipproto_udp sol_socket af_inet // The af_inet address family is the address family for IPv4. using int BIND (// This function associates a local address with a socket. socket S, const struct sockaddr far * Name, int namelen); struct sockaddr {// The sockaddr structure varies depending on the Protocol selected. // specify t for the sin * _ family parameter, sockaddr contents are expressed in network byte order. u_short sa_family; char sa_data [14];}; The sockaddr struct is prepared for all the address families. It usually varies with the network protocol used, it will replace the ADDR struct with other related structs. sa_data is a memory area used to store specific protocol-related address information. for different protocol families, the same struct must be used to replace sockaddr. in the TCP/IP protocol family, addr_in can be used to replace sockaddr. struct sockaddr_in {// In the Internet address family, this structure is used by Windows Sockets to specify a local or remote endpoint // address to which to connect a socket. this is the form of the sockaddr Structure specific to the Internet // address family and can be cast to sockaddr. short sin_family; // address family; must be af_inet. u_short sin_port; // Internet Protocol (IP) port. struct in_addr sin_addr; // ip address in network byte order. char sin_zero [8]; // padding to make structure the same size as sockaddr .}; sockaddr_in: if the call is successful, 0 is returned. If the call is unsuccessful, socket_error is returned. The error information can be obtained using the wsagetlasterror function. typedef struct in_addr {// The in_addr structure represents an IPv4 internet address. union {struct {u_char s_b1, s_b2, s_b3, s_b4;} s_un_ B; // an IPv4 address formatted as four u_chars.struct {u_short s_w1, s_w2;} s_un_w; // an IPv4 address formatted as two u_shorts.u_long s_addr; // an IPv4 address formatted as a u_long .} s_un;} in_addr, * pin_addr, far * lpin_addr; specify the IP address as inaddr_any, allowing an Independent Application to accept responses from multiple interfaces. u_long htonl (// The htonl function converts a u_long from host to TCP/IP network byte order (which is big endian ). _ in u_long hostlong); u_short htons (// The htons function converts a u_short from host to TCP/IP network byte order (which is big-Endian ). _ in u_short hostshort); ------------------------ int listen (// This function places a socket at a state where it is listening for an incoming connection. socket S, int backlog // Maximum length of the waiting connection); ------------------------ socket accept (// This function permits an incoming connection attempt on a socket. socket S, struct sockaddr far * ADDR, // save IP address and port of clients int far * addrlen); ------------------------ int send (// This function sends data on a connected socket. socket S, const char far * Buf, int Len, int flags); char * far inet_ntoa (// The inet_ntoa function converts an (IPv4) internet network address // into a string in Internet standard dotted-decimal format. _ in struct in_addr in); --------------------------------- int Recv (// The Recv function calls es data from a connected socket or a bound connectionless socket. _ in socket S, _ out char * Buf, _ in int Len, _ in int flags );