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

  • qq怎么删自动回复的自定义信息(qq怎么删自动回复话)

    qq怎么删自动回复的自定义信息(qq怎么删自动回复话)

  • 萤石云可以装在电视吗(萤石云可以下载监控吗)

    萤石云可以装在电视吗(萤石云可以下载监控吗)

  • 打开淘宝音乐就停了怎么办(打开淘宝音乐就停止播放)

    打开淘宝音乐就停了怎么办(打开淘宝音乐就停止播放)

  • 秘乐能不能注销实名认证(秘乐能不能注销抖音账号)

    秘乐能不能注销实名认证(秘乐能不能注销抖音账号)

  • usb setup什么意思(usb,什么意思)

    usb setup什么意思(usb,什么意思)

  • 抖音短视频删除对账号有影响吗(抖音短视频删除了怎么恢复作品)

    抖音短视频删除对账号有影响吗(抖音短视频删除了怎么恢复作品)

  • 图计算解决什么问题(图计算的应用)

    图计算解决什么问题(图计算的应用)

  • 苹果xr无线网老是掉线(苹果xr为什么wifi老是断开)

    苹果xr无线网老是掉线(苹果xr为什么wifi老是断开)

  • 照片带日期怎么设置(照片带日期怎么去掉水印)

    照片带日期怎么设置(照片带日期怎么去掉水印)

  • oppor17自带游戏变声器吗(oppor17自带游戏空间怎么恢复)

    oppor17自带游戏变声器吗(oppor17自带游戏空间怎么恢复)

  • 荣耀v20显示hd是什么意思(荣耀20出现hd)

    荣耀v20显示hd是什么意思(荣耀20出现hd)

  • ipada1538什么型号(苹果ipada1538什么型号)

    ipada1538什么型号(苹果ipada1538什么型号)

  • word文档页面设置16开(Word文档页面设置上下左右怎么设置)

    word文档页面设置16开(Word文档页面设置上下左右怎么设置)

  • 抖音个人认证有什么用(抖音个人认证有风险吗)

    抖音个人认证有什么用(抖音个人认证有风险吗)

  • 淘宝客服聊天发不出去(淘宝客服聊天发不过去)

    淘宝客服聊天发不出去(淘宝客服聊天发不过去)

  • 台式电脑设置在哪里(台式电脑如何安装)

    台式电脑设置在哪里(台式电脑如何安装)

  • 个人qq群标签怎么设置(个人qq群标签怎么改名字)

    个人qq群标签怎么设置(个人qq群标签怎么改名字)

  • 微课传奇能退款吗(微课传奇不让看了吗)

    微课传奇能退款吗(微课传奇不让看了吗)

  • 抖音视频设置不了私密(抖音视频设置不给谁看会影响流量吗)

    抖音视频设置不了私密(抖音视频设置不给谁看会影响流量吗)

  • 如何设置淘宝支付方式(如何设置淘宝支付扣款顺序)

    如何设置淘宝支付方式(如何设置淘宝支付扣款顺序)

  • 抖音恶魔缠身特效怎么拍(抖音恶魔缠身特效视频)

    抖音恶魔缠身特效怎么拍(抖音恶魔缠身特效视频)

  • 电脑硬盘坏了怎么办(电脑硬盘坏了怎么造成的)

    电脑硬盘坏了怎么办(电脑硬盘坏了怎么造成的)

  • 手机root在哪里打开(oppo手机root在哪里)

    手机root在哪里打开(oppo手机root在哪里)

  • oppo手机qq消息不弹窗(oppo手机qq消息不显示内容)

    oppo手机qq消息不弹窗(oppo手机qq消息不显示内容)

  • Linux虚拟机怎么拍摄快照并管理?(linux虚拟机怎么输入中文)

    Linux虚拟机怎么拍摄快照并管理?(linux虚拟机怎么输入中文)

  • ab命令  Web服务器的性能测试工具(abap webservice)

    ab命令 Web服务器的性能测试工具(abap webservice)

  • 年报纳税总额能查到吗
  • 一般纳税人资格证明在哪里查询
  • 销项税是什么意思进项税是什么意思
  • 美元利息结汇时结汇项目是什么
  • 个税身份证验证不通过已经申报缴税
  • 网上申报中月平均工资怎么计算
  • 资产减值损失为什么不能转回
  • 企业间借款利息怎么算
  • 招标资料费计入成本吗
  • 存货报废应如何记账
  • 广告服务怎么做分录
  • 自营方式建造固定资产增值税
  • etc开了充值发票还可以开消费发票么?
  • 企业房产税如何申报缴纳
  • 7月1号出台的政策
  • 零售超豪华小汽车交消费税吗
  • 用友填制凭证外币科目没弹出
  • 公司个税申报是什么意思
  • 一般纳税人增值税申报操作流程
  • 财政部2019年39号第七条
  • 未做账怎么补账
  • 转正工资和试用工资区别
  • 劳务派遣 税务
  • 增值税查补税款可以开发票吗
  • profiler.exe - profiler是什么进程 有什么用
  • 苹果电脑开机声音怎么关
  • 关于销售免税品的规定
  • mac屏幕横过来了
  • 应付账款周转天数越大说明什么
  • wifi出现感叹号上不了网怎么办
  • echarts纵坐标加单位
  • php中implode
  • 辅导期一般纳税人标准
  • js如何随机生成字符串
  • php删除数组中指定元素
  • 如何使用axios直接发送GET请求
  • opencv讲解
  • 电子发票如何发邮箱给别人
  • index.php备份文件名
  • 给第三方的销售怎么做
  • 实际缴纳的增值税用哪个科目
  • 空白发票作废怎么处理
  • 成立业主委员会申请书
  • 折旧方法有哪些英文
  • 抵债资产如何做债权转让
  • 会员卡充值赠送金额怎么做账
  • 公司在建厂房图片大全
  • 低值易耗品属于周转材料吗
  • 所得税纳税调增会计需要做业务处理吗
  • 事业单位对公账户和零余额账户一样么
  • 资产状况信息公示还是不公示
  • 财务预算编制方法包括
  • 支票丢失需要登报吗
  • 赡养老人扣除标准个税
  • 如何办icp许可证
  • 企业建账的法律规定
  • sqlserver 版本号
  • win10预览版和正式版区别
  • win2008 server r2 intel无法安装网卡驱动不存在英特尔PRO适配器的解决方法
  • linux系统中用户密码保存在什么文件中
  • linux系统讲解
  • windowxp系统升级
  • 虚拟机的ubuntu
  • ubuntu系统如何设置永不休眠
  • centos云服务器
  • 升级win10后c盘满了
  • c#程序代码大全
  • css样式表三种方式
  • jquery自动完成插件
  • python快速上手 自动化
  • js中tolocalestring
  • 文件管理android访问限制
  • unity3drpg游戏
  • js中的?
  • listview添加按钮
  • 掌上税务app
  • 出租车发票真伪怎么查询
  • 房屋出租到哪里挂出去
  • 2018年企业所得税
  • 税务总局纪律作风
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设