《tcp、ip通信(tcp and ip communications)》由会员分享,可在线阅读,更多相关《tcp、ip通信(tcp and ip communications)(12页珍藏版)》请在金锄头文库上搜索。
1、tcp、ip通信(TCP and IP communications)Sockets (2012-09-08 22:02:57) TCP sockets programming gradually we label Reprint: sockets programmingIn Windows Sockets, specify the IP address and port number using the SOCKET_IN structure.Struct socket_inShort sin_family;U_short sin_port;Struct in_addr sin_addr;C
2、har sin_zero8;Where sin_family must be AF_INET;The sin_port range is 102449151;Sin_addr saves the IP address to a value of 4 bytes.than basic TCP programming function thanYou need to include the header file #include 1.WSAStartup () functionIs the first Windows Sockets function that the socket applic
3、ation must call.The following code shows how the application that supports only Windows Sockets 1.1 is called WSAStartup ():WORD wVersionRequested; / / DLL versionWSADATA wsaData; / / dynamic library informationInt err;WVersionRequested = MAKEWORD (1, 1); the /MAKEWORD () function specifies the vers
4、ion numberErr = WSAStartup (wVersionRequested, &wsaData);If (ERR = = 0) The available DLL / / not foundReturn;If (LOBYTE (wsaData.wVersion) = 1! | HIBYTE (wsaData.wVersion) = 1!)Return / / 2.2 version of the DLL is notWSACleanup ();Return;2.socket () functionCreate socket.SOCKET s = socket (AF_INET,
5、 SOCK_STREAM, IPPROTO_TCP); / / create a UDP, the two parameter is SOCK_DGRAM, IPPROTO_UDPIf (INVALID_SOCKET = s)/ / socket creation failed3.bind () functionBind a socket to a known address.SOCKET s; / / socketStruct sockaddr_in servAddr; / / server socket addressInt nServPort = 5500; / / server por
6、tInt nErrCode; / / error code/ / define server addressServAddr.sin_famliy = AF_INET;ServAddr.sin_addr.S_addr = htonl (INADDR_ANY);ServAddr.sin_port = htons (nServPort);Bind / socketNErrCode = bind (s, (SOCKADDR*) &servAddr, sizeof (servAddr);If (SOCKET_ERROR = nErrCode)Bind the socket / failure4.lis
7、ten () functionSet socket to monitor mode.SOCKET s; / / socketInt nErrCode; / / error code/ / monitorNErrCode = listen (s, 3); / / the second parameter represents the maximum queue length for connectionIf (SOCKET_ERROR = nErrCode)Monitor / / failure5.accept () functionAccept a connection request.SOC
8、KET sListen; / / listening socketSOCKET sAccept; / / accept socketSockaddr_in addrClient; / / client.Int addrClientlen = sizeof (addrClient); / / length/ / accept client requestsSAccept = accept (sListen, (SOCKADDR*) &addrClient, &addrClientlen);If (INVALID_SOCKET = sAccept)/ / accept failure6.recv
9、() functionReceive data.SOCKET s;Char buf128; / / receive data bufferInt nReadLen;/ / receive dataNReadLen = recv (s, buf, 128, 0);If (SOCKET_ERROR = nReadLen)/ / receive failure7.send () functionSend data.SOCKET s;Char buf128; / / receive data bufferInt retVal; / / return valueStrcpy (buf, send dat
10、a); / / copy data/ / send dataRetVal = recv (s, buf, strlen (buf), 0);If (SOCKET_ERROR = retVal)/ / send failure8.closesocket () functionClose the socket and free the resource.9.shutdown () function/ / string output transmissionThe exit / /Closesocket (sServer);Closesocket (sClient);WSACleanup ();Re
11、turn 0;-TCP - client(send the string to the server and exit)/ / console program, client.cpp file complete program#include stdafx.h#include #pragma comment (LIB, wsock32.lib)#define BUF_SIZE 64WSADATA wsd;SOCKET sHost; / / server socketSOCKADDR_IN servAddr; / / server addressChar bufBUF_SIZE; / / buf
12、ferInt retVal; / / return valueInt main (int, argc, char*, argv)The socket initialization / / DLLIf (WSAStartup (MAKEWORD (2,2), &wsd) = = 0)Printf (WSAStartup failed, n);Return -1;The / / create a socketSHost = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);If (INVALID_SOCKET = sHost)Printf (socket fai
13、led, n);WSACleanup ();Return -1;The connection to the server / /ServAddr.sin_family = AF_INET;ServAddr.sin_addr.s_addr = inet_addr (127.0.0.1);ServAddr.sin_port = htons (4999);Int nServAddLen = sizeof (servAddr);RetVal = connect (sHost, (LPSOCKADDR) &servAddr, nServAddLen);If (SOCKET_ERROR = retVal)
14、Printf (Connect failed, n);Closesocket (sHost);WSACleanup ();Return -1;The / / send dataZeroMemory (buf, BUF_SIZE);Strcpy (buf, Tcp, Socket);RetVal = send (sHost, buf, strlen (buf), 0); / / send dataIf (SOCKET_ERROR = retVal)Printf (send failed, n);Closesocket (sHost);WSACleanup ();Return -1;The exit / /Closesocket