Hello, I just want to know how every single line of code actually does.
Codes :
1 private String SocketRead(Socket socket) {
2 StringBuilder result = new StringBuilder();
3 byte []buffer = new byte[1];
4 while( socket.Receive(buffer)>0 ) {
5 char ch = (char)buffer[0];
6 if( ch=='\n') break;
7 if( ch!='\r') result.Append(ch);
8 }
9 return result.ToString();
10 }
I don't understand line 3,4 and 5. What does byte[1] means ?
Loading
Jaganathan BantheswaranPosted Nov 13, 2013, 3:41 AM
The function reads the bytes from socket until it reads the new line char [\n].
byte []buffer = new byte[1]; // Allocates new byte array of size 2.
while( socket.Receive(buffer)>0 ) { // Checks received bytes are more than 0, if no more bytes are there, then it will exit the while loop
char ch = (char)buffer[0]; // Getting the first byte received.
Jaganathan BantheswaranPosted Nov 13, 2013, 6:55 AM
\r = CR (Carriage Return) -- Used as a new line character in Unix
\n = LF (Line Feed) -- Used as a new line character in Mac OS
\r\n = CR + LF -- Used as a new line character in Windows
kennedy chozoPosted Nov 13, 2013, 6:14 AM