c++客户端向服务器发信息,使用Dev-C++实现简单的客户端和服务器

e10657361c5a3c2b2b20e9520cb231d5.png先看 效果截图

在工具菜单下选择编译器选项

添加编译命令-lwsock32 codeblock这些都是这样的(不设置将会编译失败)

762495b8316797c4d6fa009f62c62f43.png

首先是初始化 使用Win Socket 必须初始化 原因是 系统有一个Socket池 需要使用Socket则在池中拿一个

具体解释可以参考

http://blog.sina.com.cn/s/blog_4b146a9c01011ncl.html

在这一步客户端和服务器都是一样的

//初始化WSA

WORD sockVersion = MAKEWORD(2,2);

WSADATA wsaData;

if(WSAStartup(sockVersion, &wsaData)!=0)

{

return 0;

}

之后就是分别介绍

服务器

服务器初始化SOCKECT

//创建套接字

SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(slisten == INVALID_SOCKET)

{

printf("socket error !");

return 0;

}

使用socket函数进行初始化

这个函数有三个参数:

第一个参数:表示   协议域,又称为协议族

AF_INET (IPV_4)

AF_INET6  (IPV_6)

AF_LOCAL(AF_UNIX,本地通信用)

AF_ROUTE ( 路由套接字)

还有一些暂时不做介绍(太多了看不完 QAQ)

第二个参数:信息传送方式

SOCK_STREAM 有保障的(即能保证数据正确传送到对方)面向连接的SOCKET

SOCK_DGRAM 无保障的面向消息的socket

SOCK_RAW 原始套接字(更接近底层的操作)端口对于SOCK_RAW而言没有任何意义

关于SOCK_RAW具体参考https://www.cnblogs.com/aspirant/p/4084127.html、http://blog.chinaunix.net/uid-29426265-id-4217021.html

SOCK_PACKET  (Windows 下似乎是不支持的 这里不做讨论)

SOCK_SEQPACKET (与网络驱动程序直接通信)

SOCK_RDM: 提供可靠的数据包连接 (Linux 下似乎不支持 ?)

SOCK_STREAM是基于TCP的,数据传输比较有保障。

