Async test client for TCP socket

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I've made some modifications to the async client example found on MSDN for the purpose of testing a server application.



It works entirely as I expect and/or want, even running multiple clients, but as I intend to use this as the foundation of the actual client and not just for testing I figured I'd see if I'm doing anything wrong or if anything could be optimized.



My main point of concern here being the usage of Task.Run to initialize the send and receive functions for the client. I've seen instances that suggest using the Task.Factory is something I want to avoid unless needed and I've seen instances that suggest Task.Factory.FromAsync is what I want to use. Even then, I've only seen FromAsync being proposed with BeginSend, so I'm also not sure if this applies to BeginRecieve as well.



While most of it is from msdn, I've had past issues with examples. Having said that, if you see anything else that stands out to you, please feel free to point that out to me as well.



using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Threading.Tasks;

public class StateObject

public Socket workSocket = null;
public const int BufferSize = 256;
public byte buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();


public class AsynchronousClient

private const int port = 11000;

private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

private static String response = String.Empty;

public static void Main(String args)

StartClient();


private static void StartClient()

try

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

Task.Run(() => Receive(client));

Task.Run(() => StartTest(client));

Console.Read();


catch (Exception e)

Console.WriteLine(e.ToString());



private static void StartTest(Socket client)

for (int i = 0; i <= 3000; i++)

Send(client, string.Format
("This Is A Test Message (0)<


private static void ConnectCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);

Console.WriteLine("Socket connected to 0",
client.RemoteEndPoint.ToString());

connectDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Receive(Socket client)

try

StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void ReceiveCallback(IAsyncResult ar)

try

StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

int bytesRead = client.EndReceive(ar);
state.sb.Append
(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

Console.WriteLine(state.sb.ToString()); state.sb.Clear();

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Send(Socket client, String data)

byte byteData = Encoding.ASCII.GetBytes(data);

Console.WriteLine("Sending: " + data);

client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);


private static void SendCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;

int bytesSent = client.EndSend(ar);

sendDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());









share|improve this question





















  • A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
    – VisualMelon
    Jan 22 at 17:22
















up vote
1
down vote

favorite












I've made some modifications to the async client example found on MSDN for the purpose of testing a server application.



It works entirely as I expect and/or want, even running multiple clients, but as I intend to use this as the foundation of the actual client and not just for testing I figured I'd see if I'm doing anything wrong or if anything could be optimized.



My main point of concern here being the usage of Task.Run to initialize the send and receive functions for the client. I've seen instances that suggest using the Task.Factory is something I want to avoid unless needed and I've seen instances that suggest Task.Factory.FromAsync is what I want to use. Even then, I've only seen FromAsync being proposed with BeginSend, so I'm also not sure if this applies to BeginRecieve as well.



While most of it is from msdn, I've had past issues with examples. Having said that, if you see anything else that stands out to you, please feel free to point that out to me as well.



using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Threading.Tasks;

public class StateObject

public Socket workSocket = null;
public const int BufferSize = 256;
public byte buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();


public class AsynchronousClient

private const int port = 11000;

private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

private static String response = String.Empty;

public static void Main(String args)

StartClient();


private static void StartClient()

try

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

Task.Run(() => Receive(client));

Task.Run(() => StartTest(client));

Console.Read();


catch (Exception e)

Console.WriteLine(e.ToString());



private static void StartTest(Socket client)

for (int i = 0; i <= 3000; i++)

Send(client, string.Format
("This Is A Test Message (0)<


private static void ConnectCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);

Console.WriteLine("Socket connected to 0",
client.RemoteEndPoint.ToString());

connectDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Receive(Socket client)

try

StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void ReceiveCallback(IAsyncResult ar)

try

StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

int bytesRead = client.EndReceive(ar);
state.sb.Append
(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

Console.WriteLine(state.sb.ToString()); state.sb.Clear();

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Send(Socket client, String data)

byte byteData = Encoding.ASCII.GetBytes(data);

Console.WriteLine("Sending: " + data);

client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);


private static void SendCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;

int bytesSent = client.EndSend(ar);

sendDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());









share|improve this question





















  • A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
    – VisualMelon
    Jan 22 at 17:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've made some modifications to the async client example found on MSDN for the purpose of testing a server application.



It works entirely as I expect and/or want, even running multiple clients, but as I intend to use this as the foundation of the actual client and not just for testing I figured I'd see if I'm doing anything wrong or if anything could be optimized.



My main point of concern here being the usage of Task.Run to initialize the send and receive functions for the client. I've seen instances that suggest using the Task.Factory is something I want to avoid unless needed and I've seen instances that suggest Task.Factory.FromAsync is what I want to use. Even then, I've only seen FromAsync being proposed with BeginSend, so I'm also not sure if this applies to BeginRecieve as well.



