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 );}}}