Home PHP Libraries Other libraries Ratchet - Sockets library for Web
Ratchet - Sockets library for Web
套接字的编程步骤
在使用之前须链接库函数:工程->设置->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()();//必须调用这个函数清除参数
}


Disclaimer

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

Raphael, a Javascript library for drawing vector graphics in web pages_jquery Raphael, a Javascript library for drawing vector graphics in web pages_jquery

16 May 2016

Raphael is a Javascript library for drawing vector graphics in web pages. It uses SVG W3C recommended standards and VML as the basis for creating graphics. You can easily create various complex bar charts, pie charts, and curves by manipulating the DOM through JavaScript. Pictures and other charts will be introduced in detail next. Friends who are interested can learn more.

Is there a simple and lightweight CSS library that can be directly applied to HTML5 web pages? No need for complicated CSS libraries. _html/css_WEB-ITnose Is there a simple and lightweight CSS library that can be directly applied to HTML5 web pages? No need for complicated CSS libraries. _html/css_WEB-ITnose

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.

How Do I Link Static Libraries That Depend on Other Static Libraries? How Do I Link Static Libraries That Depend on Other Static Libraries?

13 Dec 2024

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

How to import third-party libraries in ThinkPHP How to import third-party libraries in ThinkPHP

03 Jun 2023

Third-party class libraries Third-party class libraries refer to other class libraries besides the ThinkPHP framework and application project class libraries. They are generally provided by third-party systems or products, such as class libraries of Smarty, Zend and other systems. For the class libraries imported earlier using automatic loading or the import method, the ThinkPHP convention is to use .class.php as the suffix. Non-such suffixes need to be controlled through the import parameters. But for the third type of library, since there is no such agreement, its suffix can only be considered to be php. In order to easily introduce class libraries from other frameworks and systems, ThinkPHP specifically provides the function of importing third-party class libraries. Third-party class libraries are uniformly placed in the ThinkPHP system directory/

Use jquery.noConflict() to solve the problem of conflicts between jquery library and other libraries Use jquery.noConflict() to solve the problem of conflicts between jquery library and other libraries

20 Jun 2017

When developing with jQuery, you may also use other JS libraries, such as Prototype, but conflicts may occur when multiple libraries coexist; if conflicts occur, you can solve them through the following solutions: 1. jQuery libraries in other Import the library before and use the jQuery (callback) method directly such as:

What are linux dependency packages What are linux dependency packages

24 Mar 2023

Linux dependency packages refer to "library files". Most dependency packages are library files, including dynamic libraries and static libraries. Linux systems, like other operating systems, are modular in design, which means that functions depend on each other, and some Functions require some other functions to support them, which can improve code reusability.

See all articles