位置: 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官网)

  • 印花税处罚适用征管法吗
  • 未按期纳税申报怎么处理
  • 财税通财务软件怎么下载
  • 公司现金账户归谁管
  • 债券的利益
  • 申报无票收入次月红冲有没有风险
  • 微信小程序需要授权怎么设置
  • 公司的班车费用用什么发票
  • 林业一般纳税人的税率是多少?
  • 预缴税款什么时候可以手动填写申报表
  • 经营性应收项目和经营性应付项目
  • 已入账未抵扣退回发票
  • 增值税当月抵扣吗
  • 自然人增值税免税额
  • 冬虫夏草开票什么税率
  • 收取职工房租 算收入吗
  • 价内税和价外税区别
  • 发票申领需要什么资料
  • 存货扣税比是什么意思?
  • 没有认证的进项发票怎么入账
  • 金蝶哪个版本最好
  • 公司注销了帐户钱能办款
  • 收到农产品发票3%抵9%怎么做账
  • 财务费用属于什么会计科目类别
  • 文艺汇演活动经历怎么写
  • 如何解决无线网络连接问题
  • harmonyos怎么打开OTG
  • vue3如何
  • 私户给公户转账多久到账
  • 建筑业增值税会计核算是哪门课的内容
  • 华为路由器怎么设置wifi密码
  • wordpress登录注册
  • html简单的游戏
  • elementui级联选择器清空方法
  • 前端框架学哪个好
  • phpcms 还有人用吗
  • 社保和公积金个人缴纳在哪里查看
  • elementui动态表单数据回显
  • golang char
  • php websocket教程
  • 命令行延时
  • 收到利息收入计入什么科目
  • 上个月成本做多了怎么冲账
  • 其他综合收益在资产负债表中如何填列
  • 条形码费用属于哪个科目
  • 商业保险 抵扣个税
  • 营业执照需要提交什么材料
  • win2008 enterprise R2 x64 中安装SQL server 2008的方法
  • 小规模纳税人税额怎么算
  • 服务行业人工费一般控制在多少合适
  • 发票可否盖财务章
  • 城建税教育费附加什么时候交
  • 控股合并和吸收合并会计处理的区别
  • 12月结账后要年度结转吗
  • 现金收账凭证
  • 外汇汇率调整分为哪几种
  • 个人生产经营所得税怎么申报
  • 公司研发项目立项
  • 会计调账是什么意思
  • 备查账一般采用什么账簿
  • windows2003服务
  • centos清理磁盘空间
  • freebsd忘记root密码
  • linux搭建l2tp服务器
  • eac.exe是什么
  • linux中ls命令的功能
  • ubuntu查cpu信息
  • Cocos2d-x3.3 Physics物理引擎模块解决了刚体穿透问题
  • javascript中匿名函数
  • css一般可以放在几个地方
  • javascript中interval与setTimeOut的区别示例介绍
  • 查看node
  • javascript详细介绍
  • javascript函数如何定义及其作用
  • 代收国税地税收入
  • 浙江国税局电话客服热线
  • 上海买房各种税
  • 购买房屋交契税
  • 长春市医保社保什么时候年检
  • 深圳国税局网站如何查询企业往年财务报表
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设