网络编程之Elementary TCP Sockets(一)

AI驱动Spine骨骼动画生成:从图像拆分到动画自动化的全流程解析 计算机视觉与生成式AI技术正深度赋能传统数字内容生产流程。在2D动画制作领域,骨骼动画技术通过模拟真实生物关节运动,实现了角色动画的高效制作与灵活控制。其核心原理在于将角色拆分为独立部件,通过骨骼层级关系驱动部件变换,结合关键帧插值算法生成流畅动作。这项技术的工程价值在于大幅降低动画制作门槛,提升内容生产效率,尤其适用于游戏开发、影视预演、广告设计等需要快速原型迭代的场景。本文聚焦于如何利用SIFT特征匹配、RANSAC几何验证等CV算法实现部件自动对位,并结合动画12原则的程序化实现,构建从单张角色立绘到 阅读详情

1. socket

#include <sys/socket.h>
int socket(int family, int type, int protocol);
Returns: non-negative descriptor if OK, −1 on error

加个表

上表就是对该函数的一个总结,在APUE篇笔者也记录过该内容,在此不赘述,上表没有反映出来的几点需要注意下:

  • AF_UNIX (the historical Unix name)
  • TCP is a byte stream protocol, and supports only SOCK_STREAM sockets.
  • Linux supports a new socket type, SOCK_PACKET, that provides access to the datalink.
  • The key socket, AF_KEY, is newer than the others. It provides support for cryptographic security. Similar to the way that a routing socket (AF_ROUTE) is an interface to the kernel’s routing table, the key socket is an interface into the kernel’s key table.

2. connect Function

#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
Returns: 0 if OK, −1 on error

一些基本的使用注意点:

  • The socket address structure must contain the IP address and port number of
    the server.
  • The client does not have to call bind before calling connect: the kernel will choose both an ephemeral port and the source IP address if necessary.
  • In the case of a TCP socket, the connect function initiates TCP’s three-way handshake . The function returns only when the connection is established or anerror occurs.

第三点指出connect会发起TCP的三次握手,而该函数只有在连接建立或者错误发生时返回,所以UNP列出了几种错误的情况:

  • If the client TCP receives no response to its SYN segment, ETIMEDOUT is returned.
  • If the server’s response to the client’s SYN is a reset (RST), this indicates that no process is waiting for connections on the server host at the port specified (i.e., the server process is probably not running). This is a hard error and the error ECONNREFUSED is returned to the client as soon as the RST is received. An RST is a type of TCP segment that is sent by TCP when something is wrong. Three conditions that generate an RST are:
    • when a SYN arrives for a port that has no listening server (what we just described),
    • when TCP wants to abort an existing connection//server发送给client
    • when TCP receives a segment for a connection that does not exist.
  • If the client’s SYN elicits an ICMP ‘‘destination unreachable’’ from some intermediate router, this is considered a soft error. The client kernel saves the message but keeps sending SYNs with the same time between each SYN as in the first scenario. If no response is received after some fixed amount of time, the saved ICMP error is returned to the process as either EHOSTUNREACH or ENETUNREACH. It is also possible that the remote system is not reachable by any route in the local system’s forwarding table, or that the connect call returns without waiting at all.

特别的上述第三个原因需要以后再次回顾,这里不过分追究,注意elicits在英文中的的用法为elicit sth from sb,UNP中举了几个connet错误的列子,并相应做出解释:

solaris % daytimetcpcli 192.168.1.100
connect error: Connection timed out

we specify an IP address that is on the local subnet (192.168.1/24) but the host ID (100) is nonexistent. That is, there is no host on the subnet with a host ID of 100, so when the client host sends out ARP requests (asking for that host to respond with its hardware address), it will never receive an ARP reply.//问题1

solaris % daytimetcpcli 192.168.1.5
connect error: Connection refused

This example is to specify a host (a local router) that is not running a daytime server. The server responds immediately with an RST.

