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

  • 安卓手机如何打开.mtz文件(安卓手机如何打开nfc功能)

    安卓手机如何打开.mtz文件(安卓手机如何打开nfc功能)

  • 高德地图如何查看公交车实时位置(高德地图如何查看街景实景)

    高德地图如何查看公交车实时位置(高德地图如何查看街景实景)

  • iphone11promax是双卡双待吗(iphone11promax是双层主板吗)

    iphone11promax是双卡双待吗(iphone11promax是双层主板吗)

  • 小米lot模组是什么

    小米lot模组是什么

  • mov文件有些能播放有些不能(mov文件怎么播放不了)

    mov文件有些能播放有些不能(mov文件怎么播放不了)

  • 为什么qq相册里的照片显示不出来(为什么qq相册里的照片变小了)

    为什么qq相册里的照片显示不出来(为什么qq相册里的照片变小了)

  • 苹果系统微信什么时候可以改微信号(苹果系统微信新功能)

    苹果系统微信什么时候可以改微信号(苹果系统微信新功能)

  • 网易云hd和网易云音乐区别(网易云hd和网易云音乐可以同步登陆吗)

    网易云hd和网易云音乐区别(网易云hd和网易云音乐可以同步登陆吗)

  • 什么是录屏(什么是录屏类微课)

    什么是录屏(什么是录屏类微课)

  • 苹果11闪电接口检测到液体怎么办(苹果11闪电接口坏了换一个要多少钱)

    苹果11闪电接口检测到液体怎么办(苹果11闪电接口坏了换一个要多少钱)

  • 苹果11的三种尺寸(苹果11尺寸尺寸)

    苹果11的三种尺寸(苹果11尺寸尺寸)

  • 华为手机卡1卡2设置在哪里(华为手机卡1卡2在哪里查找到)

    华为手机卡1卡2设置在哪里(华为手机卡1卡2在哪里查找到)

  • 华为手机怎么下载腾讯视频(华为手机怎么下载第二个微信)

    华为手机怎么下载腾讯视频(华为手机怎么下载第二个微信)

  • 抖音上的下载文件在哪(抖音上的下载文件在哪找)

    抖音上的下载文件在哪(抖音上的下载文件在哪找)

  • 怎么下载当贝市场到电视(怎么下载当贝市场到u盘)

    怎么下载当贝市场到电视(怎么下载当贝市场到u盘)

  • ps磨砂质感怎么做(ps做磨砂质感)

    ps磨砂质感怎么做(ps做磨砂质感)

  • 玩抖音合拍合唱是怎么玩的(抖音合唱合拍视频)

    玩抖音合拍合唱是怎么玩的(抖音合唱合拍视频)

  • 手机npc模式是什么意思(手机的npc是什么意思)

    手机npc模式是什么意思(手机的npc是什么意思)

  • 如何查看电脑显卡内存(如何查看电脑显卡型号)

    如何查看电脑显卡内存(如何查看电脑显卡型号)

  • iphone清理缓存小技巧(iphone 清缓存)

    iphone清理缓存小技巧(iphone 清缓存)

  • xr几个卡槽(苹果xr几个卡槽)

    xr几个卡槽(苹果xr几个卡槽)

  • 富士x100t和x100f区别

    富士x100t和x100f区别

  • 类纸膜磨笔头吗(类纸膜磨笔头怎么办)

    类纸膜磨笔头吗(类纸膜磨笔头怎么办)

  • 苹果8p特殊功能介绍(iphone8特殊功能)

    苹果8p特殊功能介绍(iphone8特殊功能)

  • 朋友圈头像双击为什么震动(朋友圈头像双击动作装扮是什么意思)

    朋友圈头像双击为什么震动(朋友圈头像双击动作装扮是什么意思)

  • 怎么关闭qq小程序功能(怎么关闭qq小程序游戏功能)

    怎么关闭qq小程序功能(怎么关闭qq小程序游戏功能)

  • 如何体验电脑管家游戏加速功能?(电脑管家怎么检测)

    如何体验电脑管家游戏加速功能?(电脑管家怎么检测)

  • DedeCMS 首页分类信息两列调用方法(dedecms分页标签)

    DedeCMS 首页分类信息两列调用方法(dedecms分页标签)

  • 购买金税盘和维税盘区别
  • 流转税具体的账务处理,可以怎样考虑?
  • 工程款税率是多少专票
  • 用友t3采购订单怎么录入
  • 其他应收款收不回来税务处理
  • 金融业产值和金融业增加值有什么不同
  • 增值税税控系统专用设备
  • 旅游企业纳税
  • 工人工伤误工费赔偿多少一天
  • 缴纳的增值税在资产负债表中怎么体现
  • 一般纳税人劳务费税率是13还是6
  • 增值税降到16个点什么时候实行
  • 同一控制下企业合并报表编制
  • 什么样的情况可以补缴社保
  • 退所得税的分录
  • 技术研发支出会计科目
  • 暂估成本跨年后未收到票如何处理
  • 银行卡季度结息
  • 应交税金明细账余额怎么算
  • 物业公司收取水费如何开具发票
  • 一般纳税人可以开1%的发票吗
  • 新公司注册后先开户还是先税务登记
  • 银行承兑汇票承兑手续费是多少
  • 增值税的销项税额和进项税额的区别
  • win10桌面网络图标怎么调出来
  • 基本户里面的钱的用途
  • win10网页打不开但是有网
  • 私款转入公司账户怎么办
  • 挂账多年的往来账怎么处理
  • giantantispywaremain.exe是什么进程 有什么作用 giantantispywaremain进程查询
  • php的mysql_query
  • 销货退回与折让属于什么科目
  • 未按规定开具发票怎么处罚
  • 篱笆的样子
  • php图片代码
  • Vue+Element UI-el-date-picker时间日期选择器组件(实现向后端传值)
  • 计算机领域划分
  • jdbc连接mysql的url怎么写
  • php识别验证码的库高级
  • python中的包有哪些
  • 微服务框架图
  • 一次性收取一年租金增值税怎么计算
  • 劳务费800以上扣税标准
  • 股权转让协议受让方应注意
  • 小说连载阅读安卓版下载
  • 其他应付款在清算时怎么处理
  • 如何确定固定资产的原始成本
  • 捐赠支出怎么抵税
  • 税务相关政策的解读
  • 固定资产清理的借贷方向
  • 年度奖金个税计算器
  • 垃圾处理费怎么申报
  • 生产经营个人所得税税率表
  • 临时工工资无发票可以扣除吗
  • 投资性房地产递延所得税其他综合收益
  • Mysql 数据库 拷贝 原文件
  • mysql连接是什么协议
  • 简单介绍清明上河图
  • Mysql中explain作用详解
  • mysql不能运行
  • insert into tbl() select * from tb2中加入多个条件
  • sqlserver响应好慢
  • 修改注册表解决画面撕裂
  • vs显示进程已退出
  • 找回丢失的抖音如何恢复抖音
  • fsck.mode
  • win7系统如何将插孔设置为ac97前面板
  • javascript运用
  • app塔防游戏
  • shell 脚本 判断
  • jquery弹出窗口的方法
  • jquery 字符串以什么开头
  • bat文件指令
  • jquery实现移动端
  • 深入理解计算机系统
  • unity怎么录屏
  • andriod中SimpleAdapter+listview,点击item 传值事件
  • kmp算法理解
  • 购货方已抵扣的发票购货方怎么申请红冲
  • 印花税计征周期
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设