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

  • 双清是什么意思(外贸双清是什么意思)

    双清是什么意思(外贸双清是什么意思)

  • 荣耀play4是什么处理器(荣耀play4是什么时候出的)

    荣耀play4是什么处理器(荣耀play4是什么时候出的)

  • 学信网的照片怎么下载(学信网的照片怎么下载原图)

    学信网的照片怎么下载(学信网的照片怎么下载原图)

  • 微星主板game boost是什么意思(微星主板game boost)

    微星主板game boost是什么意思(微星主板game boost)

  • 苹果11无法隔空投送怎么办(苹果无法隔空投送找不到联系人)

    苹果11无法隔空投送怎么办(苹果无法隔空投送找不到联系人)

  • tof镜头能干什么(tof镜头有用吗)

    tof镜头能干什么(tof镜头有用吗)

  • xlsm是什么文件(xlsm是什么文件宏)

    xlsm是什么文件(xlsm是什么文件宏)

  • 苹果屏幕边缘黑色阴影(苹果手机屏幕边缘黑斑)

    苹果屏幕边缘黑色阴影(苹果手机屏幕边缘黑斑)

  • 声卡对方听不到伴奏(声卡对方听不到效果)

    声卡对方听不到伴奏(声卡对方听不到效果)

  • word怎么设置左右两部分(word怎么设置左边图片右边文字)

    word怎么设置左右两部分(word怎么设置左边图片右边文字)

  • amd四核处理器相当于i几(amd四核cpu性能排行)

    amd四核处理器相当于i几(amd四核cpu性能排行)

  • 选定整个文档的快捷键(新建文档)

    选定整个文档的快捷键(新建文档)

  • 如何为word设置密码(如何为word设置人脸指纹或pln)

    如何为word设置密码(如何为word设置人脸指纹或pln)

  • 电脑进入安全模式按哪个键(电脑进入安全模式后怎么正常开机)

    电脑进入安全模式按哪个键(电脑进入安全模式后怎么正常开机)

  • 美团怎么买东西的步骤(美团怎么买东西的步骤外卖)

    美团怎么买东西的步骤(美团怎么买东西的步骤外卖)

  • isp是啥(ISP是啥名称)

    isp是啥(ISP是啥名称)

  • word文档怎样填充颜色(word文档怎样填充)

    word文档怎样填充颜色(word文档怎样填充)

  • 淘宝备注在哪能看见(淘宝备注在哪能看见之前的备注)

    淘宝备注在哪能看见(淘宝备注在哪能看见之前的备注)

  • 蓝牙耳机不煲机会怎样(蓝牙耳机不煲机后果)

    蓝牙耳机不煲机会怎样(蓝牙耳机不煲机后果)

  • 苹果如何拦截网络电话(苹果如何拦截网页广告)

    苹果如何拦截网络电话(苹果如何拦截网页广告)

  • 小米手机照相模糊原因(小米手机照相模式怎样退出)

    小米手机照相模糊原因(小米手机照相模式怎样退出)

  • 快手看别人视频有记录吗(快手看别人视频会有访客记录吗)

    快手看别人视频有记录吗(快手看别人视频会有访客记录吗)

  • 抖音点赞受限怎么解决(抖音点赞受限怎么解除限制)

    抖音点赞受限怎么解决(抖音点赞受限怎么解除限制)

  • 微信怎么不让别人看到手机号(微信怎么不让别人看到我的实名)

    微信怎么不让别人看到手机号(微信怎么不让别人看到我的实名)

  • oppo照片水印怎么加(oppo照片水印怎么修改时间)

    oppo照片水印怎么加(oppo照片水印怎么修改时间)

  • 小米手机的耳机孔在哪里(小米手机的耳机模式怎么关)

    小米手机的耳机孔在哪里(小米手机的耳机模式怎么关)

  • 如何调整python窗口(Tkinter)大小(python如何设置窗口背景色)

    如何调整python窗口(Tkinter)大小(python如何设置窗口背景色)

  • 小规模计提税金及附加会计分录怎么写
  • 外贸出口增值税怎么申报
  • 一税两费可以从费用中扣除吗
  • 会展服务费怎么开票
  • 短期借款明细账应采用三栏式账页格式
  • 企业注销资产负债表年初数要调整吗?
  • 税务局返还的三种税种
  • 2021年金税盘
  • 填写银行结算凭证的有关印鉴,应集中由谁保管
  • 分期收款销售会计分录(现销)
  • 小规模无票收入怎么做账
  • 吸收合并控股合并新设合并的区别
  • 地税保险单位编号是什么
  • 个人所得税手续费返还账务处理
  • 公司增值税发票有限额吗
  • 什么情况下一般疑问句用do
  • 土地使用税能计入土地转让的土地增值税清算吗
  • 残保金和工会经费需要计提吗
  • 所得税补缴自查需要缴纳什么
  • 雇主责任险税务处理
  • 特殊性税务处理弥补亏损限额
  • 财务费用明细科目怎么写
  • 公司内部培训师试讲开场白
  • 物业管理体现在哪些地方
  • 资产负债中应交税费怎么算
  • 资产负债表里面的应收账款包括哪些
  • 计提固定资产会计科目
  • 行政事业单位的固定资产不计提折旧
  • 应付的职工薪酬
  • 暂估冲回账务处理
  • 临时文件夹移动到c盘根目录下windows7
  • 结转进项税销项税额的分录怎么写
  • 实例简介php的一般过程
  • php大量数据处理
  • 分公司收到总公司拨款怎么做分录
  • 企业购进固定资产其入账成本应包括
  • 支付劳务公司的劳务费计入应付账款还是其他应付款
  • 开发票为什么要对公账户?
  • 固定资产进项抵扣新政策2021
  • 自注意力机制是什么
  • 新成立的公司怎么算生育津贴
  • 增值税进项税额计算公式
  • 自动结转制造费用生成不了凭证怎么办
  • 个人餐饮费发票
  • 跆拳道馆薪酬详细方案
  • 一般纳税人条件要求2020
  • 收到预收款开具发票如何入账
  • 固定资产被盗怎么处理
  • 一般纳税人取得普票怎么做账
  • 享受所得税减免优惠的生产性外商投资企业包括
  • 长期借款和长期贷款一样吗
  • 员工垫付的钱会计分录
  • 飞机发票和行程单一样吗
  • 会计师事务所工作怎么样
  • SQL Server 作业同步 (结合备份作业)
  • 安装sql2008时出现以下错误
  • window10预览版
  • 2t硬盘如何分区最合理
  • winxp系统怎么设置默认账户登入
  • mac支持的字体格式
  • linux ./执行
  • 如何快速卸载游戏
  • 如何卸载centos7
  • 查看win8.1版本
  • centos如何操作
  • win8怎么下载qq
  • Win10 Mobile build 10586.338上手视频曝光
  • 校园网升级套餐
  • Linux系统开源VPN Openvpn进程异常退出解决方法
  • jquery ajax异步提交表单数据的方法
  • python生成密钥
  • python操作微信自动发消息
  • 批处理/d
  • javascript中函数
  • unity协程的工作原理
  • Shell中使用scp命令实现文件上传代码
  • javascript面向
  • jquery获取outerhtml
  • c# addin
  • 江苏省官网
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设