solaris % daytimetcpcli 192.3.4.5
connect error: No route to host

This example specifies an IP address that is not reachable on the Interne. If we watch the packets with tcpdump, we see that a router six hops away returns an ICMP host unreachable error. As with the ETIMEDOUT error, in this example, connect returns the EHOSTUNREACH error only after waiting its specified amount of time.

tcpdump是抓包工具,从该工具也能判断网络的故障原因,抓包分析是针对网络问题比较基本的手段。

3. bind

#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
Returns: 0 if OK, −1 on error

对于TCP来说,可以由编程人员指定地址和端口号,也可以让系统自己指定:

  • Servers bind their well-known port when they start. If a TCP client or server does not do this, the kernel chooses an ephemeral port for the socket when either connect or listen is called.
  • TCP client does not bind an IP address to its socket. The kernel chooses the source IP address when the socket is connected, based on the outgoing interface that is used, which in turn is based on the route required to reach the server.
  • If a TCP server does not bind an IP address to its socket, the kernel uses the destination IP address of the client’s SYN as the server’s source IP address。//问题2

加个图:

如果在bind中使用通配地址或者端口号指定为0时,发生以下行为:

If we specify a port number of 0, the kernel chooses an ephemeral port when bind is called. But if we specify a wildcard IP address, the kernel does not choose the local IP address until either the socket is connected (TCP) or a datagram is sent on the socket (UDP).//只是说明了该自动选择一个地址的发生的时间

//IPv4
struct sockaddr_in servaddr;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard */
//IPv6
struct sockaddr_in6 serv;
serv.sin6_addr = in6addr_any; /* wildcard */

上述给了两个如何指定通配地址的例子,特别注意到htonl,INADDR_ANY是一个值为0的常量,本质上其并不需要转换字节顺序,但是这里还是调用了htonl,原因如下:

all the INADDR_ constants defined by the netinet/in.h header are defined in host byte order, we should use htonl with any of these constants.

4. listen Function

TCP server调用listen后,会进行以下事情:

  • When a socket is created by the socket function, it is assumed to be an active
    socket, that is, a client socket that will issue a connect. The listen function
    converts an unconnected socket into a passive socket, indicating that the kernel
    should accept incoming connection requests directed to this socket.
  • The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Returns: 0 if OK, −1 on error

UNP特别强调了backlog参数的理解,因为内核维护了两个队列:

  • An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three-way handshake. These sockets are in the SYN_RCVD state.//这里佐证TCP状态的变化时以socekt为单位的。
  • A completed connection queue, which contains an entry for each client with whom the TCP three-way handshake has completed. These sockets are in the ESTABLISHED state.

加个两个图
这里写图片描述

具体描述上述过程的细节如下:

  • When a SYN arrives from a client, TCP creates a new entry on the incomplete queue and then responds with the second segment of the three-way handshake: the server’s SYN with an ACK of the client’s SYN. This entry will remain on the incomplete queue until the third segment of the three-way handshake arrives (the client’s ACK of the server’s SYN), or until the entry times out.
  • If the three-way handshake completes normally, the entry moves from the incomplete queue to the end of the completed queue. When the process calls accept, he first entry on the completed queue is returned to the process, or if the queue is empty, the process is put to sleep until an entry is placed onto the completed queue.

关于这两个队列有以下情况值得思考:

  • The backlog argument to the listen function has historically specified the maximum value for the sum of both queues.
  • Do not specify a backlog of 0, as different implementations interpret this differently. If you do not want any clients connecting to your listening socket, close the listening socket.
  • If the queues are full when a client SYN arrives, TCP ignores the arriving SYN ; it does not send an RST. This is because the condition is considered temporary, and the client TCP will retransmit its SYN, hopefully finding room on the queue in the near future. If the server TCP immediately responded with an RST, the client’s connect would return an error, forcing the application to handle this condition instead of letting TCP’s normal retransmission take over. Also, the client could not differentiate between an RST in response to a SYN meaning there is no server at this port versus there is a server at this port but its queues are full.
  • Data that arrives after the three-way handshake completes, but before the server calls accept, should be queued by the server TCP, up to the size of the connected socket’s receive buffer.