SOCK_DGRAM是基于UDP的,专门用于局域网,基于广播SOCK_STREAM 是数据流,一般是tcp/ip协议的编程,SO

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个 RTSP 服务器需要考虑以下几个方面: 1. RTSP 协议的实现; 2. RTP/RTCP 协议的实现; 3. 视频编码和解码的实现; 4. 网络传输的实现。 其中,RTSP 协议和 RTP/RTCP 协议是 RTSP 服务器的核心,视频编码和解码是 RTSP 服务器的重要功能,而网络传输则是 RTSP 服务器的基础。 下面是一个使用 C++ 和 FFmpeg 实现 RTSP 服务器的示例代码: ``` #include <iostream> #include <string> #include <chrono> #include <thread> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> extern "C" { #include <libavformat/avformat.h> #include <libavdevice/avdevice.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> #include <libavutil/time.h> } using namespace std; #define WIDTH 640 #define HEIGHT 480 #define FPS 25 int main(int argc, char* argv[]) { AVFormatContext* pFormatCtx = NULL; AVOutputFormat* fmt = NULL; AVStream* video_st = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; uint8_t* picture_buf = NULL; AVFrame* picture = NULL; int size; int framecnt = 0; AVPacket pkt; int got_picture = 0; int ret = 0; int i = 0; int err = 0; AVDictionary* options = NULL; AVIOContext* pb = NULL; AVFormatContext* ofmt_ctx = NULL; AVStream* video_out = NULL; AVCodecContext* codec_out = NULL; av_register_all(); avdevice_register_all(); // 1. 打开摄像头 AVInputFormat* ifmt = av_find_input_format("v4l2"); if (avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL) != 0) { cout << "Couldn't open input stream." << endl; return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { cout << "Couldn't find stream information." << endl; return -1; } av_dump_format(pFormatCtx, 0, "/dev/video0", 0); // 2. 初始化编码器 fmt = av_guess_format("rtsp", NULL, NULL); if (!fmt) { cout << "Couldn't guess format." << endl; return -1; } pCodec = avcodec_find_encoder(fmt->video_codec); if (!pCodec) { cout << "Couldn't find encoder." << endl; return -1; } video_st = avformat_new_stream(NULL, pCodec); if (!video_st) { cout << "Couldn't create video stream." << endl; return -1; } pCodecCtx = video_st->codec; pCodecCtx->codec_id = fmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->bit_rate = 400000; pCodecCtx->width = WIDTH; pCodecCtx->height = HEIGHT; pCodecCtx->time_base.num = 1; pCodecCtx->time_base.den = FPS; pCodecCtx->gop_size = 10; pCodecCtx->max_b_frames = 1; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; if (pCodec->id == AV_CODEC_ID_H264) { av_opt_set(pCodecCtx->priv_data, "preset", "slow", 0); } if (avcodec_open2(pCodecCtx, pCodec, &options) < 0) { cout << "Couldn't open codec." << endl; return -1; } av_dump_format(ofmt_ctx, 0, "rtsp://localhost:8554/live.sdp", 1); // 3. 初始化 AVIOContext avformat_alloc_output_context2(&ofmt_ctx, NULL, "rtsp", "rtsp://localhost:8554/live.sdp"); if (!ofmt_ctx) { cout << "Couldn't create output context." << endl; return -1; } pb = avio_alloc_context(NULL, 0, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL); if (!pb) { cout << "Couldn't create AVIOContext." << endl; return -1; } ofmt_ctx->pb = pb; // 4. 添加输出流 video_out = avformat_new_stream(ofmt_ctx, NULL); if (!video_out) { cout << "Couldn't create output stream." << endl; return -1; } codec_out = video_out->codec; codec_out->codec_id = pCodec->id; codec_out->codec_type = AVMEDIA_TYPE_VIDEO; codec_out->bit_rate = 400000; codec_out->width = WIDTH; codec_out->height = HEIGHT; codec_out->time_base.den = FPS; codec_out->time_base.num = 1; codec_out->pix_fmt = AV_PIX_FMT_YUV420P; if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { codec_out->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } // 5. 打开输出流 err = avio_open(&ofmt_ctx->pb, ofmt_ctx->url, AVIO_FLAG_WRITE); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Could not open output URL '%s'\n", ofmt_ctx->url); return -1; } err = avformat_write_header(ofmt_ctx, &options); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output URL\n"); return -1; } // 6. 开始编码和送数据 picture = av_frame_alloc(); size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); picture_buf = (uint8_t*)av_malloc(size); avpicture_fill((AVPicture*)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); av_new_packet(&pkt, size); while (true) { if (av_read_frame(pFormatCtx, &pkt) >= 0) { if (pkt.stream_index == 0) { // 解码 avcodec_decode_video2(pCodecCtx, picture, &got_picture, &pkt); if (got_picture) { // 编码 picture->pts = av_rescale_q(framecnt, pCodecCtx->time_base, video_st->time_base); ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture); if (ret >= 0 && got_picture) { pkt.stream_index = video_st->index; av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base); pkt.pos = -1; err = av_interleaved_write_frame(ofmt_ctx, &pkt); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error muxing packet\n"); break; } cout << "Write frame " << framecnt << endl; framecnt++; } av_packet_unref(&pkt); } } } else { av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_FRAME); } this_thread::sleep_for(chrono::milliseconds(40)); } av_write_trailer(ofmt_ctx); avcodec_close(pCodecCtx); av_free(pCodecCtx); av_free(picture_buf); av_free(picture); avio_close(ofmt_ctx->pb); avformat_free_context(ofmt_ctx); avformat_close_input(&pFormatCtx); return 0; } ``` 在上面的代码中,我们使用 FFmpeg 库打开摄像头,并初始化了一个 RTSP 服务器,将摄像头采集到的视频编码后发送给客户端。 在代码中,我们首先打开摄像头,然后初始化编码器。接下来,我们初始化 AVIOContext,添加输出流并打开输出流。最后,我们开始编码和送数据。 编译代码时需要链接 FFmpeg 库: ``` g++ -o rtsp_server rtsp_server.cpp -lavdevice -lavformat -lavcodec -lavutil -lswscale -lpthread ``` 启动代码后,可以使用 VLC 等 RTSP 客户端访问 RTSP 服务器: ``` vlc rtsp://localhost:8554/live.sdp ``` 这样就可以在客户端上观看摄像头采集到的视频了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值