networking socket使用方法
socket简易介绍和使用
Networksocketprogramming
Basicsocetprogramming
JensBandener (StudentElektro-andInformationstechnology)
May6,2010
Introduction
Thisshortskriptexplainsthebasicimplementationstepsforsocketbasednet-workcommunication.
Socketsareendpointsofbidirectionalinter-processornetworkcommunicationsusinganIPbasednetworkeitherforstreambasedorfordatagrambasedtrans-fers.Thesocketconnectioncanbeusedtotransferdataofanytypebetweenthetwoendpoints.Thesocketinterfaceisprovidedbytheoperatingsystem,whichmanagesthesocketsandtheconnection-information.
AttheEndofthisskriptyou ndaworkingexampleserver/clientapplicationshowingtheneccessarystepsforcommunicationbetweentheserverandtheclient.
Jens.Bandener@rub.de
socket简易介绍和使用
Contents
Introduction
2Socketcategories
2.1Communicationmodell........................3Socketprogramming
3.1Creatingsockets.......3.2Connectionestablishment..3.3Bindthesocket........3.4Server-side-listenandaccept3.5Sendingandreceivingdata.
....................................................................................................
123334555667
4Simpleserver-clientapplication
4.1clientside...............................4.2serverside...............................
2Socketcategories
TCPsocketsarestructuresdevelopedasusableinterfacesforimplementingnetworkconnectiontoapplications.Socketsareanindustrialstandardandimplementedinmanyprogramminglanguages.
Generally,aSocketisan ledescriptorandcanbedevidedintotwomaincategories:(1)streamand(2)datagramm
sockets.
Figure1:socketcategories
The rstcategory,stream,isusede.g.inTCPconnections.Streambasedsockedsprovidesequenzingandanerrorcorrection.Sequenzingmeans,thatthesocetconnectionguarantees,thateachsegmentofthe leisachievedinthesameorderitwassent.
2
socket简易介绍和使用
Datagrammbasedsockets,http://edinUDPprotocols,arepackedorientedtransfers.Thisisaconnectionlesstransferinwhichthe leispidedinpacketsbeforetransfering.Incontrasttothestreamtransfers,thedatagrammbasedtransfershavenoerrorcorrectionorsequenzing.
2.1Communication
modell
Figure2:socetcommunication
Figure2showsthemainideainthesocketarchitecture.Insteadofprogram-mingthelowlevelnetworkcommunication,youcancreateasocket-connectionbetweentwoprocessesmanagedbytheoperatingsystem.Afterconnectingtwosocketsyoucanuseallstandartfunctionstoe.g.forward lesetc.tothesocet.Thesocketmanagestheremainingcommunicationprocess.Thisproceedingisverysimilartotheinterprocess-communicationintheunixsystem.
3
3.1
Socketprogramming
Creatingsockets
Socketscanbecreatedusingthesocket()command.Therequiredlibrariesforsocketprogrammingaresys/types.handthesys/socket.hlibrary.
Whencreatinganewsocketyouhavetospecifythefamily,thetypeandthedesiredprotocolforthesocket.
Listing1:creatinganewsocket
1
intsocket(intfamily,inttype,intprotocoll)
The rstparameterintfamiliyspeci estheprotocolfamily.ThemostcommonprotocolistheAF_INETfortheTCP/IPcommunicationortheAF_UNIXforthelocalinterprocesscommunicationwithintheunix-system.Anextractofthepossiblefamiliesislistedintable1.
Thesecondparameterspeci esthesockettype.Typicallyyouchoosebetweenstream-sockets(SOCK_STREAM)anddatagramm-sockets(SOCK_DGRAM).Thereareafewmoretypesforspecialuseswhicharenotannotatedatthispoint.
3
socket简易介绍和使用
adress
AF_UNIX,AF_LOCAL
AF_INETAF_IPX
protocol
PF_UNIX,PF_LOCAL
PF_INETPF_IPX
meaning
localcommunication
IPv4internetcommunication(TCP/IP)
IPX-Novell-protocol
Table1:protocolfamilies, rstparameter
Thethirdandlastparameterallowsyoutochoosetheusedprotocol.Typicallythisparameterissetto0whichmeansthatthedefaultprotocolforthespeci edsocked-typeisused.
3.2Connectionestablishment
Toconnecttwoprocesseswithalreadyimplementedsocketsyoucanusetheconnect()commandtoestablishtheconnection.
Listing2:connectingasocket
123
intconnect(
intsockfd,
structsockaddr addresssocklen_taddress_length);
Youneedtospecifythethreeparametersintheconnect()commandtoestablishtheconnection.Asintheexampleprogrammattheendofthisscript,theclienttypicallyconnectstoaserverapplicationn.Sotheconnect()commandwillbefoundintheclientapplication.
The stparametergivesthesocket,whichisusedfortheconnectiontotheserver.Thesecondparameterspeci esthetheaddresstothedestinationsocket.Thisstructisspeci edinthenetinet/in.hheader le.
Listing3:extractnetinet/in.h
1234567891011
#include<sys/socket.h>#include<netinet/in.h>structsockaddr_in{
unsignedshortintsin_family;unsignedshortintsin_port;structin_addrsin_addr;
}
structin_addr{
in_addr_ts_addr;
}...
Tomatchtherequiredtypesintheconnectcall,youneedtocastthesecondparametertostructsockaddr.
4
socket简易介绍和使用
Listing4:connectingasocket
123
structsockaddr_inaddr;
intconnect(socket,(structsockaddr )&addr,
sizeof(addr));
Alternativetothecasttothesockaddryoucouldstraightusethestructsockaddr.Theadvantageofthesockaddr_inistheoptiontogivetheaddressandtheportseparatelyinsteadofgivingtheprotocolspeci caddress.
3.3Bindthesocket
Afterconnectingthesocketyouneedto“bind”thesockettoaspeci edport.Thecallforthebind()functionissimilartotheconnect()function.
Listing5:bindfunction
123
intbind(
intsockfd,
structsockaddr addresssocklen_taddress_length);
Incaseofasuccessfulbindthebind()functionreturns0,otherwiseitreturns 1forafailure.
3.4Server-side-listenandaccept
Ontheoppositeside,heretheserverapplication,theserver-socketneedstolistenonthespeci edportandpossiblyaccepttheincomingconnectionfromtheclientapplications.Typicallyitispossibletohandlemorethenoneincomingconnection.Fort …… 此处隐藏:7740字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [文秘资料]班长职务辞职报告
- [文秘资料]完美的辞职报告
- [文秘资料]经典的员工辞职报告
- [文秘资料]医院口腔医生辞职报告
- [文秘资料]总经理辞职报告范文四篇
- [文秘资料]超市职员个人辞职报告
- [文秘资料]村妇联主任的辞职报告
- [文秘资料]辞职报告书格式
- [文秘资料]酒店辞职报告简单范文
- [文秘资料]联通的辞职报告
- [文秘资料]2017最新私企员工辞职报告范文
- [文秘资料]2019年度医院基层党组织书记抓党建述职
- [文秘资料]工作时间长辞职报告
- [文秘资料]辞职报告怎么写出来
- [文秘资料]个人能力原因辞职报告
- [文秘资料]网络工程师辞职报告
- [文秘资料]项目部辞职报告
- [文秘资料]缝纫工辞职报告怎么写
- [文秘资料]XXX州委书记述职报告
- [文秘资料]抓基层党建工作述职报告
- (王虎应老师讲课记录)六爻理象思维
- 八个常见投影机故障排除法
- 质量专业综合知识(中级)第一章质量管理
- 煤矿班组建设实施意见
- 我国快餐业与肯德基经营模式的比较与分
- 汽车保险杠模具标准化模架技术工艺研究
- 汽车二级维护作业团体赛比赛规程
- 装卸搬运工安全操作规程
- 高效的工作方法-刘铁
- 依据《生产安全事故报告和调查处理条例
- 2015专业PS夜景亮化效果图制作教程
- 企业劳动定额定员浅析
- 中枢神经系统医学影像学本科五年制第五
- 长城汽车参观探营第三站:研发试验中心
- 小升初语文专项训练
- 建筑工程质量检测资质分类与等级标准
- 周燕珉-我国养老社区的发展现状与规划
- 《生命里最后的读书会》读后感
- 实验室管理评审报告
- CCNA思科网院教程精华之网络基础知识




