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

  • 企业经营管理培训班大概多少钱哪里去学

    企业经营管理培训班大概多少钱哪里去学

  • vivox70怎么长截屏(vivo怎样截长屏)

    vivox70怎么长截屏(vivo怎样截长屏)

  • iwatch7怎么看照片(applewatch怎么看全部照片)

    iwatch7怎么看照片(applewatch怎么看全部照片)

  • 小米10支持65w充电器吗(小米10支不支持65w快充)

    小米10支持65w充电器吗(小米10支不支持65w快充)

  • 芒果tv浮窗权限怎么开启(芒果tv如何悬浮窗)

    芒果tv浮窗权限怎么开启(芒果tv如何悬浮窗)

  • 小米系统截长屏被限制长度(小米截长屏幕截图)

    小米系统截长屏被限制长度(小米截长屏幕截图)

  • 苹果x怎么显示电量百分比(苹果x怎么显示电量)

    苹果x怎么显示电量百分比(苹果x怎么显示电量)

  • 存储器容量的基本单位(存储器容量的基本单位是)

    存储器容量的基本单位(存储器容量的基本单位是)

  • pro max什么意思(promax什么意思中文翻译)

    pro max什么意思(promax什么意思中文翻译)

  • 微信运动取消关注后怎么恢复(微信运动取消关注后别人能看到吗)

    微信运动取消关注后怎么恢复(微信运动取消关注后别人能看到吗)

  • 华为v30耳机接口在哪(华为荣耀v30耳机接口)

    华为v30耳机接口在哪(华为荣耀v30耳机接口)

  • word下划线粗细不一致(论文封面下划线不一样粗)

    word下划线粗细不一致(论文封面下划线不一样粗)

  • nova7是双卡双待吗(nova7是双卡双待手机吗)

    nova7是双卡双待吗(nova7是双卡双待手机吗)

  • 苹果用充电宝伤手机吗(苹果充电宝伤会让手机背面掉漆吗)

    苹果用充电宝伤手机吗(苹果充电宝伤会让手机背面掉漆吗)

  • 华为mate30怎么清理运行程序(华为mate30怎么清理内存最干净)

    华为mate30怎么清理运行程序(华为mate30怎么清理内存最干净)

  • 手机云闪付功能在哪里(云闪付的手机闪付功能)

    手机云闪付功能在哪里(云闪付的手机闪付功能)

  • 苹果xs开机菜单导航怎么关闭(苹果xs开机菜单怎么设置)

    苹果xs开机菜单导航怎么关闭(苹果xs开机菜单怎么设置)

  • cpl滤镜的作用(相机uv镜和偏振镜哪个更有用)

    cpl滤镜的作用(相机uv镜和偏振镜哪个更有用)

  • redmⅰ是什么手机(redm是什么手机)

    redmⅰ是什么手机(redm是什么手机)

  • 新浪微博怎么注销邮箱(新浪微博怎么注册新账号)

    新浪微博怎么注销邮箱(新浪微博怎么注册新账号)

  • 新浪微博怎么注销超话(新浪微博怎么注册新账号)

    新浪微博怎么注销超话(新浪微博怎么注册新账号)

  • 京东白条如何提现(京东白条如何提前还款一部分)

    京东白条如何提现(京东白条如何提前还款一部分)

  • 苹果设置输入法为默认输入法教程(苹果设置输入法在哪里设置)

    苹果设置输入法为默认输入法教程(苹果设置输入法在哪里设置)

  • Python中字符串的strip、lstrip和rstrip(python中字符串的长度怎么算)

    Python中字符串的strip、lstrip和rstrip(python中字符串的长度怎么算)

  • phpcms v9更新栏目缓存失败的原因及解决方法(dedecms更新)

    phpcms v9更新栏目缓存失败的原因及解决方法(dedecms更新)

  • 建筑企业如何纳税
  • 月收入10万以下免税哪些企业适用
  • 运费收入算销售收入吗
  • 出租人融资租赁发生的初始直接费用
  • 销售分哪几类
  • 应收账款贷方余额怎么调平
  • 个体户减免增值税
  • 个税申报初始化密码
  • 公司资料更新
  • 个体户逾期申报罚款多少
  • 事假扣薪
  • 营改增 旅游服务
  • 企业增加注册资金怎么办理
  • 机票电子行程单和发票的区别
  • 分摊费用怎么说合适
  • 库存商品进行非货币资产交换收入怎么确定
  • 没办税务登记公示怎么办
  • 房屋征收服务中心是干什么的
  • 支付的劳务派遣费计入什么科目里
  • 小企业发生销售折让的原因
  • 水利建设基金有优惠政策吗
  • 个人租车给公司租金多少合适
  • 开具红字增值税专用发票是什么意思
  • 增值税小规模纳税人申报表填表说明
  • 公司开出的发票
  • 公办幼儿园食堂21项制度
  • 怎么看是不是专用发票
  • 房产企业开发产品出租
  • 水利建设基金从铁路建设基金港口建设费收入中提取5%
  • 因为担保被起诉怎么办
  • linux如何在终端运行程序
  • 厂家赠送的原材料怎么入账
  • 哪家银行理财产品收益率高
  • 高手成长礼包
  • ccs ide
  • php curl命令详解
  • 配置eAccelerator和XCache扩展来加速PHP程序的执行
  • 应付账款账户的期初贷方余额100元
  • 潘塔纳尔湿地位于巴拉圭盆地
  • 野外生存探险家
  • 先预付货款,货到了怎么做账
  • php增删改查mysqli实例
  • 利息发票如何做账务处理
  • 生产企业支付的车间劳务外包费入账科目
  • 新申报是什么
  • 开增值税发票需要和收款账户名字一致吗
  • 公司借调员工
  • 什么是死锁,死锁的四个必要条件
  • mongodb管道操作符
  • 企业付给个人工资怎么算
  • 个体户开票额度超了
  • concat group
  • 个人开运输发票需要的资料哪些?
  • 白酒系列代理
  • 销售退货成本如何计算
  • 融资租赁会计处理实例承租人
  • 公司既没有收入怎么办
  • 不验旧能领购发票吗
  • 以下各项不属于肝郁气滞证表现的是
  • 酒店食材成本如何分析
  • 零售行业涉及到什么专业
  • 一般纳税人会计分录
  • sqlserver存储过程if语句
  • sql server 性能调优
  • mysql存储过程菜鸟教程
  • centos7 eth
  • wp_official_1.1.3.apk软件下载
  • win8的应用商店
  • hptlbxfx.exe
  • 怎么有才能
  • win10 19043.1237
  • javascript的canvas
  • document.all.value
  • python xml.dom.minidom模块生成xml
  • unityui插件
  • 怎么用bootstrap搭建网站
  • node-js
  • 医保可以异地交嘛
  • 自然人扣缴端初始密码
  • 江苏省电子税务局社保缴费查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设