?? Summary? This article is stepped on how to program? Microsoft? Windows? Sockets? (Winsock) interface and network connection programming in Microsoft? Visual® Basic? .NET? This example uses a simple transmission control protocol named? TesttcpClient? (TCP)? Client application and a simple TCP® server-side (listener) application called? Testtcpserver? Use the "Windows? Sockets? Interface communicate with each other. First, create? TCP? Client application? 1. Start? Microsoft? Visual? Studio? .NET. 2. On the File menu, click New, then click Project. 3. Under Project Type, click Visual? Basic? Project. 4. Under Templates, click Console Application. Name the item? TesttcpClient, and then click OK. By default, the system creates? Module1.vb.
5. Use the following code to replace the code: imports? System.net.Socketsimports? System.text class? Ctesttcpclient ???? Shared? Sub? Main () ???????? Dim TCPCLIENT? AS? new? system.net.sockets.tcpclient ???????? '"localhost"? String? is? used ?1? The? client? and? on? listener? Are? on? the ???????? 'IF? the? listener? is? listening? at? a? computer? That? Is? Different? from? The? client,? provide? the? Host? name? name ? of? The? Computer ???????? 'where? the? listener? is? listening. ???????? Tcpclient.connect ("localhost",? 8000) ?????? ?? DIM? NETWORKSTREAM? AS? NETWORKSTREAM? =? Tcpclient.getStream () ???????? f? NetWorkstream.canwrite? And? NetWorkstream.canread? Then ???????????? '? Do? A? Simple? Write. ??????????? DIM? SendBytes? As? [Byte] ()? =? Encoding.ascii.getbytes ("is? Anybody? Listening .. ") ???????????? NetWorkStream.write (sendbytes,? 0,? Sendbytes.length) ???????????? '? read? THE? NETWORKSTREAM? INTO ?? byte? buffer. ???????????? DIM? BYTES (TCPCLIENT.RECEIVEBUFFERSIZE)? As? BYTE ???????????? NetWorkstream.read (bytes,? 0,? Cint (tcpclient.receivebuffers) ???????????? '? OUTPU T? The? Data? Received? from? the? host? ???????????? Dim? Returndata? as? string? =? encoding.ascii.getstring (bytes) ???????????? console.writeline ("TCP? Server? Returned:?" ? Returndata) ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??? f? Not? NetWorkstream.canread? Then ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????? tclient.close () ?????????????????????? f? Not? NetWorkstream.canwrite? Then ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????? tclient.close () ???????????????????????????? ??? End? IF ???????? End? IF ????????
? Pause? To? Let? The? User? View ????????? console.readline () ???? End? Sublend? Class ???? This code will be created A new instance of the "TCPClient" class calls the "Connect" method and use the "GetStream" method of the "NetWorkStream" to access the underlying data stream. The system will convert the message to a byte array, send it to the data stream, and then read the data stream from the "TCP® server (listener) application response. ?? Create? TCP? Server-side (listener) application 1. Start? Microsoft? Visual? Studio? .NET. 2. On the File menu, click New, then click Project. 3. Under Project Type, click Visual? Basic? Project. 4. Under Templates, click Console Application. Name the item? TestTCPServer, and then click OK. By default, the system creates? Moudle1.vb. ?? The following code replacement Module1.vb code: Imports System.Net.SocketsImports System.netImports System.TextImports System.Net.DnsPermissionAttributeImports System.Security.Permissions' DnsPermissionAttribute specifies permission???????? TO? REQUEST? INFORMATION? From? domain? name? servers.? Class? Ctesttcpserver
???? shared? Sub? Main ()
???????? 'listening? Must? Be? On? The? Same? Port? That? The? Client? Is? Connection? On.
???????? const? Portnumber? As? Integer? =? 8000
???????? '"localhost"? String? Is? Used ?hen? The? Client? And? The ???????????????' IF? The? Listener? Is? Listening? At? A? Computer? That? Is? Different? From? The? Client,? Then? Provide? The? Host? Name? Of? The? Computer ?????? ?? 'WHERE? THE? Listener? is? listening. ???????? DIM? TCPLISTENER? AS? NEW? TCPLISTENER (CType ("localhost"). AddressList (0), NPADDRESS) , portnumber ???????? 'Comment? the? previous? line? and? uncomment? The? FOLLOWING? LINE? IF? YOU? Are? using? Visual? Basic? .NET? (2003). ???????? 'DIM? TCPLISTENER? AS? New? Tcplistener (portnumber) ???????? tcplistener.start () ???????? console.writeline ("TCP? Server ? Is? Up? and? waiting ??????? Try ???????????? 'accept? the? pending? client? Connection? and? Return? a? tcpclient? for? Communication. ????????????? DIM? TCPCLIENT? AS? TCPCPCLIENT? =? tcplistener.accepttclient () ???????? ???? console.writeline ("Connection? ACCEPTED.") ???????????? '? Get? ???????????? DIM NetWorkStream? as? networkStream? =? tcpclient.getStream () ???????????? '? read ? The? data? stream? INTO? A? BYTE? Array. ???????????? DIM? BYTES (TCPCLIENT.RECEIVEBUFFERSIZE)? AS? BYTE ??????????? NetWorkStream.read (Bytes,? 0, CINT (TCPCLIENT.RECEIVEBUFFERSIZE)) ???????????? '? Return? The? the? client? TO? THE? CLIENT? TO? THE? Console. ???????????? DIM? ClientData? as? string? =? Encoding.ascii.getstring (bytes) ???????????? console.writeline ((" CLIENT? SENT:? "? ? Caientdata)) ???????????? DIM? Responsestring? As? String? =?" SuccessFully? Connection? To? TCP? Server. "???? ???????? DIM? SendBytes? As? [Byte] ()? =? Encoding.ascii.getbytes (responseSString) ???????????? NetWorkStream.write (sendbytes,? 0 ,? SendBytes.length) ???????????? console.writeline (("