Use C # to perform SMTP protocol client development experience - Read Server Acknowledge

xiaoxiao2021-03-06  89

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 . Therefore, there is a problem in such a way of reading. After modifying code is as follows: private string ReadFromNetStream (ref NetworkStream NetStream, string strEndFlag) {string ResultData = ""; byte [] ubBuff = new byte [1024]; try {while (true) {int nCount = NetStream.Read (ubBuff , 0, ubbuff.length; if (ncount> 0) {resultData = encoding.ascii.getstring (Ubbuff, 0, ncount);

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 (Enter Ranking), and we have used the previous program very It may not be the correct command we actually want to get. So I only re-modify the program as follows: / ** * * Read the server's return information * * / public string readfromnetStream (Ref networkStream NetStream) {if (netstream == null) Return "

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.

转载请注明原文地址:https://www.9cbs.com/read-108118.html

New Post(0)