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
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 ...
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 liên quan:
-
Giáo trình Lý thuyết hệ điều hành: Phần 1 - Nguyễn Kim Tuấn
110 trang 468 0 0 -
52 trang 441 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
74 trang 310 0 0
-
96 trang 307 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 299 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 293 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 291 1 0 -
175 trang 283 0 0
-
Giáo trình Nguyên lý các hệ điều hành: Phần 2
88 trang 283 0 0