While most of it is from msdn, I've had past issues with examples. Having said that, if you see anything else that stands out to you, please feel free to point that out to me as well.



using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Threading.Tasks;

public class StateObject

public Socket workSocket = null;
public const int BufferSize = 256;
public byte buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();


public class AsynchronousClient

private const int port = 11000;

private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

private static String response = String.Empty;

public static void Main(String args)

StartClient();


private static void StartClient()

try

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

Task.Run(() => Receive(client));

Task.Run(() => StartTest(client));

Console.Read();


catch (Exception e)

Console.WriteLine(e.ToString());



private static void StartTest(Socket client)

for (int i = 0; i <= 3000; i++)

Send(client, string.Format
("This Is A Test Message (0)<


private static void ConnectCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);

Console.WriteLine("Socket connected to 0",
client.RemoteEndPoint.ToString());

connectDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Receive(Socket client)

try

StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void ReceiveCallback(IAsyncResult ar)

try

StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

int bytesRead = client.EndReceive(ar);
state.sb.Append
(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

Console.WriteLine(state.sb.ToString()); state.sb.Clear();

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Send(Socket client, String data)

byte byteData = Encoding.ASCII.GetBytes(data);

Console.WriteLine("Sending: " + data);

client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);


private static void SendCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;

int bytesSent = client.EndSend(ar);

sendDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());









share|improve this question













I've made some modifications to the async client example found on MSDN for the purpose of testing a server application.



It works entirely as I expect and/or want, even running multiple clients, but as I intend to use this as the foundation of the actual client and not just for testing I figured I'd see if I'm doing anything wrong or if anything could be optimized.



My main point of concern here being the usage of Task.Run to initialize the send and receive functions for the client. I've seen instances that suggest using the Task.Factory is something I want to avoid unless needed and I've seen instances that suggest Task.Factory.FromAsync is what I want to use. Even then, I've only seen FromAsync being proposed with BeginSend, so I'm also not sure if this applies to BeginRecieve as well.



While most of it is from msdn, I've had past issues with examples. Having said that, if you see anything else that stands out to you, please feel free to point that out to me as well.



using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Threading.Tasks;

public class StateObject

public Socket workSocket = null;
public const int BufferSize = 256;
public byte buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();


public class AsynchronousClient

private const int port = 11000;

private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

private static String response = String.Empty;

public static void Main(String args)

StartClient();


private static void StartClient()

try

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

Task.Run(() => Receive(client));

Task.Run(() => StartTest(client));

Console.Read();


catch (Exception e)

Console.WriteLine(e.ToString());



private static void StartTest(Socket client)

for (int i = 0; i <= 3000; i++)

Send(client, string.Format
("This Is A Test Message (0)<


private static void ConnectCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);

Console.WriteLine("Socket connected to 0",
client.RemoteEndPoint.ToString());

connectDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Receive(Socket client)

try

StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void ReceiveCallback(IAsyncResult ar)

try

StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

int bytesRead = client.EndReceive(ar);
state.sb.Append
(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

Console.WriteLine(state.sb.ToString()); state.sb.Clear();

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);

catch (Exception e)

Console.WriteLine(e.ToString());



private static void Send(Socket client, String data)

byte byteData = Encoding.ASCII.GetBytes(data);

Console.WriteLine("Sending: " + data);

client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);


private static void SendCallback(IAsyncResult ar)

try

Socket client = (Socket)ar.AsyncState;

int bytesSent = client.EndSend(ar);

sendDone.Set();

catch (Exception e)

Console.WriteLine(e.ToString());











share|improve this question












share|improve this question




share|improve this question








edited Jan 22 at 12:11









t3chb0t

32.1k54195




32.1k54195









asked Jan 16 at 1:01









Emitedoc

233




233











  • A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
    – VisualMelon
    Jan 22 at 17:22
















  • A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
    – VisualMelon
    Jan 22 at 17:22















A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
– VisualMelon
Jan 22 at 17:22




A bit of a shot in the dark, but I'm going to shamelessly link to an older answer of mine about sending discrete messages over TCP which discusses some of the fun that can be had with that, because I think the rest of the web (looking at you, MSDN) does a poor job of presenting this. Presently your code appears to just send a stream of ASCII from server to client, but rarely is this the final objective. Sorry if I'm jumping to conclusions about your intentions.
– VisualMelon
Jan 22 at 17:22















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185178%2fasync-test-client-for-tcp-socket%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185178%2fasync-test-client-for-tcp-socket%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?