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

  • iqoo8怎么恢复出厂设置(iqoo7怎么恢复出厂设置)

    iqoo8怎么恢复出厂设置(iqoo7怎么恢复出厂设置)

  • 携程订单删除了能恢复吗(携程订单删除了还能看到评价吗)

    携程订单删除了能恢复吗(携程订单删除了还能看到评价吗)

  • 不能对合并单元格作部分更改(不能对合并单元格进行粘贴时怎么办)

    不能对合并单元格作部分更改(不能对合并单元格进行粘贴时怎么办)

  • qq怎么开悬浮窗口聊天(qq怎么开悬浮窗vivo)

    qq怎么开悬浮窗口聊天(qq怎么开悬浮窗vivo)

  • 关机重启快捷键是什么(苹果笔记本怎么关机重启快捷键)

    关机重启快捷键是什么(苹果笔记本怎么关机重启快捷键)

  • iphonex收不到验证短信(iphonex收不到验证码短信)

    iphonex收不到验证短信(iphonex收不到验证码短信)

  • 手机root权限开启后有什么影响(手机root权限开启之后会怎样)

    手机root权限开启后有什么影响(手机root权限开启之后会怎样)

  • 为什么迅雷用wifi下载不了,用流量就可以(为什么迅雷用wifi下载没速度)

    为什么迅雷用wifi下载不了,用流量就可以(为什么迅雷用wifi下载没速度)

  • 佳能打印机恢复键在哪(佳能打印机恢复取消按钮在哪)

    佳能打印机恢复键在哪(佳能打印机恢复取消按钮在哪)

  • 新版qq怎么知道对方在通话(新版qq怎么知道手机号)

    新版qq怎么知道对方在通话(新版qq怎么知道手机号)

  • GLK_AL00华为什么型号(华为glk_al00手机)

    GLK_AL00华为什么型号(华为glk_al00手机)

  • 汇编程序是应用软件吗(汇编程序是软件吗)

    汇编程序是应用软件吗(汇编程序是软件吗)

  • 苹果更新系统后一直白苹果(苹果更新系统后怎么恢复以前的系统)

    苹果更新系统后一直白苹果(苹果更新系统后怎么恢复以前的系统)

  • 淘宝客怎么关闭掉(淘宝客怎么关闭掉 千牛)

    淘宝客怎么关闭掉(淘宝客怎么关闭掉 千牛)

  • 手机qq怎么查看旧密码(手机qq怎么查看邮箱)

    手机qq怎么查看旧密码(手机qq怎么查看邮箱)

  • 小米6插电脑无法识别(小米插电脑无法识别)

    小米6插电脑无法识别(小米插电脑无法识别)

  • 华为mate20pro5g版什么时候上市(华为mate20pro256)

    华为mate20pro5g版什么时候上市(华为mate20pro256)

  • 收藏朋友圈图片对方知道吗(朋友圈收藏照片)

    收藏朋友圈图片对方知道吗(朋友圈收藏照片)

  • 最右怎么找通讯录好友(最右怎么匹配通讯录)

    最右怎么找通讯录好友(最右怎么匹配通讯录)

  • vivo怎么看电话卡的号码(vivo怎么看电话录音)

    vivo怎么看电话卡的号码(vivo怎么看电话录音)

  • 来微信没声音咋回事啊(来微信没声音咋调)

    来微信没声音咋回事啊(来微信没声音咋调)

  • 如何取消黄钻官方团队(如何取消黄钻官方认证)

    如何取消黄钻官方团队(如何取消黄钻官方认证)

  • ps怎么给图片增加颗粒感(ps怎么给图片增加文字)

    ps怎么给图片增加颗粒感(ps怎么给图片增加文字)

  • iphone有没有呼吸灯(苹果自带的呼吸软件)

    iphone有没有呼吸灯(苹果自带的呼吸软件)

  • ios12屏幕使用时间bug(ios12屏幕使用时间密码)

    ios12屏幕使用时间bug(ios12屏幕使用时间密码)

  • Vue2 大型项目升级 Vue3 详细经验总结(vue2升级3)

    Vue2 大型项目升级 Vue3 详细经验总结(vue2升级3)

  • python打包技巧:彻底解决pyinstaller打包exe文件太大的问题(python2打包)

    python打包技巧:彻底解决pyinstaller打包exe文件太大的问题(python2打包)

  • 存货跌价准备影响当期损益吗
  • 生产车间维修费是制造费用还是管理费用
  • 新公司建账初始数据可以全部为零吗
  • 增值税附加税由买方承担如何计算
  • 个体生产经营所得税税率表
  • 承租方支付租金会计科目
  • 资产减值准备的税务处理
  • 长期待摊费用属于无形资产吗
  • 持有至到期投资是什么意思
  • 企业所得税的营业成本怎么算
  • 原材料暂估少了如何调整
  • 材料采购发生的合理损耗计入
  • 农产品加计扣除1%政策
  • 海关缴款书税号
  • 委托贷款对方单位不还
  • 税金是否可以不计提呢?
  • 进项发票和销项发票金额相同还需要交税金吗
  • 固定资产停止使用还计提折旧吗
  • 股东向公司借款需要交税吗
  • 加工费的增值税计入什么科目
  • 什么是民政福利企业
  • 哪些税金计入存货成本
  • 供应商赠送的原材料怎么做分录
  • 服装工业企业成立时间
  • 个人独资企业生活费用扣除
  • 资本公积转增实收资本个人所得税
  • 如何升级mac系统到10.12
  • 有限责任公司出资方式有哪些
  • 服务器centos6.9安装教程
  • assoc.exe=exefile什么意思
  • 企业实际缴纳税款额账务怎么看
  • win10蓝牙无法连接,有解决方法吗
  • 生产储存剧毒化学品的单位应当设置
  • ElementPlus DateTimePicker日期时间选择器限制可选时间范围(精确时分秒)
  • php strrev
  • 进出口企业税务怎可以查出问题
  • 生产型企业出口退税退的是哪部分的税
  • 上级拨付的债券会计分录
  • 操作系统()
  • Vite4 + Vue3 + vue-router4 动态路由
  • 专用发票抵扣联是什么意思
  • 购进原材料款项未付应编制什么凭证
  • 基于个人同意处理个人信息的个人什么撤回其同意
  • 出纳报销时应注意什么
  • java中public修饰什么
  • 给员工发放礼品的祝福
  • 定额发票验旧日期怎么算
  • 账面成本与账面余额
  • 红冲的发票能不能取消
  • 公司的零星开支怎么做账
  • 事业单位之间调动难吗
  • 股东投资方式有哪些
  • 有限合伙企业的特征
  • 水费能抵扣进项税吗
  • 新建企业应如何建账
  • mysql 5.7.12 winx64安装配置方法图文教程
  • FreeBSD 6.2-RELEASE下载
  • centos 安装方法
  • freebsd重启命令
  • ghost安装盘的制作
  • winxp教程
  • dvd-rom drive是什么意思
  • browsercore.exe是什么
  • pull current
  • 2021年win10新系统版本
  • github thanos
  • 怎样在手机上查询社保缴费情况
  • linux常用命令及实例
  • python怎么写爬虫
  • 小米手机图库图标
  • 3d游戏引擎诞生
  • python网络编程从入门到精通
  • 国税网上办税大厅在哪
  • 税务机关代收工会经费手续费
  • 工会经费计税依据工资总额包括什么
  • 广东省电子税务局登录方式
  • 成都成华区税务局
  • 特服号码
  • 黄山市国家税务局倪国初
  • 深圳国税咨询电话是多少
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设