5. accept

#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
Returns: non-negative descriptor if OK, −1 on error

API用法如下:

  • The cliaddr and addrlen arguments are used to return the protocol address of the connected peer process (the client). Before the call, we set the integer value referenced by *addrlen to the size of the socket address structure pointed to by cliaddr; on return, this integer value contains the actual number of bytes stored by the kernel in the socket address structure.
  • This function returns up to three values: an integer return code that is either a new socket descriptor or an error indication, the protocol address of the client process (through the cliaddr pointer), and the size of this address (through the addrlen pointer). If we are not interested in having the protocol address of the client returned, we set both cliaddr and addrlen to null pointers.

一些accept函数的需要注意的小知识点:

  • If accept is successful, its return value is a brand-new descriptor automatically created by the kernel. This new descriptor refers to the TCP connection with the client.//问题3
  • A given server normally creates only one listening socket, which then exists for the lifetime of the server.
  • The kernel creates one connected socket for each client connection that is accepted (i.e., for which the TCP three-way handshake completes).

6. close Function

#include <unistd.h>
int close(int sockfd);
Returns: 0 if OK, −1 on error

The default action of close with a TCP socket is to mark the socket as closed and return to the process immediately. The socket descriptor is no longer usable by the process: It cannot be used as an argument to read or write. But, TCP will try to send any data that is already queued to be sent to the other end, and after this occurs, the normal TCP connection termination sequence takes place.

上述一为close的一般概念,但是sockfd同一般文件一样,都有引用计数,所以真正内核关闭该文件的时间,应该是引用计数为0时。要与link count相区分,两个是不一样的概念。打开的文件描述符的引用计数位于内核维护的文件列表条目中。

7. TCP state transmitation

虽然看上去最基本的API介绍完了,但是其与TCP state的对应却没有仔细讨论过,先看一个图:
  这里写图片描述
  之前的理解一直以为是针对client TCP和Server TCP两个协议栈来说的,现在觉得如果想理解listen的两个队列,就必需理解状态变化是对socket说的。从client调用connect时,其socket变为SYN_SENT,而server端调用listen,此时未完成三次握手的socket,状态为SYN_RCVD,当三次握手完成时,该socket状态变为ESTABLISHED,当server调用accept时,返回该socketfd。

这里笔者假设新的socket在listen函数调用后,accept调用前就已经生成了。

8. 遗留问题

  1. the client host sends out ARP requests (asking for that host to respond with its hardware address), it will never receive an ARP reply.
  2. Server没有bind一个地址,此时client又怎么知道connect谁呢?
  3. 既然listen中的描述以每个socekt描述TCP的状态,那么在调用accept时返回的新socektfd到底是在什么时候形成的呢?
虚拟机ubuntu22.04.3中安装vitis2024.1注意事项 虚拟机ubuntu安装vitis2024.1 阅读详情

相关推荐

5G协议栈逆向工程:基于开源模拟器的核心网信令分析实战

本文详细介绍了基于开源模拟器Free5GC和UERANSIM的5G协议栈逆向工程实战,涵盖核心网信令分析、环境搭建、认证协议解析及安全漏洞验证。通过NGAP/NAS信令交互解密和异常流量注入测试,帮助研究人员深入理解5G网络安全机制,并提供实用的安全加固建议。

weixin_29250573的博客 679

FreeBSD 配置路由

FreeBSD 配置路由<br />在同一个局域网上的计算机是直接连通的,但是不同的网络上的计算机并没有直接相连,只能通过一台特殊的专用计算 机 -路由器来完成连通。路由器连接有多个网络界面,每一个网络界面连接到一个相应的网络上,具有一个这个网络上的IP地址,因此,网络内部的计算机可以和路 由器本身相互通信。 <br />当网络内部的计算机要向外连接的时候,数据包实际是交给路由器的,路由器再将数据包从连接外部网络的网络界面上转发出去,数据包经过这 样一个一个的转发过程,直到到达目的地。对于网络

