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

  • 苹果135g网怎么开(苹果135g怎么变4g)

    苹果135g网怎么开(苹果135g怎么变4g)

  • 抖音内测版本怎么下载(抖音内测版本怎么下)

    抖音内测版本怎么下载(抖音内测版本怎么下)

  • p30pro需要贴膜吗(华为p30pro手机需不需要贴膜)

    p30pro需要贴膜吗(华为p30pro手机需不需要贴膜)

  • b站怎么设置退出后台继续播放(b站怎么设置退出后仍播放)

    b站怎么设置退出后台继续播放(b站怎么设置退出后仍播放)

  • 修改微信号的条件(修改微信号的条件怎么满足)

    修改微信号的条件(修改微信号的条件怎么满足)

  • 缓存穿透和缓存击穿有什么区别(缓存穿透和缓存的区别)

    缓存穿透和缓存击穿有什么区别(缓存穿透和缓存的区别)

  • 华为mate40pro发布时间(华为mate60pro发布)

    华为mate40pro发布时间(华为mate60pro发布)

  • 常用bcd码有哪些(bcd码含义)

    常用bcd码有哪些(bcd码含义)

  • 笔记本16g和8g差距大吗(笔记本8g和16g内存差多少钱)

    笔记本16g和8g差距大吗(笔记本8g和16g内存差多少钱)

  • 卖家给买家差评买家会知道吗(卖家给买家差评怎么办)

    卖家给买家差评买家会知道吗(卖家给买家差评怎么办)

  • oppo手机自带天气软件怎么恢复(oppo手机自带天气在哪里)

    oppo手机自带天气软件怎么恢复(oppo手机自带天气在哪里)

  • 视频声音延迟怎么解决(调整视频声音延迟app)

    视频声音延迟怎么解决(调整视频声音延迟app)

  • 电池长时间间不充电,充不进去怎么办(电池长时间不使用)

    电池长时间间不充电,充不进去怎么办(电池长时间不使用)

  • 腾讯课堂有摄像头吗(腾讯课堂有摄像头功能吗)

    腾讯课堂有摄像头吗(腾讯课堂有摄像头功能吗)

  • 去iphone售后帮验机吗(苹果售后验证手机要钱吗)

    去iphone售后帮验机吗(苹果售后验证手机要钱吗)

  • 路由器的上网账号和口令是什么(路由器的上网账号是什么意思)

    路由器的上网账号和口令是什么(路由器的上网账号是什么意思)

  • 只有群主才可以踢人吗(微信群艾特所有人是不是只有群主才可以)

    只有群主才可以踢人吗(微信群艾特所有人是不是只有群主才可以)

  • vivoy93语音助手怎么使用(vivoy93语音助手在哪里)

    vivoy93语音助手怎么使用(vivoy93语音助手在哪里)

  • vivox27怎么开人脸(vivox27怎么开启人脸识别)

    vivox27怎么开人脸(vivox27怎么开启人脸识别)

  • oppo添加桌面小部件(oppo添加桌面小组件)

    oppo添加桌面小部件(oppo添加桌面小组件)

  • jkmtl00华为什么型号(jkmtl00b华为什么型号)

    jkmtl00华为什么型号(jkmtl00b华为什么型号)

  • qq新版本怎么绑定闺蜜关系(新版qq怎么绑定好友关系)

    qq新版本怎么绑定闺蜜关系(新版qq怎么绑定好友关系)

  • 苹果x运营商怎么修改(iphone运营商在哪)

    苹果x运营商怎么修改(iphone运营商在哪)

  • centos7安装clickhouse并设置用户名密码案例详解(CENTOS7安装报错)

    centos7安装clickhouse并设置用户名密码案例详解(CENTOS7安装报错)

  • 个人开发选项
  • 计提印花税会计分录最新
  • 房租押金不退如何
  • 所购税控盘减免税款
  • 财务专用章的重要性
  • 简易征收的老项目是指什么时候
  • 小规模企业所得税多少
  • 发票没有写纳税人识别号可以吗
  • 税控发票包括哪些
  • 现金折扣在买方还是卖方
  • 交印花税合同金额怎么算
  • 报销购物卡发票公司如何交税?
  • 银行结算账户的规定
  • 年终所得税汇算提示成本费用与实际取得发票有差异
  • 期末结转增值税顺序
  • 金融资产交易中心是干嘛的
  • 卷烟消费税税率表
  • 持续经营净利润是什么意思
  • 委托代理出口能否办理退税
  • 高新技术企业资格复核
  • 退回的费用如何入账
  • 解决的英文
  • 电脑每次开机都要选择系统怎么办
  • 进口免费赠送报成了一般贸易
  • 怎么更新电脑系统win10
  • thinkphp yii
  • bios屏蔽接口
  • zmweb.exe是什么进程
  • 埃热泽尔斯湖面上的波纹,拉脱维亚拉特加尔地区 (© Eaglewood Films/Nimia)
  • 父母将住房无偿赠送给子女 增值税
  • 年终双薪要做满一年嘛
  • 来料加工业务的会计处理
  • 其他应付款清账
  • 补交上年所得税怎么调表
  • 有哪些员工福利
  • 增值税发票有什么用途和作用
  • 在vue项目如何引入异步组件?
  • python 覆盖
  • 织梦怎么用模板建站
  • 其他业务收入与营业外收入
  • 进口税 增值税
  • sql server2005使用方法
  • 一般纳税人提供财政部和国家税务总局规定的
  • 公司注册登记需要多少钱
  • 专用发票丢失怎么入账
  • 预缴税款是什么税款
  • 公司基建维修承诺书
  • 福利费的进项税额会计分录
  • 小规模纳税人涉嫌虚开发票
  • 房产税开征利好那些概念股 新闻
  • 进项转出再转入
  • 会计处理的相关知识点
  • 固定资产折旧方法一经确定不得随意变更
  • 房地产开发企业增值税税率
  • 本年利润年末有余额怎么回事
  • 租金收入分摊政策规定
  • 记账凭证的附件和附单据
  • eclipse连接mysql代码
  • linux 清理
  • centos lftp
  • wpr是什么软件
  • win8.1 下载
  • centos查看文件目录
  • win7系统打开软件慢的原因
  • .sfx.exe是什么文件
  • 如何删除win7系统
  • cocos2dx游戏开发框架
  • java程序员准备骑驴找马了,需要怎么准备
  • pycharm安装教程2020.2
  • nodejs搭建本地服务器运行html
  • jquery 延迟对象
  • 深入理解javascript特性.pdf
  • JavaScript File API实现文件上传预览
  • 安卓手机管家怎么关闭
  • jQuery插件是什么
  • Python中列表元素可通过什么访问
  • 企业所得税税率10%
  • 虚开增值税发票但未抵扣怎样处罚
  • 南京市车位产权证办理流程
  • 扣缴个人所得税怎么计算
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设