位置: IT常识 - 正文

Linux Howtos: C/C++ > Sockets Tutorial

编辑:rootadmin
Linux Howtos: C/C++ -> Sockets TutorialSockets TutorialThis is a simple tutorial on using sockets Sockets Tutorial

推荐整理分享Linux Howtos: C/C++ > Sockets Tutorial,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

This is a simple tutorial on using sockets for interprocess communication.

The client server model

Most interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.

Notice that the client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established.

Notice also that once a connection is established, both sides can send and receive information.

The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket.A socket is one end of an interprocess communication channel. The two processes each establish their own socket.

The steps involved in establishing a socket on the client side are as follows:

Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:

Create a socket with the socket() system call Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine. Listen for connections with the listen() system call Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. Send and receive dataSocket Types

When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain.

Linux Howtos: C/C++ > Sockets Tutorial

There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format.

The address of a socket in the Unix domain is a character string which is basically an entry in the file system.

The address of a socket in the Internet domain consists of the Internet address of the host machine (every computer on the Internet has a unique 32 bit address, often referred to as its IP address).In addition, each socket needs a port number on that host.Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available.

There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communciations protocol.

Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.

The examples in this tutorial will use sockets in the Internet domain using the TCP protocol.

Sample code

C code for a very simple client and server are provided for you. These communicate using stream sockets in the Internet domain. The code is described in detail below. However, before you read the descriptions and look at the code, you should compile and run the two programs to see what they do.

server.cclient.c

Download these into files called server.c and client.c and compile them separately into two executables called server and client.

They probably won't require any special compiling flags, but on some solaris systems you may need to link to the socket library by appending -lsocket to your compile command.

Ideally, you should run the client and the server on separate hosts on the Internet. Start the server first. Suppose the server is running on a machine called cheerios. When you run the server, you need to pass the port number in as an argument. You can choose any number between 2000 and 65535. If this port is already in use on that machine, the server will tell you this and exit. If this happens, just choose another port and try again. If the port is available, the server will block until it receives a connection from the client. Don't be alarmed if the server doesn't do anything;

It's not supposed to do anything until a connection is made.

Here is a typical command line:

server 51717
本文链接地址:https://www.jiuchutong.com/zhishi/304107.html 转载请保留说明!

上一篇:帝国cms怎么添加vip(帝国cms添加栏目前台不显示)

下一篇:phpcms v9怎么样(phpcms v9官网)

  • 城市维护建设税的计税依据是什么
  • 销售利税率计算公式及分析
  • 两处工资薪金所得
  • 酒店收入是以当月入账时间为准吗
  • 购入原材料结转采购成本的会计分录
  • 个独企业缴纳个人经营所得税额的会计分录
  • 收到增值税发票是进项还是销项
  • 小规模纳税人增值税申报表怎么填
  • 待抵扣税金凭证分录
  • 非工作日是否可以开标
  • 生产部门领用低值易耗品,实际成本
  • 全年一次性奖金税收计算器
  • 一次性发放年终奖怎么税务筹划
  • 固定资产清理是备抵账户吗
  • 建筑图纸设计费包括晒图费吗
  • 企业开具的增值税专用发票属于
  • 企业出租房屋交什么税?
  • 电子发票怎么红冲步骤视频
  • 个税手续费返还计入哪个科目
  • 万元的票可以开到多大金额
  • 蔬菜免税会计分录怎么做
  • 所得税的税率有哪几种
  • 进料加工料件需补税的情况
  • 企业亏损减资的会计处理
  • 收到社保补助不发放
  • 企业委托研发过程中发生的研发费用如何加计扣除?
  • 工会经费的开支必须取得发票么
  • 王者荣耀进不去怎么回事最新
  • 企业合并案例
  • 幼儿园资金收支管理财务制度
  • 物业公司成本结转方案
  • php数组函数输出《咏雪》里有多少"片"字
  • PHP:oci_set_module_name()的用法_Oracle函数
  • php-cp
  • fodhelper.exe是什么程序
  • 支付押金无法付款
  • 项目完工叫什么
  • 住宅用途的房屋可以办理医疗器械证吗
  • axios提交文件
  • 设计资质承担范围
  • 生产车间工资计提
  • 印花税应税凭证数量是什么意思
  • 主营业务成本入账分录
  • 应付职工薪酬怎么冲平
  • 个体工商户经济类型是内资吗
  • 金税盘中的发票修复是什么意思
  • 退预收款需要对方同意吗
  • 只有进项发票,没有销项可以吗
  • 小规模纳税人货款怎么算
  • 不借助的英文
  • 每个公司都要交五险一金吗
  • 金税四期一般纳税人开票先票后款行吗
  • 增值税扣缴义务发生时间为扣缴义务人扣缴税款的当天
  • 银行承兑汇票是什么意思
  • 城建税的征收范围不包括农村
  • 辅助生产车间的低值易耗品计入
  • 应付账款借方余额负数表示什么
  • 电子缴税付款凭证怎么做记账凭证
  • 职工福利费计提标准
  • 公司账户的钱怎么合理的转出来
  • 税金附加科目有哪些
  • 个体户未进行抄报税怎么处罚
  • 合伙 利润分配
  • 实收资本应补缴哪些税
  • 安装sqlserver2000 IP地址出错怎样修改
  • sql返回数据条数
  • .NET Framework SQL Server 数据提供程序连接池
  • mysql数据库设置固定值
  • 苹果电脑mac系统怎么用
  • 电脑系统停用
  • 电脑已禁用ime是什么
  • unity3d功能介绍
  • java模拟支付功能
  • 你知道必要的意思吗英文
  • 云南定额发票真伪查询平台
  • 交错了税可以退吗
  • 建筑行业增值税税收优惠政策
  • 重庆税务登录
  • 车辆购置税多久能退回来
  • 近亲房屋赠与免税费
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设