教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 文秘资料 >

networking socket使用方法

来源:网络收集 时间:2026-01-22
导读: socket简易介绍和使用 Networksocketprogramming Basicsocetprogramming JensBandener (StudentElektro-andInformationstechnology) May6,2010 Introduction Thisshortskriptexplainsthebasicimplementationstepsforsocketbasednet-workcommunication. Socke

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

networking socket使用方法.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/2177946.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)