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

  • 销项负数的分录怎么做
  • 无形资产登记什么明细账
  • 工资表只显示一个人的其他看不见
  • etc怎么取过路费啊
  • 有限合伙企业有经营权吗
  • 纳税申报现金流量表报错了可以重新申报吗
  • 企业季度预缴所得税怎么账务处理
  • 交易性金融资产借贷方向
  • 应收票据周转率高
  • 发票冲红增值税怎么申报
  • 公司商务卡可以日常消费
  • 汇兑损益在外币业务核算中有什么重要意义
  • 劳务分包服务费率
  • 增值税实际缴纳总额
  • 企业增值税税负率与利润率的关系
  • 公司账户境外汇款
  • 公司变卖废纸也需要缴税
  • 加工贸易的方式
  • 返利发票税率
  • 原始凭证的审核内容
  • 查补的增值税如何申报
  • 减免税款未抵扣增值税
  • 生产成本期末余额在贷方
  • 王者荣耀百里守约是男是女
  • 企业财政拨款所得免税吗
  • 支付店面租金为什么是管理费用
  • 如何设置谷歌浏览器语言为中文
  • 专项维修基金会产生利息吗
  • 个税这个月多交了下个月减回来
  • linux添加系统用户命令
  • 最强超频
  • 小规模纳税人免税额度是多少
  • 食堂维修费用计什么科目
  • 如何快速使用验证码
  • html-css
  • php使用自定义函数编程求半径r的圆的周长和面积
  • php获取浏览器ua
  • 人工智能怎么做出来的
  • 计算机视觉基础知识
  • modprobe operation not permitted
  • linux执行php脚本
  • 公司开的餐费专票可以抵扣吗
  • 固定资产如何分类?
  • 详解MySQL中EXPLAIN解释命令及用法讲解
  • 金税四期功能
  • 固定资产计提折旧的会计科目
  • 一般纳税人收到的普票可以抵扣吗
  • 买电脑怎么做账
  • 申报成功后怎么更正申报
  • 收到汇款计入什么科目
  • 持有至到期投资减值准备
  • 研发阶段的产品质检要参与
  • 软件行业成本如何归集
  • 修改mysql配置的两种方法
  • sqlserver数据库维护
  • rhel7.2安装
  • u盘安装win8系统教程图解
  • 如何查看文件系统
  • win7装vmware
  • 苹果电脑如何缩小屏幕显示
  • ctfmon.exe成功怎么解决
  • Windows update更新服务启动被拒绝访问
  • winxp如何禁用u盘
  • windows8 如何关机
  • win10无internet怎么办
  • Android NDK Cocos2dx 3.4 Label中文显示
  • node urlencode
  • jqgrid设置宽度
  • 10分钟掌握心理学
  • 执行批处理命令
  • 配置ip地址的方法有哪几种
  • 深入理解新发展理念,推进供给侧结构性改革心得体会
  • shell操作oracle数据库
  • javascript语言基础
  • js中.html
  • 广州税务举报电话
  • 税控盘升级后开票流程
  • 重庆市国税电子税务局
  • 上海地税定额发票在哪开
  • 城建税教育费附加怎么做分录
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设