Simple async await tcp server design - c#

I'm designing an asynchronous TCP server using async-await approach making use of event-driven .NET environment with the following features:
Request and response functionality.
Server side initiated events.
Does this server design is correct in terms of async-await approach?
Code below covers only basic functionality to judge whether I'm missing something or not.
The server uses COM Type library, which is where PRMasterAutomation class comes from.
public class AsyncServer {
private const int BufferSize = 4096;
private static bool ServerRunning = true;
private List<TcpClient> clients = new List<TcpClient>();
private PRMasterAutomation automation;
public AsyncServer()
{
InitializeComponent();
automation = new PRMasterAutomation();
automation.OnMonitoringEvent += automation_OnMonitoringEvent;
if (!automation.Login("ADMIN", ""))
return;
if (automation.GetPRMasterState() == PRMasterStateEnum.StateIdle)
automation.StartMonitoring();
var tcpServer = new TcpListener(IPAddress.Any, 9001);
tcpServer.Start();
ListenForClients(tcpServer);
}
private async void ListenForClients(TcpListener tcpServer)
{
while (ServerRunning)
{
TcpClient tcpClient = await tcpServer.AcceptTcpClientAsync();
clients.Add(tcpClient);
ProcessClient(tcpClient);
}
}
private async void ProcessClient(TcpClient tcpClient)
{
var stream = tcpClient.GetStream();
var buffer = new byte[BufferSize];
int amountRead;
string xml;
while (ServerRunning)
{
if ((amountRead = await stream.ReadAsync(buffer, 0, BufferSize)) > 0)
{
var message = Encoding.ASCII.GetString(buffer, 0, amountRead);
xml = "";
if (message.Equals("get_users"))
{
xml = automation.GetUsersXml();
}
else if (message.Equals("get_events"))
{
xml = automation.GetEventsHistoryXml("");
}
if (xml.Length > 0)
{
xml += "\r\n";
await stream.WriteAsync(Encoding.ASCII.GetBytes(xml), 0, xml.Length);
}
}
}
}
async void automation_OnMonitoringEvent(DateTime date, DateTime time, int networkID, int readerID, int userID, int groupID, int eventCode, int zoneID, int TandAID, string strEvent, string strAccessPoint, string strUserSource, string strGroup, string strNetwork, string strZone, string strTandAMode)
{
foreach (var c in clients)
{
if (c != null && c.Connected)
{
var stream = c.GetStream();
StringBuilder sb = new StringBuilder();
sb.Append(date.ToString() + ";" + time.ToString() + ";" + networkID.ToString() + ";" + userID.ToString() + "\r\n");
await stream.WriteAsync(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
}
}
}
}

Does this server design is correct in terms of async-await approach?
Yes, but not in terms of sockets. ReadAsync will return any number of bytes between 1 and infinite, it will not return messages.
Stream.ReadAsync(): The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.
You have to create a buffer and keep reading and adding to that buffer until you know you have received a whole message. This means checking for \r\n in your case. If you encounter such a delimiter, you can extract the message from the buffer and start receiving a new message.

Related

C# multi threaded or multi process?

I have a small question about multithreaded connections between one (Server) to many (Clients) relationship.
My question is:
Is my script multithreaded? Or Multi process? I have received some feedback stating that it is multiprocess?
Would each client receive their stream independently?
I'd just like to know if this is the correct approach to this.
Here you can see I start to listen to client connections and create a thread for each client connection and I pass the client :
private void StartListen()
{
//Creating a TCP Connection and listening to the port
tcpListener = new TcpListener(System.Net.IPAddress.Any, 6666);
tcpListener.Start();
toolStripStatusLabel1.Text = "Listening on port 6666 ...";
int counter = 0;
while (true)
{
try
{
client = tcpListener.AcceptTcpClient();
clientList.Add(client);
IPEndPoint ipend = (IPEndPoint)client.Client.RemoteEndPoint;
//Updating status of connection
toolStripStatusLabel1.Text = "Connected from " + IPAddress.Parse(ipend.Address.ToString());
th_inPutStream = new Thread(delegate () { inPutStream(client, counter); });
th_inPutStream.Start();
counter++;
}
catch (Exception err)
{
Cleanup_dep();
}
}
}
This is where I handle the client input stream where i pre-process the client input stream:
private void inPutStream(TcpClient client, int clientID)
{
try
{
while (true)
{
NetworkStream networkstream = client.GetStream();
if (networkstream.DataAvailable == true)
{
int messageID = 0;
int messageSize = 0;
int bufferSize = 100;
//First retrieve size of header
byte[] fileSizeBytes = new byte[4];
int bytes = networkstream.Read(fileSizeBytes, 0, 4);
int dataLength = BitConverter.ToInt32(fileSizeBytes, 0);
//Read stream containing header with message details
byte[] header = new byte[dataLength];
int headerbyte = networkstream.Read(header, 0, dataLength);
string headerString = Encoding.ASCII.GetString(header, 0, headerbyte);
// Process Message information ie Message ID & Message Size
string[] headerSplit = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string s in headerSplit)
{
if (s.Contains(":"))
{
headers.Add(s.Substring(0, s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
}
}
//Fetch Message ID & Size
messageSize = Convert.ToInt32(headers["len"]);
messageID = Convert.ToInt32(headers["MsgId"]);
//Filter actions by Message ID
if (messageID == 1)//Machine info
{
string machineinfo = receiveMessage(messageSize, bufferSize, networkstream);
clientNames.Add(machineinfo.Split(new char[] { '\r', '\n' })[0]);
updateClientListUI();
}
if (messageID == 2)//CMD Commands
{
cmdres = receiveMessage(messageSize, bufferSize, networkstream);
activeForm.updateText(cmdres);
}
}
}
}
catch (Exception err)
{
Cleanup_dep();
}
}
Since you are using Thread and not Process I don't see any indication as to why this would be a multi process approach.
Without knowing how TcpClient.GetStream is exactly implemented, I would be suprised if the call on one client would have anything to do with the same call on another client. Other than obvious, unavoidable, coherences like sharing the same bandwidth if the connections are accepted on the same nic of course.
Depends on what you are trying to do. If this is for work, I would question if there isn't an existing solution you could use. If this is for education there are a few improvements that come to mind:
use async and Task instead of Thread is usually recommended
using var networkstream = client.GetStream(); will make sure the stream is disposed of correctly

C# good practice waiting for TCP response

While making a c# application for remote controlling cisco routers using TCP, I got the problem of waiting for a response from the router.
For the application I have to connect to a Cisco router using a TCP connection. After the connection has been made a networkstream will push my command to the Cisco router. To let the router process the command, I am using Thread.Sleep. This is not the best solution.
Here is the complete code to get a idea what my program is doing.
string IPAddress = "192.168.1.1";
string message = "show running-config"; // command to run
int bytes;
string response = "";
byte[] responseInBytes = new byte[4096];
var client = new TcpClient();
client.ConnectAsync(IPAddress, 23).Wait(TimeSpan.FromSeconds(2));
if (client.Connected == true)
{
client.ReceiveTimeout = 3;
client.SendTimeout = 3;
byte[] messageInBytes = Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
Console.WriteLine();
stream.Write(messageInBytes, 0, messageInBytes.Count()); //send data to router
Thread.Sleep(50); // temporary way to let the router fill his tcp response
bytes = stream.Read(responseInBytes, 0, responseInBytes.Length);
response = Encoding.ASCII.GetString(responseInBytes, 0, bytes);
return response; //whole command output
}
return null;
What is a good and reliable way to get the full response.
Thanks for any help or command.
More info:
The networksteam is always filled with something, most of the time it is filled with the cisco IOS login page. The biggest problem is to determine when the router is done filling up the response.
The response I most of the time get:
"??\u0001??\u0003??\u0018??\u001f\r\n\r\nUser Access Verification\r\n\r\nUsername: "
The return data will be diffent every time because it will be a result of a cisco command. This can vary from a short string to a very long string.
mrmathijs95 -
When reading from NetworkStream with Stream.Read it not 100% sure that you will read all expected data. Stream.Read can return when only few packet arrived and not waiting for others.
To be sure that you get all data use BinaryReader for reading.
BinaryReader.Read method will block current thread until all expected data arrived
private string GetResponse(string message)
{
const int RESPONSE_LENGTH = 4096;
byte[] messageInBytes = Encoding.ASCII.GetBytes(message);
bool leaveStreamOpen = true;
using(var writer = new BinaryWriter(client.GetStream()))
{
writer.Write(messageInBytes);
}
using(var reader = New BinaryReader(client.GetStream()))
{
byte[] bytes = reader.Read(RESPONSE_LENGTH );
return Encoding.ASCII.GetString(bytes);
}
}
Don't use Thread.Sleep. I would async/await the entire thing, given that you don't always know what the data is based on your recent edit. This is how I would do it (untested):
public class Program
{
// call Foo write with "show running-config"
}
public class Foo
{
private TcpClient _client;
private ConcurrentQueue<string> _responses;
private Task _continualRead;
private CancellationTokenSource _readCancellation;
public Foo()
{
this._responses = new ConcurrentQueue<string>();
this._readCancellation = new CancellationTokenSource();
this._continualRead = Task.Factory.StartNew(this.ContinualReadOperation, this._readCancellation.Token, this._readCancellation.Token);
}
public async Task<bool> Connect(string ip)
{
this._client = new TcpClient
{
ReceiveTimeout = 3, // probably shouldn't be 3ms.
SendTimeout = 3 // ^
};
int timeout = 1000;
return await this.AwaitTimeoutTask(this._client.ConnectAsync(ip, 23), timeout);
}
public async void StreamWrite(string message)
{
var messageBytes = Encoding.ASCII.GetBytes(message);
var stream = this._client.GetStream();
if (await this.AwaitTimeoutTask(stream.WriteAsync(messageBytes, 0, messageBytes.Length), 1000))
{
//write success
}
else
{
//write failure.
}
}
public async void ContinualReadOperation(object state)
{
var token = (CancellationToken)state;
var stream = this._client.GetStream();
var byteBuffer = new byte[4096];
while (!token.IsCancellationRequested)
{
int bytesLastRead = 0;
if (stream.DataAvailable)
{
bytesLastRead = await stream.ReadAsync(byteBuffer, 0, byteBuffer.Length, token);
}
if (bytesLastRead > 0)
{
var response = Encoding.ASCII.GetString(byteBuffer, 0, bytesLastRead);
this._responses.Enqueue(response);
}
}
}
private async Task<bool> AwaitTimeoutTask(Task task, int timeout)
{
return await Task.WhenAny(task, Task.Delay(timeout)) == task;
}
public void GetResponses()
{
//Do a TryDequeue etc... on this._responses.
}
}
I didn't expose the read cancellation publicly, but you could add this method to cancel the read operation:
public void Cancel()
{
this._readCancellation.Cancel();
}
And then dispose of your client and all that fun stuff.
Lastly, because you said there's always data available on the stream, where you're doing the read you may have to do some logic on the number of bytes last read to offset yourself within the stream if the data doesn't clear. You'll know if the responses you're getting is always the same.
This is the working code for me.
It uses the solution of Fabio,
combined with a while loop to check every X miliseconds if the response has changed.
client.ReceiveTimeout = 3;
client.SendTimeout = 3;
byte[] messageInBytes = Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
Console.WriteLine();
using (var writer = new BinaryWriter(client.GetStream(),Encoding.ASCII,true))
{
writer.Write(messageInBytes);
}
using (var reader = new BinaryReader(client.GetStream(),Encoding.ASCII, true))
{
while (itIsTheEnd == false)
{
bytes = reader.Read(responseInBytes, 0, responseInBytes.Count());
if (lastBytesArray == responseInBytes)
{
itIsTheEnd = true;
}
lastBytesArray = responseInBytes;
Thread.Sleep(15);
}
}
response = Encoding.ASCII.GetString(responseInBytes);
Thanks for everyone who suggested a solution.
And thanks to Fabio for the given solution.

Using WPF to Interact with Indicators in MultiCharts .Net (Trading Program)

I am trying to create a WPF form using Visual Studio C# that will interact with indicators I create for my trading charts in MultiCharts .Net
I've added a WPF Project to the solution and added the namespace for the indicators. However, I can not figure out how I can manipulate inputs for the indicator object. Any help from someone who works with this program would be much appreciated.
You have to create a local network connection to make this work--I used sockets, but anything in a similar way will support the same goal:
Make a new MC indicator like so:
public class tests_OwnApp : IndicatorObject {
public tests_OwnApp(object _ctx):base(_ctx)
{
PortNumber = 5000;
}
[Input]
public int PortNumber { get; set; }
public static string Symbol;
const string ServerIP = "127.0.0.1";
IPAddress localAdd;
TcpListener listener;
TcpClient client;
NetworkStream nwStrm;
private IPlotObject plot1;
protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
localAdd = IPAddress.Parse(ServerIP);
client = null;
}
protected override void StartCalc() {
// assign inputs
// establish connection
if (listener == null)
{
listener = new TcpListener(localAdd, PortNumber);
}
listener.Stop();
Thread.Sleep(100);
listener.Start();
}
protected override void CalcBar()
{
// indicator logic
if (Bars.LastBarTime <= Bars.Time[0] + new TimeSpan(2,0,0))
{
Symbol = Bars.Info.Name;
if (client == null || !client.Connected)
client = listener.AcceptTcpClient();
// create network stream
nwStrm = client.GetStream();
// send symbol to app:
Output.WriteLine("sending " + Symbol + " to application");
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(Symbol);
string closingPrice = Convert.ToString(Bars.Close[0]);
string totalMsg = "Sym," + Symbol +
"\nClose[0]," + closingPrice + "\nClose[1]," + Bars.Close[1].ToString();
byte[] bytes2 = ASCIIEncoding.ASCII.GetBytes(totalMsg);
nwStrm.Write(bytes2, 0, bytes2.Length);
}
}
}
In your app (Console App example, extends to WPF):
static void Main(string[] args)
{
const int PortNum = 5000;
const string ServerIP = "127.0.0.1";
DateTime startTime = DateTime.Now;
TcpClient client = new TcpClient(ServerIP, PortNum);
NetworkStream nwStream = client.GetStream();
while (true)
{
if (client.ReceiveBufferSize != 0)
{
nwStream = client.GetStream();
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
string msg = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Console.WriteLine("received: " + msg);
if (DateTime.Now - new TimeSpan(0, 0, 30) > startTime)
{
break;
}
Thread.Sleep(100);
Console.Write("-ping-");
}
}
Console.ReadLine();
}
Run them both together and you'll find they connect and the info is received.
I used this example to get started, in case my example is not sufficient.
Be sure to include all the necessary namespaces:
using System.Net;
using System.Net.Sockets;
See the documentation I believe chapter three shows you how to work with indicators

C# reading/writing over SOCKET to JAVA and having some concurrency/socket issue

Rrom C#, reading/writing over SOCKET to JAVA and having some concurrency/socket issue.
I am trying to implement a server client application where the server is Java and the Client is C#. And they communicate over TCP/IP and exchanging some binary data between them.
Particularly I have a Packet class defined both in Java and C#. It has a header, key and value. Both Java and C# writes and reads the Packet to Socket in exact same way. This way I am able to send a request Packet from C#, process it on Java Server and send the response back as a Packet.
The original problem is way more complicated but I was able to boil it down to this "Simple" version.
I have implemented both server and client as described below. The code is also available at the bottom.
For me to state the problem you have to continue to read:)
Server(Java) side
On Server side I have a very dummy ServerSocket usage. It reads the incoming Packets and sends back almost the same Packet as response.
Client(C#) side
The client is a bit complicated. Client starts N(configurable) number of threads(I'll call them user threads). One In Thread and One Out Thread. All user threads create a Call object with dummy request Packet and unique id. Then adds the call into a local BlockingCollection.
The Out Thread continuously reads the local BlockingCollection and sends all request packets to server
The In Thread also continuously reads response Packets from server and matches them to the Call objects(remember the unique call id).
If there is no response for a particular Call object within 5 sec intervals the user thread will complain about it by printing into Console.
Also there is a timer with 10 sec interval that prints how many transactions have been executed per second.
If you reached so far, thank you:).
Now the problem:
The code below, which is the implementation of what I described above works fine with Mono on Mac. On Windows it also doesn't fail immediately with low number(<10) of user threads. As I increase the number of threads suddenly, somehow the response packets that client receives are getting corrupted. In terms of this application all user threads get stuck because the answer to their request is not received.
The question is why they are corrupted? As you see the threads that touche socket are In and Out threads. But somehow the amount of user threads affects the client and brakes it.
It looks like some concurrency or socket problem, but I could find it.
I have put the code for Server(Java) and Client(C#). They don't have any dependency just compiling and running the Main methods on both(first server) shows the problem.
I appreciate if you read so far.
Server Code
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
public class DummyServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9900);
System.out.println("Server started");
for(;;){
final Socket socket = server.accept();
System.out.println("Accepting a connection");
new Thread(new Runnable(){
public void run() {
try {
System.out.println("Thread started to handle the connection");
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
for(int i=0; ; i++){
Packet packet = new Packet();
packet.readFrom(dis);
packet.key = null;
packet.value = new byte[1000];
packet.writeTo(dos);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
public static class Packet {
byte[] key;
byte[] value;
long callId = -1;
private int valueHash = -1;
public void writeTo(DataOutputStream outputStream) throws IOException {
final ByteBuffer writeHeaderBuffer = ByteBuffer.allocate(1 << 10); // 1k
writeHeaderBuffer.clear();
writeHeaderBuffer.position(12);
writeHeaderBuffer.putLong(callId);
writeHeaderBuffer.putInt(valueHash);
int size = writeHeaderBuffer.position();
int headerSize = size - 12;
writeHeaderBuffer.position(0);
writeHeaderBuffer.putInt(headerSize);
writeHeaderBuffer.putInt((key == null) ? 0 : key.length);
writeHeaderBuffer.putInt((value == null) ? 0 : value.length);
outputStream.write(writeHeaderBuffer.array(), 0, size);
if (key != null)outputStream.write(key);
if (value != null)outputStream.write(value);
}
public void readFrom(DataInputStream dis) throws IOException {
final ByteBuffer readHeaderBuffer = ByteBuffer.allocate(1 << 10);
final int headerSize = dis.readInt();
int keySize = dis.readInt();
int valueSize = dis.readInt();
readHeaderBuffer.clear();
readHeaderBuffer.limit(headerSize);
dis.readFully(readHeaderBuffer.array(), 0, headerSize);
this.callId = readHeaderBuffer.getLong();
valueHash = readHeaderBuffer.getInt();
key = new byte[keySize];
dis.readFully(key);
value = new byte[valueSize];
dis.readFully(value);
}
}
}
C# Client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Collections.Concurrent;
using System.Threading;
namespace Client
{
public class Program
{
readonly ConcurrentDictionary<long, Call> calls = new ConcurrentDictionary<long, Call>();
readonly BlockingCollection<Call> outThreadQueue = new BlockingCollection<Call>(1000);
readonly TcpClient tcpClient = new TcpClient("localhost", 9900);
readonly private int THREAD_COUNT;
static int ops;
public static void Main(string[] args) {
new Program(args.Length > 0 ? int.Parse(args[0]) : 100).Start();
}
public Program(int threadCount) {
this.THREAD_COUNT = threadCount;
new Thread(new ThreadStart(this.InThreadRun)).Start();//start the InThread
new Thread(new ThreadStart(this.OutThreadRun)).Start();//start the OutThread
}
public void Start(){
for (int i = 0; i < THREAD_COUNT; i++)
new Thread(new ThreadStart(this.Call)).Start();
Console.WriteLine(THREAD_COUNT + " User Threads started to perform server call");
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.Stats);
aTimer.Enabled = true;
}
public void Stats(object source, System.Timers.ElapsedEventArgs e){
Console.WriteLine("Ops per second: " + Interlocked.Exchange(ref ops, 0) / 10);
}
public void Call() {
for (; ;){
Call call = new Call(new Packet());
call.request.key = new byte[10];
call.request.value = new byte[1000];
outThreadQueue.Add(call);
Packet result = null;
for (int i = 1;result==null ; i++){
result = call.getResult(5000);
if(result==null) Console.WriteLine("Call" + call.id + " didn't get answer within "+ 5000*i/1000 + " seconds");
}
Interlocked.Increment(ref ops);
}
}
public void InThreadRun(){
for (; ; ){
Packet packet = new Packet();
packet.Read(tcpClient.GetStream());
Call call;
if (calls.TryGetValue(packet.callId, out call))
call.inbQ.Add(packet);
else
Console.WriteLine("Unkown call result: " + packet.callId);
}
}
public void OutThreadRun() {
for (; ; ){
Call call = outThreadQueue.Take();
calls.TryAdd(call.id, call);
Packet packet = call.request;
if (packet != null) packet.write(tcpClient.GetStream());
}
}
}
public class Call
{
readonly public long id;
readonly public Packet request;
static long callIdGen = 0;
readonly public BlockingCollection<Packet> inbQ = new BlockingCollection<Packet>(1);
public Call(Packet request)
{
this.id = incrementCallId();
this.request = request;
this.request.callId = id;
}
public Packet getResult(int timeout)
{
Packet response = null;
inbQ.TryTake(out response, timeout);
return response;
}
private static long incrementCallId()
{
long initialValue, computedValue;
do
{
initialValue = callIdGen;
computedValue = initialValue + 1;
} while (initialValue != Interlocked.CompareExchange(ref callIdGen, computedValue, initialValue));
return computedValue;
}
}
public class Packet
{
public byte[] key;
public byte[] value;
public long callId = 0;
public void write(Stream stream)
{
MemoryStream header = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(header))
{
writer.Write(System.Net.IPAddress.HostToNetworkOrder((long)callId));
writer.Write(System.Net.IPAddress.HostToNetworkOrder((int)-1));
}
byte[] headerInBytes = header.ToArray();
MemoryStream body = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(body))
{
writer.Write(System.Net.IPAddress.HostToNetworkOrder(headerInBytes.Length));
writer.Write(System.Net.IPAddress.HostToNetworkOrder(key == null ? 0 : key.Length));
writer.Write(System.Net.IPAddress.HostToNetworkOrder(value == null ? 0 : value.Length));
writer.Write(headerInBytes);
if (key != null) writer.Write(key);
if (value != null) writer.Write(value);
byte[] packetInBytes = body.ToArray();
stream.Write(packetInBytes, 0, packetInBytes.Length);
}
}
public void Read(Stream stream)
{
BinaryReader reader = new BinaryReader(stream);
int headerSize = IPAddress.NetworkToHostOrder(reader.ReadInt32());
int keySize = IPAddress.NetworkToHostOrder(reader.ReadInt32());
int valueSize = IPAddress.NetworkToHostOrder(reader.ReadInt32());
this.callId = IPAddress.NetworkToHostOrder(reader.ReadInt64());
int valuePartitionHash = IPAddress.NetworkToHostOrder(reader.ReadInt32());
this.key = new byte[keySize];
this.value = new byte[valueSize];
if (keySize > 0) reader.Read(this.key, 0, keySize);
if (valueSize > 0) reader.Read(this.value, 0, valueSize);
}
}
}
This is a pretty common mistake: any Read call on a socket may not actually read as many bytes as you ask for, if they are not currently available. Read will return the number of bytes read by each call. If you expect to read n bytes of data, then you need to call read multiple times until the number of bytes read adds up to n.

Downloading web site using sockets

I'm trying to download source of the site using sockets. Currently i can download headers and after that i just terminate connection because i don't know how long should I receive data. This is the code:
private void HandleConnect(SocketAsyncEventArgs e)
{
if (e.ConnectSocket != null)
{
// simply start sending
bool completesAsynchronously = e.ConnectSocket.SendAsync(e);
// check if the completed event will be raised.
// if not, invoke the handler manually.
if (!completesAsynchronously)
{
SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
}
}
}
private void HandleReceive(SocketAsyncEventArgs e)
{
string responseL = Encoding.UTF8.GetString(e.Buffer, 0, e.Buffer.Length);
response += responseL;
temp += responseL;
string[] lines = Regex.Split(response, "\r\n\r\n");
if (lines.Length > 1 && header == "")
{
header = lines[0].ToString() + "\r\n";
lines[0] = "";
response = lines.ToString();
}
if (header == "")
{
bool completesAsynchronously = e.ConnectSocket.ReceiveAsync(e);
}
else
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
_callback(false, this);
});
}
}
I was trying to search for \r\n but it didn't help :/
Please help!
Thank you in advance :)
I use this code to send headers to the site and then read its content. I hope you find it useful.
ReadStateObject stateObject; //Info below
mytcpclient = new TcpClient();
mytcpclient.Connect(host, port);
mysocket = mytcpclient.Client;
SendHeader(mysocket);//Info below
ns = mytcpclient.GetStream();
if (ns.CanRead)
{
stateObject = new ReadStateObject(ns, 1024);
ns.BeginRead(stateObject.ReadBuffer, 0, stateObject.ReadBuffer.Length, new AsyncCallback(ReadCallBack), stateObject);
}
StateObject is small class used to represent the AsyncState object in BeginRead method:
class ReadStateObject
{
public NetworkStream Stream {get; set;}
public byte[] ReadBuffer;
public ReadStateObject(NetworkStream _stream, int bufferSize)
{
Stream = _stream;
ReadBuffer = new byte[bufferSize];
}
}
And this is a Callback Method used in BeginRead method.
private void ReadCallBack(IAsyncResult result)
{
ReadStateObject stateObject = (ReadStateObject)result.AsyncState;
NetworkStream myNetworkStream = stateObject.Stream;
int numberofbytesread = 0;
StringBuilder sb = new StringBuilder();
numberofbytesread = myNetworkStream.EndRead(result);
sb.Append(Encoding.ASCII.GetString(stateObject.ReadBuffer, 0, numberofbytesread));
/*It seems, if there is no delay, the DataAvailable may not be true even when there are still data to be received from the site, so I added this delay. Any suggestions, how to avoid this are welcome*/
Thread.Sleep(500);
while (myNetworkStream.DataAvailable)
{
byte[] mydata = new byte[1024];
numberofbytesread = myNetworkStream.Read(mydata, 0, mydata.Length);
sb.Append(Encoding.ASCII.GetString(mydata, 0, numberofbytesread));
}
Console.Writeln(sb.ToString());
mytcpclient.Close();
}
And this is where Headers are sent to the site
public void SendHeader(Socket mySocket)
{
String sBuffer = "";
sBuffer = sBuffer + "GET /"+pathquery+" HTTP/1.1" + "\r\n";
sBuffer = sBuffer + "Host: "+ hostname + "\r\n";
sBuffer = sBuffer + "Content-Type: text/html\r\n";
sBuffer = sBuffer + "\r\n";
Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);
mySocket.Send(Encoding.ASCII.GetBytes(sBuffer), Encoding.ASCII.GetBytes(sBuffer).Length, 0);
}
Maybe, you should use WebClient or HttpWebRequest instead of sockets.
Using sockets and interpreting Http protocol can be painful.

Categories

Resources