Take the TCPCLIENT connection as an example, first obtain the data stream sent back by the server. NetworkStream StreamAcount = TcpClient.getStream (); When we send requests for the SMTP server, such as connectivity, transfer username, password, the server returns the answering data stream. We must read the server to the server, this step I have experienced 3 changes. The initial program is written in accordance with the example of "Visaul C # .NET Network Core Programming": Private String ReadFromnetStream (Ref NetworkStream NetStream) {byte [] by = new byte [512]; netstream.read (by , 0, By.Length; string read = system.text.Encoding.ascii.getstring (by); return read;} This method is actually to turn all read data streams into string form, but actual network transmission In fact, the SMTP server is actually not necessarily all valid commands, and the command is ended in
IF (resultdata.endswith (streak) {brenk;}
IF (ncount == 0) {throw new system.exception ("timeout");}}} catch (system.exception se) {throw se; messagebox.show (se.tostring ()); return ";} return As soon as it can be intercepted, it can be intercepted. But this is still not correct, because the SMTP server will send back things to some welcome information in some cases, and they are also ended with
String results = ""; try {while (true) {string linedata = readlinestr (NetStream); if (null! = Linedata && linedata.length> 4) {if (linedata.substring (3, 1) .compareto ("" )! = 0) {resultData = linedata "/ r / n"; Continue;} else {resultData = linedata; break;}} else {resultData = linedata; break;}}} catch (Exception E)}} Throw e;} Return ResultData;
/ ** * * Return information of the translational reading server * * / public byte [] readline (networkStream m_Strmsource) {arraylist linebuf = new arraylist (); byte prevbyte = 0;
INT currbyteint = m_Strmsource.Readbyte (); while (currbyteint> -1) {linebuf.add ((byte) currbyteint;
IF ((prevbyte == (byte) '/ r' && (Byte) currbyteint == (byte) '/ n')) {byte [] retval = new byte [linebuf.count-2]; linebuf.copyto (0 , RetVal, 0, Linebuf.count-2); Return RetVal;} prevbyte = (byte) currbyteint
Currbyteint = m_Strmsource.Readbyte ();
IF (linebuf.count> 0) {byte [] retval = new byte [linebuf.count]; linebuf.copyto (0, retval, 0, linebuf.count);
Return RetVal;
Return NULL;
/ ** * * convert the return information of the server into string * * / public string readlinestr (NetWorkStream MyStream) {byte [] b = readline (MyStream); if (null == b) {Return Null;} Else {Return System.Text.Encoding.ascii.getstring (b);}} In this way, we can read those starting with 3-position answering code, add a space, then some send back the data stream, the end is back The correct command format of the car plus.