999

视觉辅助RIS波束追踪:用摄像头“看见”用户,实现毫秒级波束对准

在无线通信领域,波束成形技术是提升高频段信号覆盖与质量的核心手段,其原理是通过调整天线阵列的相位,使信号能量在特定方向相干叠加,形成高增益定向波束。这项技术的价值在于能有效克服高频信号路径损耗大、穿透性差的瓶颈,广泛应用于5G/6G、毫米波通信及室内定位等场景。然而,传统波束追踪依赖信道估计或波束扫描,存在训练开销大、实时性差的问题。本文聚焦于一种创新解决方案:将计算机视觉与可重构智能表面(RIS)相结合。通过摄像头直接感知用户位置,并利用RIS快速重构反射波束方向,实现了近乎零开销、毫秒级延迟的波束追踪,

weixin_33552698的博客 621

分享mac主机通过虚拟机中parallels连接内网

准备条件 mac和虚拟主机能正常访问外网,虚拟机开启vpn客户端后,虚拟机网页能够访问内网。 记录虚拟机vpn网卡的ip信息 ip 为 192.168.ipThd.xx . 这个ipThd决定后面的ipThd,这个ip指的为新增vpn网卡的ip. Parallels Desktop新增host only2网络 mac修改hostOnly2网络 mac 用hostOnly2连接虚拟机 这步很关键,如果没有only#1 和 only#2 则代表设置有问题 ip 设置为 192.168.ipThd.2路

德川家康 6736

从零搭建A股量化交易系统:开源框架选型与实战部署指南

本文详细介绍了从零搭建A股量化交易系统的完整流程,重点分析了开源框架VNPY和QMT的选型与实战部署。内容涵盖系统架构设计、策略开发、性能优化及实盘过渡方案,为开发者提供了一套完整的A股实盘量化交易解决方案,助力高效构建稳定可靠的量化交易系统。

sugar的博客 1281

【电磁场与电磁波课程设计】基于HFSS的双频微带天线仿真及设计

第五代移动通信技术(5G)的商用已经近在眼前,作为移动通信中最为重要的功能组件,面向5G通信应用的手机天线与基站天线的技术研究与设计成为近年来的热点。随着移动通信中“跳频”“扩频”等通信技术发展,为了满足与多个终端的通信要求,实现多系统共用和收发共用等功能,这就要求天线在不同频段下工作。根据国家工信部的最新规定,5G移动通信的中频段被划定在3.3 GHz-3.5 GHz及4.8 GHz-5.0 GH,面向5G移动通信的天线应具有高增益、小型化、宽频段及高隔离度等技术特征,以满足5G通信的高传输速率、波束智能

weixin_44450575的博客 1万+

vmware FreeBSD安装

FreeBSD10.1安装过程 时间: 2015-02-28 21:37 作者: lsgxeva 分类: 工作学习>>FreeBSD>>server 摘要: 用vmware安装FreeBSD10.1系统 标签: Freebsd 系统 vmware 提示: 文章均来自网络,版权为原作者所有,如有侵犯权益,请联络我们. 一:安装FreeBSD1

james zhang的博客 9184

CentOS 7下Oracle 11G RAC静默安装避坑指南:从网络规划到共享存储配置全流程

本文详细介绍了在CentOS 7环境下进行Oracle 11G RAC静默安装的全流程,包括网络规划、共享存储配置和系统参数调优等关键步骤。特别针对静默安装过程中的常见问题提供了实用的避坑指南和优化建议,帮助技术人员高效完成企业级数据库部署。

weixin_29234509的博客 132

linux socket 添加路由,Linux下routing socket使用介绍

