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

  • sgtool.exe(sgtool)

    sgtool.exe(sgtool)

  • 抖音怎么弄全部慢动作(抖音怎么制作全屏视频)

    抖音怎么弄全部慢动作(抖音怎么制作全屏视频)

  • 手机短信验证码不弹出(手机短信验证码怎么设置直接跳出来)

    手机短信验证码不弹出(手机短信验证码怎么设置直接跳出来)

  • 优酷扫一扫二维码在哪里(优酷扫一扫二维码登录在哪里)

    优酷扫一扫二维码在哪里(优酷扫一扫二维码登录在哪里)

  • 淘宝使用月卡红包商家能收到钱吗(淘宝使用月卡红包商家会少收钱吗)

    淘宝使用月卡红包商家能收到钱吗(淘宝使用月卡红包商家会少收钱吗)

  • ipad上b站hd版区别(b站ipad改版)

    ipad上b站hd版区别(b站ipad改版)

  • 拼多多走步在哪里(拼多多走步在哪关闭)

    拼多多走步在哪里(拼多多走步在哪关闭)

  • 苹果7来电铃声不响怎么回事(苹果7来电铃声小怎么回事)

    苹果7来电铃声不响怎么回事(苹果7来电铃声小怎么回事)

  • 电脑钉钉视频会议黑屏什么原因(电脑钉钉视频会议麦克风没有声音)

    电脑钉钉视频会议黑屏什么原因(电脑钉钉视频会议麦克风没有声音)

  • 华为ale-ul00是什么型号(华为ale ul00是什么型号上市时间)

    华为ale-ul00是什么型号(华为ale ul00是什么型号上市时间)

  • 朋友圈发完了还能编辑吗(朋友圈发完了还能设置某人,不让看我朋友圈么)

    朋友圈发完了还能编辑吗(朋友圈发完了还能设置某人,不让看我朋友圈么)

  • 华为荣耀30s什么时候开售(华为荣耀60se)

    华为荣耀30s什么时候开售(华为荣耀60se)

  • 淘宝点过赞的内容在哪(淘宝点赞会被看到吗)

    淘宝点过赞的内容在哪(淘宝点赞会被看到吗)

  • vogaloo与vogal10区别(vogaloo是是什么型号)

    vogaloo与vogal10区别(vogaloo是是什么型号)

  • 电脑系统设置在哪打开(电脑安装系统后重启进不了系统)

    电脑系统设置在哪打开(电脑安装系统后重启进不了系统)

  • 苹果x耳机坏了能修吗(iphone耳机坏了怎么修)

    苹果x耳机坏了能修吗(iphone耳机坏了怎么修)

  • 拼多多付款后怎么取消订单(拼多多付款后怎么免单)

    拼多多付款后怎么取消订单(拼多多付款后怎么免单)

  • 小米手环3可以换壁纸吗(小米手环3可以连接华为手机吗)

    小米手环3可以换壁纸吗(小米手环3可以连接华为手机吗)

  • 苹果手表怎么验证国行(苹果手表怎么验证是不是正品)

    苹果手表怎么验证国行(苹果手表怎么验证是不是正品)

  • iphone xs有3d touch吗

    iphone xs有3d touch吗

  • 如何比较excel两列是否一样(如何比较两个excel)

    如何比较excel两列是否一样(如何比较两个excel)

  • 2070配什么cpu(2070配什么cpu完全发挥)

    2070配什么cpu(2070配什么cpu完全发挥)

  • wps文件怎么发送到qq(wps文件怎么发送给微信好友)

    wps文件怎么发送到qq(wps文件怎么发送给微信好友)

  • python从键盘输入若干个整数(python从键盘输入正整数n,计算1+2+3)

    python从键盘输入若干个整数(python从键盘输入正整数n,计算1+2+3)

  • 个人所得税的纳税方式有哪几种
  • 减免税款的会计分录摘要
  • 有形动产租赁属于应税劳务还是应税货物
  • 非现金资产债务怎么算
  • 企业年金个人所得税最新规定
  • 将自产产品赠送他人
  • 存款利率怎么算存一年后的金额
  • 企业住房维修基金怎么交
  • 用公户付了一笔款怎么办
  • 增值税留底税怎么计算
  • 税务局拍卖资产
  • 应征增值税销售额和应税销售额
  • 一个项目可以有几个单位工程
  • 月工资3000元,工伤7级能赔偿多少钱
  • 涉农和中小企业贷款分类证明没有就不能进行税前扣除么
  • 软件产品合同
  • 公司低价处理旧汽车涉税账务处理
  • 预收贷款是什么会计科目
  • 远期汇票的付款期限可以表示为
  • 企业之间承兑汇票贴现违法吗知乎
  • 已经认证抵扣的发票需要冲红
  • 公务车加油入什么科目
  • cmos密码和bios密码
  • 王者荣耀中孙尚香怎么玩
  • php中class用法
  • 销售商品售后回购
  • 若依移动端微信登录
  • 基于骨骼关键点的动作识别
  • 城市公交网
  • php的hashtable
  • 点云入门
  • 农民工专用账户回执单
  • 本期应纳税额减征额是什么意思
  • 一般纳税人上月有普通发票收入没入账可以次月加上吗
  • 增值税的滞纳金税率
  • 销售价格低于成本价,税务机关是否有权纳税调整?
  • python time.ctime()如何做时间加减法
  • 公司组织出国旅游可以不带钱吗
  • 支付给租户的装修押金
  • 会计准则中规范性的内容
  • 主营业务成本大于主营业务收入怎么办
  • 酒吧会计如何做工作
  • 资质办理属于什么开票大类
  • 职工食堂的会计处理
  • 递延负债和递延收益
  • 工作服入什么科目类别
  • 商业承兑汇票的风险
  • 事业单位的职工可以办营业执照吗
  • 采用成本法核算的长期股权投资
  • 收到保险赔款是什么意思
  • 所得税汇算清缴前取得跨年发票
  • 农业项目土地流转
  • 用友t3计提折旧了没有生成凭证
  • 企业筹建期间利润计算
  • 购买税控系统分录
  • 投资性房地产的范围
  • sql语句示例
  • 怎样取消开机磁盘自动检查
  • win10应用商店try that again
  • 硬盘安装windows11
  • window系统怎么取消开机密码
  • windows8使用教程
  • VirtualBox安装64位系统报错的处理方法
  • CentOS 6.x 到 CentOS 7 的升级过程
  • win7系统按Ctrl+Shift不能切换输入法的图文教程
  • linux常用命令kill
  • win10移动版应用商店打不开
  • Eclipse和AndroidStudio常用快捷键对比
  • android应用程序的主要语言是java
  • jQuery javascript获得网页的高度与宽度的实现代码
  • bat脚本编写教程菜鸟
  • css怎么控制图片位置
  • jquery示例
  • python 循环太慢
  • 专项附加扣除当月填报何时生效
  • 股权转让税收优惠个人所得税
  • 国税总局网
  • 纳税信用b级可以贷款多少
  • 四川医保卡原始密码是什么意思
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设