1.libsocketԴ??
2.CSocketä¹UDPç¼ç¨
3.求聊天系统中客户端通信C/C++源代码!!!
4.libevent、libev框架介绍
libsocketԴ??
源代码奉上,流程图。asp.net系统源码。。这个太简单了,你自己看看。。。。。。dnf 服务端源码。
//TCP
//服务器端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_.lib" )
#define PORT
#define BACKLOG
#define TRUE 1
void main( void )
{
int iServerSock;
int iClientSock;
char *buf = "hello, world!\n";
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
int sin_size;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//监视的端口号
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind调用失败!\n" );
WSACleanup( );
exit( 0 );
}
if( listen( iServerSock, BACKLOG ) == -1 )
{
printf( "listen调用失败!\n" );
WSACleanup( );
exit( 0 );
}
while( TRUE )
{
sin_size = sizeof( struct sockaddr_in );
iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size );
if( iClientSock == -1 )
{
printf( "accept调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "服务器连接到%s\n", inet_ntoa( ClientAddr.sin_addr ) );
if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )
{
printf( "send调用失败!" );
closesocket( iClientSock );
WSACleanup( );
exit( 0 );
}
}
}
/////客户端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_.lib" )
#define PORT
#define BACKLOG
#define TRUE 1
#define MAXDATASIZE
void main( void )
{
int iClientSock;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
int numbytes;
// struct hostent *he;
WSADATA WSAData;
// int sin_size;
/* if( ( he = gethostbyname( "liuys" ) ) == NULL )
{
printf( "gethostbyname调用失败!" );
WSACleanup( );
exit( 0 );
}
*/
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
// ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr );
ServerAddr.sin_addr.s_addr = inet_addr( "..2." );//记得换IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect失败!" );
WSACleanup( );
exit( 0 );
}
numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );
if( numbytes == -1 )
{
printf( "recv失败!" );
WSACleanup( );
exit( 0 );
}
buf[ numbytes ] = '\0';
printf( "Received: %s", buf );
closesocket( iClientSock );
WSACleanup( );
}
/////UDP
//服务器
#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >
#pragma comment( lib, "ws2_.lib" )
#define PORT
#define BACKLOG
#define TRUE 1
#define MAXDATASIZE
void main( void )
{
int iServerSock;
// int iClientSock;
int addr_len;
int numbytes;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );
if( iServerSock == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//监视的端口号
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind调用失败!\n" );
WSACleanup( );
exit( 0 );
}
addr_len = sizeof( struct sockaddr );
numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );
if( numbytes == -1 )
{
printf( "recvfrom调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "got packet from %s\n", inet_ntoa( ClientAddr.sin_addr ) );
printf( "packet is %d bytes long\n", numbytes );
buf[ numbytes ] = '\0';
printf( "packet contains \"%s\"\n", buf );
closesocket( iServerSock );
WSACleanup( );
}
//客户端
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_.lib" )
#define PORT
#define MAXDATASIZE
void main( void )
{
int iClientSock;
struct sockaddr_in ServerAddr;
int numbytes;
char buf[ MAXDATASIZE ] = { 0 };
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
ServerAddr.sin_addr.s_addr = inet_addr( "..2." );//记得换IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) );
if( numbytes == -1 )
{
printf( "sendto调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );
closesocket( iClientSock );
WSACleanup( );
}
CSocketä¹UDPç¼ç¨
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_.lib")
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 sersocket=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN seraddr;
seraddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons();
bind(sersocket,(SOCKADDR*)&seraddr,sizeof(SOCKADDR));
SOCKADDR clientaddr;
int len=sizeof(SOCKADDR);
char revbuf[];
char sendbuf[];
recvfrom(sersocket,revbuf,,0,(SOCKADDR*)&clientaddr,&len);
printf("%s\n",revbuf);
scanf("%s",&sendbuf);
sendto(sersocket,sendbuf,strlen(sendbuf)+1,0,(SOCKADDR*)&clientaddr,len);
closesocket(sersocket);
WSACleanup();
}
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_.lib")
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_DGRAM,0);
SOCKADDR_IN clientaddr;
clientaddr.sin_addr.S_un.S_addr=inet_addr(".0.0.1");
clientaddr.sin_family=AF_INET;
clientaddr.sin_port=htons();
int len=sizeof(SOCKADDR);
char revbuf[];
char sendbuf[];
printf("请è¾å ¥å 容ï¼\n");
while(1)
{
scanf("%s",&sendbuf);
sendto(sockclient,sendbuf,strlen(sendbuf)+1,0,(SOCKADDR*)&clientaddr,len);
recvfrom(sockclient,revbuf,,0,(SOCKADDR*)&clientaddr,&len);
printf("%s\n",revbuf);
}
closesocket(sockclient);
WSACleanup();
}
大åå°å¼ï¼CSocketåªæ¯è¿è¡äºå°è£ èå·²ï¼åçæ¯ä¸æ ·çï¼ç¼ç¨è¦æè·¯çµæ´»æè¡ã
求聊天系统中客户端通信C/C++源代码!!!
#include "stdafx.h"
#include <Winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
#pragma comment (lib,"ws2_.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested;//版本号
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 1 );//1.1版本的套接字
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 0;
}//加载套接字库,加裁失败则返回
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return 0;
}//如果不是1.1的则退出
SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);//创建socket SOCK_STREAM表示TCP
if (sockClient==INVALID_SOCKET)
{
cout<<"INVALID_SOCKET"<<endl;
}
SOCKADDR_IN addrClient; //设置相关地址结构
cout<<"ip"<<endl;
char ip[]=".0.0.1";
cin>>ip;
addrClient.sin_addr.s_addr=inet_addr(ip);
addrClient.sin_family=AF_INET;
addrClient.sin_port=htons();//设置server端端口号,你可以随便设置
//连接
int x=connect(sockClient,(SOCKADDR*)&addrClient,sizeof(SOCKADDR));
if (x==SOCKET_ERROR)
{
cout<<"连接出错"<<endl;
return 0;
}
while(1)
{
char ddb_send[]="\0";
cin>>ddb_send;
if (strcmp(ddb_send,"exit")==0)
break;
char * buf=ddb_send;
int y=send(sockClient,buf,,0);
if (y==SOCKET_ERROR)
cout<<"SOCKET_ERROR"<<endl;
}
closesocket(sockClient);//关闭
WSACleanup();
return 0;
}
libevent、libev框架介绍
本文深入讲解了libevent的API,并剖析了libevent的evbuffer源码。libevent、libev和libuv都是C语言实现的异步事件库,主要负责注册异步事件、检测异步事件,并根据事件的可修改的商城源码触发先后顺序调用相应的回调函数处理事件。这些事件包括网络I/O事件、定时事件以及信号事件,共同驱动服务器运行。
libevent和libev主要封装了与操作系统交互的简单事件管理接口,让开发者无需关注平台差异,只需处理事件的具体逻辑。libev改进了libevent的架构决策,如消除全局变量的使用,采用回调函数传递上下文,构建不同的数据结构以降低事件耦合性,使用最小四叉堆作为计时器,从而实现高效管理。然而,libevent和libev在window平台的激活码程序源码支持较差,因此libuv应运而生,基于libev,尤其在window平台上更好地封装了iocp,node.js即基于libuv。
在libevent的编译安装过程中,首先从git下载release-2.1.-stable.tar.gz,然后在编译程序时指定库名:-levent。由于头文件和库文件已经复制至系统路径,因此在编译时无需额外指定-I和-L。
libevent的封装层次分为网络封装和解决的问题。网络封装包括IO检测和IO操作,解决的问题涉及连接建立(如最大连接数、黑白名单等)和连接断开,以及数据的源码的链接页面 查找到达与发送。如果不想手动操作IO事件,libevent会管理读写I/O处理,使开发者只需处理逻辑,无需关心边界问题。
libevent提供了事件检测与操作的封装。事件检测是低层封装,由libevent负责,用户自定义IO操作。该层次封装了事件管理器操作和事件接口。事件管理器event_base用于构建事件集合,检测事件就绪情况。释放管理器使用event_base_free,event_reinit用于重置,event_get_supported_methods查看支持的方法。
事件循环通过event_base_dispatch和event_base_loop实现,等待事件产生,提供类似epoll红黑树循环的功能。事件循环终止使用event_base_loopbreak和event_base_loopexit,前者在事件回调执行后终止,后者立即终止。
事件对象通过event_new创建,event_free销毁。注册与注销事件使用event_add和event_del,事件驱动的核心思想是libevent的核心功能。
libevent事件对象包括只使用事件检测、IO操作自处理的Demo。此外,自带缓冲的事件-bufferevent介绍其作为event的高级版本,拥有两个缓冲区和三个回调函数,分别用于读取、写入和事件处理。
bufferevent提供读写数据到缓冲区的封装,三个回调函数分别处理读取、写入和事件触发。构建、销毁bufferevent对象,以及连接操作、设置回调等。
事件类型注册与注销使用bufferevent_enable/disable,获取读写缓冲区使用bufferevent_get_input和bufferevent_get_output,数据分割使用evbuffer_readln和固定长度读取使用evbuffer_remove。
对于bufferevent,一个文件描述符对应两个缓冲区和三个回调函数,文件描述符用于与客户端通信,非监听文件描述符。两个缓冲区指读缓冲区和写缓冲区,三个回调分别对应读操作、写操作和事件触发。
链接监听器-evconnlistener封装底层socket通信函数,如socket、bind、listen、accept。创建监听器后,等待新客户端连接,调用用户指定的回调函数。构建监听器使用evconnlistener_new_bind,回调函数evconnlistener_cb接收与客户端通信的描述符和连接对端地址。
信号事件在libevent中与网络事件相似,通过epoll监听。定时事件和网络事件的处理机制基于最小堆与epoll_wait,通过源码分析可深入了解流程。
evbuffer作为libevent底层实现的链式缓冲区,用于bufferevent事件中的数据读写。每个evbuffer由链表组成,包含关键成员和实现细节。evbuffer的优点在于高效处理数据移动和内存浪费,缺点是数据在不连续内存中存储,可能导致多次io。libev关注具体网络IO事件、定时事件和信号事件,提供API如ev_io_init、ev_io_start、ev_timer_start和ev_run。通过libev宏定义封装,开发者能使用与libevent类似的接口。