Linux下routing socket使用介绍发布时间:2006-09-23 00:59:37来源:红联作者:wuhu911作者:王贵豹(daiban2@tom.com)您可以自由转载,但如果在商业报刊上刊登,请先征得作者同意。《UNIX Network Programming Volume 1 - 3rd Edition》第18章讲到BSD UNIX系统中routing socket的应用,这...

weixin_34741145的博客 632

27、单阶段目标检测算法(SSD):原理、训练与实战

本文详细介绍了单阶段目标检测算法SSD的原理、训练方法与实战应用。内容涵盖SSD的核心机制,如特征图离散化、边界框预测、损失函数设计及难负样本挖掘,并通过使用dlib车辆数据集从零开始训练SSD模型的完整流程展示了实际操作步骤。文章还讨论了训练中的关键问题,如边界框坐标处理和‘忽略区域’缺失的影响,提供了模型评估与优化策略,包括超参数调整、基础网络替换和数据增强。最后总结了SSD的优势与局限性,并展望了未来目标检测技术的发展方向。

nice1的博客 509

LPDDR6来了!到底比LPDDR5/5X强在哪?从速度到总线,技术细节一次说清!

LPDDR6不仅仅是速度快点、位宽大点那么简单,它是一次架构级的革新,从引脚、总线、时钟管理到Burst协议全部升级。🎯我们正在迈入一个“AI优先”的时代,内存架构也必须跟上。对于做芯片、做整机、做系统的你,理解LPDDR6的技术变化,提前规划测试方案,是现在就该启动的动作!

weixin_43199439的博客 1287

SIOCADDRT: File exists

原文地址::http://blog.youkuaiyun.com/carolzhang8406/article/details/6201504 相关文章 1、route: SIOCADDRT: File exists 问题----http://blog.chinaunix.net/uid-8048969-id-3369102.html  2、RF 6 SP2 下无线网络设置----http://

xqhrs232的专栏 1万+

为什么我的OpenClaw安装后无法启动?Gateway服务故障排查全攻略

OpenClaw是一款功能强大的自动化工具,但其安装和运行依赖于多个服务组件,其中Gateway服务是核心组件之一。如果Gateway服务无法启动,整个OpenClaw系统将无法正常运行。本文将详细介绍OpenClaw安装后无法启动的常见原因及故障排查方法,帮助你快速定位并解决问题。

一键难忘的博客 4635

网络编程之Elementary TCP Sockets(二)

1. fork and exec Functions   笔者在APUE中比较详细的记录过fork函数,这里相关细节不在赘述,只看看和网络编程相关的概念: The parent calls accept and then calls fork. The connected socket is then shared between the parent and child. Nor...

LoveStackover的博客 386

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

UNP读书笔记--Chapter 4 Elementary TCP Sockets

n/a

Chris.Xiao 741

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

高级C与网络编程复习(4)—— 基本套接字函数(Elementary Sockets Functions)(第四章)

socket 函数 #include <sys/socket.h> /** * 该函数用于创建一个socket套接字 * @param domin 协议族/地址族 * @param type 套接字的类型 * SOCK_STREAM ==> TCP套接字 * SOCK_DGRAM ==>...

MingJ的博客 429

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Elementary Sockets:readn, writen, and readline Functions

Elementary Sockets:readn, writen, and readline Functions_Aurola_百度空间Elementary Sockets:readn, writen, and readline FunctionsStream sockets (e.g., TCP sockets) exhibit a behavior with the read and wri...

weixin_33725239的博客 95

linux route路由

网关(Gateway)又称网间连接器、协议转换器。网关在网络层以上实现网络互连就好像一个房间可以有多扇门一样,一台主机可以有多个网关。默认网关的意思是一台主机如果找不到可用的网关,就把数据包发给默认指定的网关,由这个网关来处理数据包。默认网关。默认网关一般填写192.168.x.1需要特别注意的是:默认网关必须是电脑自己所在的网段中的IP地址,而不能填写其他网段中的IP地址1. route命令 -

