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

  • 什么是所得税税负
  • 核销 坏账
  • 个税补缴纳
  • 减税后含税单价计算公式
  • 个人所得税怎么扣
  • 货物运输代理服务
  • 小规模纳税人税率2023年是多少
  • 进口设备的重置成本包括( )
  • 装饰费是否计算缴纳土地增值税
  • 已入账未抵扣退回发票
  • 银行转账结算方式的种类及适用范围
  • 哪些收据可以税务登记
  • 承兑汇票兑现怎么填写
  • 企业用于存货的支出
  • 计提固定资产的方法有几种?
  • 非盈利组织捐赠支出
  • 企业购入新能源电车需要缴纳印花税吗
  • 筹建期员工社保分录
  • 企业承担社保
  • 支付境外咨询费代扣代缴增值税
  • 厂家给返点算拿回扣吗
  • 如何在虚拟机上下载软件
  • vc_runtimeMinimum_x64.msi 找不到的解决方法
  • windows为什么从7开始
  • swupdtmr.exe - swupdtmr进程是什么意思 什么作用
  • 管理费用怎么结转分录
  • 5000以下的固定资产当月一次性扣除怎么申报
  • 怎样让鼠标变得好看些
  • win10无法运行软件怎么回事
  • 开机要按f1才能进系统
  • vue2.0解决跨域问题
  • vite首次打开界面加载慢问题/解决
  • php注册功能的实现
  • 喝薏米红豆水最佳时间
  • 附有退回条件的销售商
  • ChatGPT可以做什么
  • 无数据库cms
  • Yii2创建多界面主题(Theme)的方法
  • 使用筷子就餐会不会传染乙肝病毒
  • element ui vue 3
  • 小微企业声明函去哪个部门开
  • 去年亏损今年盈利
  • 财务报表季报利润表本月金额怎么填
  • 帝国cms什么语言
  • 土地增值税扣除项目20%
  • 购买无形资产的入账价值包括增值税吗
  • 小规模纳税人和一般纳税人的区别
  • 个人独资企业与有限公司的区别
  • 专项应付款的会计处理
  • 还银行贷款如何计算利息
  • 银行退汇怎么做会计分录
  • 水利基金征收标准
  • 建筑机械使用安全规范最新版
  • mysql5717安装及配置超详细教程
  • 左右内边距代码
  • Windows Server 2008:手足之争下的赢家
  • 操作系统运行慢怎么解决
  • igs文件是什么文件
  • windowxp系统升级
  • linux中find命令基本使用方法
  • automaticupdate
  • linux防火墙命令大全
  • windows102021年更新
  • mac怎么共享网络给iphone
  • win系统开发
  • win10 64位系统提示0x80070643错误代码的解决方法
  • 在linux中使用v2ray
  • cocos2d教程
  • 分享五个有用的东西
  • 批处理常用命令总结
  • css div高度填满父容器剩余空间
  • python urljoin
  • vue2.0与bootstrap3实现列表分页效果
  • js创建对象的三种方式
  • js 构建树
  • javascript怎么学
  • nodejs python交互
  • unity ugui教程
  • android开发前景
  • 马云交了多少税费
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设