套接字的编程步骤 在使用之前须链接库函数:工程->设置->Link->输入ws2_32.lib,OK! SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);//创建套接字(socket)。 SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//转换Unsigned short为网络字节序的格式 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 );加载套接字库 if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup()( ); return; } SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);创建套接字(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));向服务器发出连接请求(connect)。 char recvBuf[100];和服务器端进行通信(send/recv)。 recv(sockClient,recvBuf,100,0); printf("%s\n",recvBuf); send(sockClient,"This is lisi",strlen("This is lisi")+1,0); closesocket(sockClient);关闭套接字。 WSACleanup()();//必须调用这个函数清除参数 }
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
24Jun2016
Is there a simple and lightweight CSS library that can be directly applied to HTML5 web pages? No need for complicated CSS libraries.
![How Do I Link Static Libraries That Depend on Other Static Libraries?](https://img.php.cn/upload/article/001/246/273/173408941480807.jpg)
13Dec2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...
![Animation Library You Can Use for your Web Apps](https://img.php.cn/upload/article/001/246/273/173685082419771.png)
14Jan2025
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
![How to Silence TensorFlow\'s Debugging Output?](https://img.php.cn/upload/article/001/246/273/173013069196898.jpg)
28Oct2024
Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...
![How Does jQuery Simplify DOM Manipulation for Web Developers?](https://img.php.cn/upload/article/001/246/273/173584251998599.jpg)
03Jan2025
Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...
![Which native Java image processing library is right for you?](https://img.php.cn/upload/article/001/246/273/173023154939075.jpg)
30Oct2024
Native Java Image Processing Libraries for High-Quality ResultsAs you have encountered limitations with ImageMagick and JAI, let's explore other...
![](/static/imghwm/down_right.png)
![](/static/imghwm/taglogo.png)
Hot Tools
![PHP library for dependency injection containers](https://img.php.cn/upload/manual/000/000/001/5e21721e79a2b232.png)
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
![](/static/imghwm/taglogo.png)