. NET network communication programming [using socket for transmission of strings, files, and serialized objects]-using encapsulated Network Service 2

Source: Internet
Author: User
Tags sendfile

Use SOCKET directly as the client and UDP

ClientCode:

 DEMO code  Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. net. Sockets; Using Tcplabcommon; Using System. net; Namespace Udplabclient1 { Class Program {Static   Void Main ( String [] ARGs) {Socket socket = New Socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP); endpoint = New Ipendpoint (IPaddress. parse (" 127.0.0.1 ") 6000 ); // Socket. BIND (endpoint ); Netpacketservice packetsvr = New Netpacketudpservice (socket, endpoint ); For ( Int I = 0; I <10; I ++) {packetsvr. sendobject ( New Book {Title =" Java programming ideology _ "+ I, price = I + 1}); packetsvr. sendtext (" Jiang _ "+ I);} packetsvr. sendfile (@" E: \ simple example reference .txt "); Console. Readline ();}}}

 

 

Server code:

 DEMO code  Using System; Using System. Collections. Generic; Using System. LINQ;Using System. text; Using System. net. Sockets; Using System. net; Using Tcplabcommon; Using System. IO; Namespace Udplabsvr1 { Class Program { Static   Void Main ( String [] ARGs) {socket serversocket = New Socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP); ipendpoint endpoint = New Ipendpoint (IPaddress. parse (" 127.0.0.1 "), 6000); serversocket. BIND (endpoint); netpacketservice packetsvr = New Netpacketudpservice (serversocket, endpoint ); Do {Netpacket packet = packetsvr. receivepacket (); Switch (Packet. packethead. ptype ){ Case Packettype. String: console. writeline (convert. tostring (packet. Data )); Break ; Case Packettype. Binary: netfile F = packetsvr. parsefile (byte []) packet. data); console. writeline (" Successfully received file: "+ F. filename); filestream FS = New Filestream (F. filename, filemode. Create); FS. Write (F. Content, 0, F. content. Length); FS. Flush (); FS. Close (); Break ; Case Packettype. Complex: Book = packet. Data As Book; console. writeline (book. Title +" \ T "+ Book. Price ); Break ; Default : Break ;}} While ( True );}}}

 

// Certificate //----------------------------------------------------------------------------------------------------------------------------------------

Use udpclient as the client and server to simplify the Code:

Client code:

 DEMO code  Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. net. Sockets; Using System. net; Using Tcplabcommon; Namespace Udplabclient2 { Class Program {Static   Void Main ( String [] ARGs ){ // Socket socket = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP ); Ipendpoint endpoint = New Ipendpoint (IPaddress. parse (" 127.0.0.1 ") 6000 ); // Socket. BIND (endpoint ); Udpclient = New Udpclient (); netpacketservice packetsvr = New Netpacketudpservice (udpclient. Client, endpoint );For ( Int I = 0; I <10; I ++) {packetsvr. sendobject ( New Book {Title =" Java programming ideology _ "+ I, price = I + 1}); packetsvr. sendtext (" Jiang _ "+ I);} packetsvr. sendfile (@" E: \ simple example reference .txt "); Console. Readline ();}}}

 

Server code:

 DEMO code  Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. net. Sockets; Using System. net; Using Tcplabcommon; Using System. IO; Namespace Udplabsvr2 { Class Program { Static   Void Main ( String [] ARGs ){// Socket serversocket = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP ); Ipendpoint endpoint = New Ipendpoint (IPaddress. parse (" 127.0.0.1 ") 6000 ); // Serversocket. BIND (endpoint ); Udpclient = New Udpclient (endpoint); netpacketservice packetsvr = New Netpacketudpservice (udpclient. Client, endpoint ); Do {Netpacket packet = packetsvr. receivepacket (); Switch (Packet. packethead. ptype ){ Case Packettype. String: console. writeline (convert. tostring (packet. Data )); Break ; Case Packettype. Binary: netfile F = packetsvr. parsefile (byte []) packet. data); console. writeline (" Successfully received file: "+ F. filename); filestream FS = New Filestream (F. filename, filemode. Create); FS. Write (F. Content, 0, F. content. Length); FS. Flush (); FS. Close ();Break ; Case Packettype. Complex: Book = packet. Data As Book; console. writeline (book. Title +" \ T "+ Book. Price ); Break ; Default : Break ;}} While ( True );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.