资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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;Char 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 application 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 version 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, 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 portInt 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.listen () 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.SOCKET 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 () 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 data); / / 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 ();Return 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; / / bufferInt 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 failed, 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)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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号