套接字的編程步驟 在使用之前須鏈接庫(kù)函數(shù):工程->設(shè)置->Link->輸入ws2_32.lib,OK! SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);//創(chuàng)建套接字(socket)。 SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//轉(zhuǎn)換Unsigned short為網(wǎng)絡(luò)字節(jié)序的格式 addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); 客戶端代碼如下: #include <Winsock2.h> #include <stdio.h> void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData );加載套接字庫(kù) if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup()( ); return; } SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);創(chuàng)建套接字(socket)。 SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));向服務(wù)器發(fā)出連接請(qǐng)求(connect)。 char recvBuf[100];和服務(wù)器端進(jìn)行通信(send/recv)。 recv(sockClient,recvBuf,100,0); printf("%s\n",recvBuf); send(sockClient,"This is lisi",strlen("This is lisi")+1,0); closesocket(sockClient);關(guān)閉套接字。 WSACleanup()();//必須調(diào)用這個(gè)函數(shù)清除參數(shù) }
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article

24 Jun 2016
Is there a simple and lightweight CSS library that can be directly applied to HTML5 web pages? No need for complicated CSS libraries.

20 Mar 2025
The article discusses using WebSockets for real-time communication, detailing setup, message handling, and best practices. It highlights WebSocket benefits and challenges in enhancing user experience and managing scalability and security.

13 Dec 2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

14 Jan 2025
GSAP GreenSock Animation Platform (GSAP) is a JavaScript library that lets users create animations for web development. It's used to animate a variety of elements, including SVG, UI, text, and WebGL.? ScrollReveal ScrollReveal is a

18 Apr 2025
React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

28 Oct 2024
Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...


Hot Tools

PHP library for dependency injection containers
PHP library for dependency injection containers

A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking

Small PHP library for optimizing images
Small PHP library for optimizing images
