Send command with 4 Byte ASCII header. This code needs to allow a command sent w
ID: 3676104 • Letter: S
Question
Send command with 4 Byte ASCII header. This code needs to allow a command sent with a 4 byte ASCII header:
The IP is just an example and not real IP:
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("198.2.2.2", 8000);
// use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
byte[] ba = ASCIIEncoding.ASCII.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[4096];
int k = stm.Read(bb, 0, bb.Length);
for (int i = -1; i < k; i++)
Console.Write(Convert.ToChar(bb[i])+4);
tcpclnt.Close();
}
Explanation / Answer
This below code is will send the data to tcp client and server will receive the data and will prcess..
Client code:
This below code will send the 4 bytes of data to server using tcpClient method.
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace NetMan.TCPIPMessenger.Client
{
class Program
{
private static byte[] MessageToByteArray(string message, Encoding encoding)
{
var byteCount = encoding.GetByteCount(message);
if (byteCount > byte.MaxValue)
throw new ArgumentException("Message size is greater than 255 bytes in the provided encoding");
var byteArray = new byte[byteCount + 1];
byteArray[0] = (byte)byteCount;
encoding.GetBytes(message, 0, message.Length, byteArray, 1);
return byteArray;
}
public static void Main(string[] args)
{
const string message = "Hello, World!";
var byteArray = MessageToByteArray(message, Encoding.ASCII);
using (var tcpClient = new TcpClient())
{
tcpClient.Connect("127.0.0.1", 31337);
using (var networkStream = tcpClient.GetStream())
using (var bufferedStream = new BufferedStream(networkStream))
{
// Send three exactly the same messages.
bufferedStream.Write(byteArray, 0, byteArray.Length);
bufferedStream.Write(byteArray, 0, byteArray.Length);
bufferedStream.Write(byteArray, 0, byteArray.Length);
}
}
}
}
}
Server Code :
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace NetMan.TCPIPMessenger.Server
{
class Program
{
public static void Main(string[] args)
{
// Create a TCP/IP listener.
var localAddress = IPAddress.Parse("127.0.0.1");
var tcpListener = new TcpListener(localAddress, 31337);
// Start listening for connections.
tcpListener.Start();
while (true)
{
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
var tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Client has been accepted!");
// An incoming connection needs to be processed.
Thread thread = new Thread(() => ClientSession(tcpClient))
{
IsBackground = true
};
thread.Start();
Console.WriteLine("Client session thread has been started!");
}
}
private static bool TryReadExact(Stream stream, byte[] buffer, int offset, int count)
{
int bytesRead;
while (count > 0 && ((bytesRead = stream.Read(buffer, offset, count)) > 0))
{
offset += bytesRead;
count -= bytesRead;
}
return count == 0;
}
private static void ClientSession(TcpClient tcpClient)
{
const int totalByteBuffer = 4096;
byte[] buffer = new byte[256];
using (var networkStream = tcpClient.GetStream())
using (var bufferedStream = new BufferedStream(networkStream, totalByteBuffer))
while (true)
{
// Receive header - byte length.
if (!TryReadExact(bufferedStream, buffer, 0, 1))
{
break;
}
byte messageLen = buffer[0];
// Receive the ASCII bytes.
if (!TryReadExact(bufferedStream, buffer, 1, messageLen))
{
break;
}
var message = Encoding.ASCII.GetString(buffer, 1, messageLen);
Console.WriteLine("Message received: {0}", message);
}
Console.WriteLine("Client session completed!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.