You can use Sendand Receiveoverload on Socket. An asynchronous version also exists using BeginSomethingand methods EndSomething.
You send raw bytes, so you will need to decide which protocol is simple. To send some text, select the encoding (I would use UTF-8) and get the raw bytes from Encoding.GetBytes.
Example:
Byte[] bytes = Encoding.UTF8.GetBytes("Royale With Cheese");
UTF8 Encoding, .
/ :
int sent = connection.Send(bytes);
:
Byte[] bytes = new Bytes[100];
int received = connection.Receive(bytes);
, . , , SocketException. , , , sent received. Send Receive. , ( , , , ?)
- , :
var bytes = Encoding.UTF8.GetBytes("Royale With Cheese");
int count = 0;
while (count < bytes.Length)
{
count += connection.Send(
bytes,
count,
bytes.Length - count,
SocketFlags.None)
}
Send Receive , , . , - (Socket.SendTimeout Socket.ReceiveTimeout.) 0, , .
? .
int count = 0;
var bytes = new Byte[100];
do
{
count += connection.Receive(
bytes,
count,
bytes.Length - count,
SocketFlags.None);
} while (count < bytes.Length);
. ... , 100? , , , . , 100? 100 .
. , , :
var sunIsBurning = true;
while (sunIsBurning) {
var bytes = new Byte[16];
int received = socket.Receive(bytes);
if (received > 0)
Console.Out.WriteLine("Got {0} bytes. START:{1}END;", received, Encoding.UTF8.GetString(bytes));
}
: " 16 , sunIsBurning, UTF-8". , , , UTF-8 (, ). , .
15 , , 15 . :)
; .
" " , , reset .