超哥的专栏 1074

Python和sendfile

sendfile(2) is a UNIX system call which provides a “zero-copy” way of copying data from one file descriptor (a file) to another (a socket). Because this copying is done entirely within the kernel, sen...

cumei1658的博客 1794

网络路由技术及运用(3)

本文主要涉及到静态路由。 三、UNIX系统上设置路由 由于AIX在业界的广泛使用,本文以AIX操作系统为例对如何在UNIX上设置路由进行介绍。 对于静态路由而言,路由表可用SMIT命令或ROUTE命令进行手工维护。需要指出的是SMIT命令可更新ODM数据库相应的路由信息,因此可永久生效,而ROUTE命令并不修改ODM数据库,因此在机器重新启动时将丢失路由。下面首先介绍ROUTE命令的使...

weixin_34062469的博客 587

套接字编程

+++++++套接字编程+++++++ (位于6-4的中间) 【socket( ); 创建套接字,返回一个套接字描述符】 int socket(int family, int type, int protocol); family:协议族 AF_INET: IPv4协议 AF_INET6: IPv6协议 AF_LOCAL: UNIX域协议 F

罗函 1039

Linux kernel ‘net/key/af_key.c’信息泄露漏洞

漏洞名称: Linux kernel ‘net/key/af_key.c’信息泄露漏洞 CNNVD编号: CNNVD-201307-071 发布时间: 2013-07-05 更新时间: 2013-07-05 危害等级: 漏洞类型: 信息泄露 威胁类型: 本地 CVE编号: ...

weixin_30917213的博客 209

一篇搞懂TCP、HTTP、Socket、Socket连接池!

如果想要使传输的数据有意义,则必须使用到应用层协议比如Http、Mqtt、Dubbo等。基于TCP协议上自定义自己的应用层的协议需要解决的几个问题:心跳包格式的定义及处理报文头的定义,就是你发送数据的时候需要先发送报文头,报文里面能解析出你将要发送的数据长度你发送数据包的格式,是json的还是其他序列化的方式下面我们就一起来定义自己的协议,并编写服务的和客户端进行调用:定义报文头格式:length:000000000xxxx;xxxx代表数据的长度,总长度20,举例子不严谨。

m0_71777195的博客 509

C语言——socket通信详解

目录关键函数服务端客户端效果展示客户端服务端 关键函数 1、int socket(int family,int type,int protocol) family: 指定使用的协议簇:AF_INET(IPv4) AF_INET6(IPv6) AF_LOCAL(UNIX协议) AF_ROUTE(路由套接字) AF_KEY(秘钥套接字) type: 指定使用的套接字的类型:SOCK_STREAM(字节流套接字) SOCK_DGRAM protocol: 如果套接字类型不是原始套接字,

WU2629409421perfect的博客 3961

网络编程-tcp socket编程

真正的房子只有几个门,但是一个ip地址的端口可以有65536(256×256)个之多,端口是通过端口号来标记的,端口号只有整数,范围是从0到65535(256×256-1)TCP/IP协议,称为传输控制协议/因特网互联协议,又叫网络通讯协议,这个协议是internet最基本的协议、internet国际互联网络的基础,就是由网络传的IP协议和传输层的TCP协议组成的。2.1-1024是固定端口,又叫有名端口,即被某些程序固定使用,一般程序员不使用。2.接收客户端的tcp连接,建立客户端和服务器端的链接。

m0_56719679的博客 255

【网络篇】socket编程——TCP(史上最全)

TCP协议详解。

Linux驱动开发工程师,熟悉Linux/Android底层驱动、TP、显示、IIC、网络等通用外设调试适配,Linux上层应用开发。常用平台瑞芯微、全志、恩智浦等。 1万+
上一篇: 《DP学习系列》从零开始学习动态规划,有依赖的背包问题(七)
下一篇: 网络编程之Elementary TCP Sockets(二)
LoveStackover
博客等级 码龄9年 14粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值