Danh mục

SocketProgramminginC#Part1–Introduction

Số trang: 10      Loại file: doc      Dung lượng: 79.00 KB      Lượt xem: 11      Lượt tải: 0    
Thư Viện Số

Hỗ trợ phí lưu trữ khi tải xuống: 3,000 VND Tải xuống file đầy đủ (10 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

ThepurposeofthisarticleistoshowyouhowyoucandosocketprogramminginC#.Thisarticleassumessomefamiliaritywiththesocketprogramming,thoughyouneednottobeexpertinsocketprogramming.Thereareseveralflavorstosocketprogramminglikeclientside,serverside,blockingorsynchronous,nonblockingorasynchronousetc.Withalltheseflavorsinmind,Ihavedecidedtobreakthissubjectintotwoparts.Inthepart1Iwillstartwiththeclientsideblockingsocket.LateroninthesecondpartIwillshowyouhowtocreateserversideandnonblocking....
Nội dung trích xuất từ tài liệu:
SocketProgramminginC#Part1–IntroductionSocketProgramminginC#Part1–IntroductionThepurposeofthisarticleistoshowyouhowyoucandosocketprogramminginC#.Thisarticleassumessomefamiliaritywiththesocketprogramming,thoughyouneednottobeexpertinsocketprogramming.Thereareseveralflavorstosocketprogramminglikeclientside,serverside,blockingorsynchronous,nonblockingorasynchronousetc.Withalltheseflavorsinmind,Ihavedecidedtobreakthissubjectintotwoparts.Inthepart1Iwillstartwiththeclientsideblockingsocket.LateroninthesecondpartIwillshowyouhowtocreateserversideandnonblocking.Networkprogramminginwindowsispossiblewithsockets.Asocketislikeahandletoafile.SocketprogrammingresemblesthefileIOasdoestheSerialCommunication.Youcanusesocketsprogrammingtohavetwoapplicationscommunicatewitheachother.Theapplicationaretypicallyonthedifferentcomputersbuttheycanbeonsamecomputer.Forthetwoapplicationstotalktoeacheitheronthesameordifferentcomputersusingsocketsoneapplicationisgenerallyaserverthatkeepslisteningtotheincomingrequestsandtheotherapplicationactsasaclientandmakestheconnectiontotheserverapplication.Theserverapplicationcaneitheracceptorrejecttheconnection.Iftheserveracceptstheconnection,adialogcanbeginwithbetweentheclientandtheserver.Oncetheclientisdonewithwhateveritneedstodoitcanclosetheconnectionwiththeserver.Connectionsareexpensiveinthesensethatserversallowfiniteconnectionstooccur.Duringthetimeclienthasanactiveconnectionitcansendthedatatotheserverand/orreceivethedata.Thecomplexitybeginshere.Wheneitherside(clientorserver)sendsdatatheothersideissupposedtoreadthedata.Buthowwilltheothersideknowwhendatahasarrived.Therearetwooptionseithertheapplicationneedstopollforthedataatregularintervalsorthereneedstobesomesortofmechanismthatwouldenableapplicationtogetnotificationsandapplicationcanreadthedataatthattime.Well,afterallWindowsisaneventdrivensystemandthenotificationsystemseemsanobviousandbestchoiceanditinfactis.AsIsaidthetwoapplicationsthatneedtocommunicatewitheachotherneedtomakeaconnectionfirst.Inorderforthetwoapplicationtomakeconnectionsthetwoapplicationsneedtoidentifyeachother(oreachotherscomputer).ComputersonnetworkhaveauniqueidentifiercalledI.P.addresswhichisrepresentedindotnotationlike10.20.120.127etc.Letsseehowallthisworksin.NET.Beforewegoanyfurther,downloadthesourcecodeattachedwiththisarticle.Extractthezipfiletoafoldersayc:Tempyouwillseefollowingtwofolders:ServerClientIntheServerfoldertherewillbeoneEXE.AndintheclienttherewillbethesourcecodeinC#thatisourclient.TherewillbeonefilecalledSocketClient.slnwhichthesolutionfile.IfyoudoubleclickthatyourVS.NETwillbelaunchedandyouwillseetheprojectSocketClientProjinthesolution.UnderthisprojectyouwillhaveSocketClientForm.csfile.Nowbuildthecode(bypressingCtrlShiftB)andrunthecodeyouwillseethefollowingdialogbox:AsyoucanseethedialogboxhasafieldforHostIPaddress(whichistheIPaddressofthemachineonwhichyouwillruntheServerApplication,locatedunderServerfolder).AlsothereisafieldwhereyoucanspecifyportnumberatwhichtheServerislistening.TheserverappIhaveprovidedherelistensatport8221.SoIhavespecifiedporttobe8221.Afterspecifyingtheseparametersweneedtoconnecttotheserver.SopressingConnectwillconnecttotheserverandtoclosetheconnectionpressClose.TosendsomedatatotheservertypesomedatainthefieldnearthebuttonnameTxandifyoupressRxtheapplicationwillblockunlessthereissomedatatoread.Withthisinfoletsnowtrytocheckthecodebehindthis:Socketprogrammingin.NETismadepossiblebytheSocketclasspresentinsidetheSystem.Net.Socketsnamespace.ThisSocketclasshasseveralmethodandpropertiesandaconstructor.Thefirststepistocreateanobjectofthisclass.Sincethereisonlyoneconstructorwehavenochoicebuttouseit.Hereishowtocreatethesocket:m_socListener = newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);Thefirstparameteristheaddressfamilywhichwewilluse,inthiscase,interNetwork(whichisIPversion4)otheroptionsincludeBanyanNetBios,AppleTalketc.(AddressFamilyisanenumdefinedinSocketsnamespace).Nextweneedtospecifysockettype:andwewouldusereliabletwowayconnectionbasedsockets(stream)insteadofunreliableConnectionlesssocket ...

Tài liệu được xem nhiều: