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 BantheswaranRank #170 · 14.1K reputationPosted Nov 12, 2013, 10:41 PM EST
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.
kennedy chozo7 reputationPosted Nov 13, 2013, 1:14 AM EST
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Nov 13, 2013, 1:55 AM EST
\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