TCP Socket client Async - connection handling - c#

I have a problem with connection handling in TCP Client socket.
This code should connect to localhost at 4444 port and listening for all incoming data from this TCP Server.
I need to write connection handling for this. For example if while attempting to connect server is not responding it should trying to connect again, or if connection is ready and after receiving some data TCP server will close the connection TCP client should try to reconnect again.
Can anyone help me with this problems
Here is what I have in this moment
using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
public class TCPClientNew : MonoBehaviour {
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];
private void StartClient()
{
try
{
_clientSocket.Connect(new IPEndPoint(IPAddress.Loopback,4444));
}
catch(SocketException ex)
{
Debug.Log(ex.Message);
// Try to reconnect ?? TODO
}
Debug.Log ("connected");
_clientSocket.BeginReceive(_recieveBuffer,0,_recieveBuffer.Length,SocketFlags.None,new AsyncCallback(ReceiveCallback),null);
}
private void ReceiveCallback(IAsyncResult AR)
{
//Check how much bytes are recieved and call EndRecieve to finalize handshake
int recieved = _clientSocket.EndReceive(AR);
if(recieved <= 0)
return;
//Copy the recieved data into new buffer , to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer,0,recData,0,recieved);
//Processing received data
Debug.Log (System.Text.Encoding.ASCII.GetString(recData));
//Start receiving again
_clientSocket.BeginReceive(_recieveBuffer,0,_recieveBuffer.Length,SocketFlags.None,new AsyncCallback(ReceiveCallback),null);
}
private void SendData(byte[] data)
{
SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
socketAsyncData.SetBuffer(data,0,data.Length);
_clientSocket.SendAsync(socketAsyncData);
}
void Start()
{
StartClient ();
}
}

What you want is a way to keep retrying the connection if it fails. And if there is an exception during a read, you want to see if we're still connect and if not then re-connect. I added a loop in the Connect() method to retry the connect after waiting for 1 second.
In the receive callback, I put a try/catch and if there is an exception I will go back to the Connect() method to retry the connection.
public class TCPClientNew
{
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];
private void Connect()
{
bool isConnected = false;
// Keep trying to connect
while (!isConnected)
{
try
{
_clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, 4444));
// If we got here without an exception we should be connected to the server
isConnected = true;
}
catch (SocketException ex)
{
Debug.Log(ex.Message);
// Wait 1 second before trying to connect again
Thread.Sleep(1000);
}
}
// We are now connected, start to receive
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private void ReceiveCallback(IAsyncResult AR)
{
//Check how much bytes are recieved and call EndRecieve to finalize handshake
try
{
int recieved = _clientSocket.EndReceive(AR);
if (recieved <= 0)
return;
//Copy the recieved data into new buffer , to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);
//Start receiving again
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (SocketException ex)
{
Debug.Log(ex.Message);
// If the socket connection was lost, we need to reconnect
if (!_clientSocket.Connected)
{
Connect();
}
else
{
//Just a read error, we are still connected
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
}
}
private void SendData(byte[] data)
{
SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
socketAsyncData.SetBuffer(data, 0, data.Length);
_clientSocket.SendAsync(socketAsyncData);
}
}

Related

C# - TCP Client does not receive server response

I am trying to create an asynchronous TCP client for Unity Game project. I am successfully creating a TCP socket connection however I am unable to receive any server response correctly.
Here is my TCP Client Listener code:
#region TCP Client
public static string ServerIP = "127.0.0.1";
public static int TCPPort = 8080;
private IPEndPoint serverTCPEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), TCPPort);
public static Socket TCPClient;
public Thread SocketThread;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private void StartTCPClient()
{
// Connect to a remote device.
try
{
// Create a TCP/IP socket.
TCPClient = new Socket(serverTCPEndPoint.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
TCPClient.BeginConnect(serverTCPEndPoint,
new AsyncCallback(ConnectCallback), TCPClient);
connectDone.WaitOne();
// Receive the response from the remote device.
TCPReceive();
receiveDone.WaitOne();
// Write the response to the console.
Debug.Log("Response received: " + response);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Complete the connection.
TCPClient.EndConnect(ar);
Debug.Log("Socket connected to: " + TCPClient.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
private void TCPReceive()
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = TCPClient;
// Begin receiving the data from the remote device.
TCPClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
Debug.Log("RECEIVING");
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
// Read data from the remote device.
int bytesRead = TCPClient.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
TCPClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
Debug.Log("RECEIVING 2: " + response);
UnityThread.executeInUpdate(() =>
{
OnIncomingTCPData(response);
});
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
#endregion
Here is how I start the client:
// Use this for initialization
void Start()
{
int accId = ClientLoginServer.accountId;
DuloGames.UI.Demo_Chat.worldServer = this;
if (accId != 0)
{
CursorMode cursorMode = CursorMode.Auto;
Vector2 hotSpot = Vector2.zero;
SocketThread = new Thread(StartTCPClient);
SocketThread.IsBackground = true;
SocketThread.Start();
Cursor.SetCursor(brownCursor, hotSpot, cursorMode);
//GameObject go = Instantiate(CharacterNamePrefab) as GameObject;
GameObject CharacterSelectPanel = GameObject.Find("/Canvas/Panel/CharacterSelectPanel");
}
DontDestroyOnLoad(worldserverConnection);
}
All I can see when server is sending data to the Unity TCP client listener is the following line:
Debug.Log("RECEIVING");
However this line seems to be not reachable:
Debug.Log("RECEIVING 2: " + response);
The following is allowing me to execute the called functions in the main unity thread.
UnityThread.executeInUpdate(() =>
{
OnIncomingTCPData(response);
});
Do you have any idea why my Asynchronous TCP client is not working correctly ?

Cannot send multiple messages via socket without releasing and re-initialising the socket

I need to exchange data from two machines with the highest throughput as possible. I have a server and a client.
The client needs to send several messages to the server (in the real application, 1 message every 22 ms). Instead of initializing the socket every time I need to send something, I would like to keep the connection open and to write several messages via socket that will be initialized once for all.
Here's my client in C#:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Diagnostics;
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
// The port number for the remote device.
private const int port = 11000;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static Socket InitClient()
{
// Connect to a remote device.
// Establish the remote endpoint for the socket.
// The name of the
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[1];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return client;
}
private static void SendData(Socket client) {
try {
// Send test data to the remote device.
StringBuilder a = new StringBuilder();
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReleaseClient(Socket client) {
try
{
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
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
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length >= 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
Socket client = InitClient();
for (int i = 0; i < 2; i++) {
SendData(client);
}
ReleaseClient(client);
Console.ReadLine();
return 0;
}
And this is the server in Python:
import socketserver
import socket
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(87380).strip()
print("{} wrote:".format(self.client_address[1]))
l = (str(len(self.data)))
print(l)
self.request.sendall(l.encode())
HOST, PORT = socket.gethostname(), 11000
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
server.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
server.serve_forever()
I can connect to the server, and if I'm calling this from C#:
InitClient();
SendData(client);
ReleaseClient(client);
Everything works fine. But if, like in my example, I'm trying to loop over SendData(), I can only send the first message and, when trying to send the second, the server gives me the following error:
System.Net.Sockets.SocketException (10053): An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
at AsynchronousClient.Receive(Socket client) in C:\Users\giuli\source\repos\ApplicationSending\ApplicationSending\Program.cs:line 133 (NdR the lines that corresponds to:
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);)
I cannot understand what is happening. Shouldn't it be possible to send multiple messages without having to re-initialize the socket?
This is how we establish the connection and keep sending multiple messages.
#Client.cs
private AutoResetEvent[] _autoSendReceiveEvents;
##Connection method
using (var connectArgs = new SocketAsyncEventArgs())
{
connectArgs.AcceptSocket = _socket;
connectArgs.RemoteEndPoint = _ipEndPoint;
connectArgs.Completed += OnCompleted;
var result = _socket.ConnectAsync(connectArgs);
if (result)
{
_autoConnectEvent.WaitOne();
}
var errorCode = connectArgs.SocketError;
if (errorCode != SocketError.Success)
{
CloseSocket();
throw new SocketException((int) errorCode);
}
}
##Sending Data
public void Send(string message)
{
if (!IsConnected)
{
throw new SocketException((int) SocketError.NotConnected);
}
var sendBuffer = GetBytes(message);
using (var sendReceiveArgs = new SocketAsyncEventArgs())
{
sendReceiveArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
sendReceiveArgs.AcceptSocket = _socket;
sendReceiveArgs.RemoteEndPoint = _ipEndPoint;
sendReceiveArgs.Completed += OnSend;
var result = _socket.SendAsync(sendReceiveArgs);
if (result)
{
_autoSendReceiveEvents[SendOperation].WaitOne();
}
}
}
##OnSend
private void OnSend(object sender, SocketAsyncEventArgs eventArgs)
{
try
{
if (eventArgs.SocketError != SocketError.Success)
{
CloseSocket();
_autoSendReceiveEvents[SendOperation].Set();
}
switch (eventArgs.LastOperation)
{
case SocketAsyncOperation.Send:
_autoSendReceiveEvents[SendOperation].Set();
break;
}
}
catch (Exception ex)
{
CloseSocket();
_autoSendReceiveEvents[SendOperation].Set();
}
}

StreamSocketListener only works once; the second call fails

Any help on the issue below would be highly appreciated. I'm using the StreamSocketListener Class to accept TCP/IP connection on my Raspberry Pi 3 running windows IoT 10 Core.
This is my server code so far:
static string _maintenancePort = "8888";
public async static void StartListening()
{
try
{
StreamSocketListener listener = new StreamSocketListener();
var currentSetting = listener.Control.QualityOfService;
listener.Control.QualityOfService = SocketQualityOfService.LowLatency;
listener.ConnectionReceived += SocketListener_ConnectionReceived;
listener.Control.KeepAlive = true;
await listener.BindServiceNameAsync(_maintenancePort);
}
catch (Exception e)
{
Log.WriteErrorLog(e);
}
}
private static async void SocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
Log.WriteDebugLog("Incoming data...");
Stream inStream = args.Socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(inStream);
string request = await reader.ReadLineAsync();
if (request != null)
{
Log.WriteDebugLog("Received : " + request);
}
}
catch (Exception e)
{
Log.WriteErrorLog(e);
}
}
I wrote the following client code to connect to the socket. This code runs on another machine.
// ManualResetEvent instances signal completion.
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 Socket client;
public static string SendMessageToClient(string ip, int port, string message, bool expectResponse)
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(ipAddress.AddressFamily,SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
message += "^" + expectResponse.ToString();
// Send test data to the remote device.
Send(client, message);
sendDone.WaitOne();
// Receive the response from the remote device.
if (expectResponse)
{
Receive(client);
receiveDone.WaitOne();
}
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
return response;
}
catch (Exception e)
{
Log.Write(e, false);
return "";
}
}
private static void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Log.WriteSingleMessage(String.Format("Sent {0} bytes to server.", bytesSent), false);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Log.Write(e, false);
}
}
}
The client code is triggered by a button click event. The problem that I'm facing is that the server code above only works once. If I send a message to the server with the client code, the server processes the string perfect. However, if I hit the button a second time, the SocketListener_ConnectionReceived event is triggered but no data is coming in. I've tried several classes for ConnectionReceived but they all behave the same.
I checked with netstat on the Raspberry Pi if the server is listening and it is.
TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING
Even child processes are created for handling the connection as you would expect from an Async calls. The client code closes the socket after it received a message that the data has been send (waitOne()) and the socket on the client machines changes to CLOSE_WAIT.
TCP 10.0.102.10:8888 10.0.100.11:31298 CLOSE_WAIT
TCP 10.0.102.10:8888 10.0.100.11:31299 ESTABLISHED
Can anyone help me out and point me in the right direction as to what I'm doing wrong. any help would be highly appreciated.
You can try to use synchronous methods instead asynchronous methods for socket client. It will work. Please refer to following code:
public void SendMessageToClientSync(string ip, int port, string message, bool expectResponse)
{
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket sender = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes(message);
// Send the data through the socket.
int bytesSent = sender.Send(msg);
if(expectResponse)
{
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes, 0, bytesRec));
}
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

TCP Server & Client not receiving each other's information

I'm very new to all that TCP thing and I just can't find what's going wrong here. It might be pretty obvious for some of you. When I run the code, I run the Start process on the Server side, it displays 'waiting for connection', then I start the client, the later displays 'connected to ', I relaunch the start process on the server side and it displays 'Connected to the client' and then, on both sides, when I call the method SendThroughTCPSocket("whatever string"), I can see it being sent but nothing is being detected on the other side ?
What could I be doing wrong ?
How could I have both of them constantly listening to each other ?
SERVER SIDE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Server
{
// State object for reading client data asynchronously
public class StateObject
{
#region instance variables
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
#endregion
}
public class AsynchronousSocketListener
{
public enum TransmissionSate
{
Waiting,
Receiving,
Received,
Sending,
Sent,
Off
}
#region class variables
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static IPHostEntry ipHostInfo;
public static IPAddress ipAddress;
public static IPEndPoint localEndPoint;
public static Socket listener;
public static Socket handler;
//flags
public static TransmissionSate currentState = TransmissionSate.Off;
#endregion
#region constructor
public AsynchronousSocketListener()
{
}
#endregion
#region Start Listening
//Start Listening for Client
public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
ipHostInfo = Dns.Resolve(Dns.GetHostName());
ipAddress = ipHostInfo.AddressList[0];
localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
//flag
currentState = TransmissionSate.Waiting;
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nConnected to the client.");
//Console.Read();
}
#endregion
#region Receive Methods
//Accept communication and Start reading
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar);
//flag
currentState = TransmissionSate.Receiving;
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
//Read and handle the information
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
//flag
currentState = TransmissionSate.Received;
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
//reference
Data.lastResponse = content;
//react
Console.WriteLine("Server just received a request");
switch (content)
{
case "DataRequest":
Console.WriteLine("Client is trying to get the preset data");
SendThroughTCPSocket(Data.xmlToSend + Data.endOfFile);
break;
default:
Console.WriteLine("Unknown request from Client");
break;
}
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
#endregion
#region Send Methods
//Send data to a socket
private static void Send(Socket handler, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
//flag
currentState = TransmissionSate.Sending;
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
//Sending data and closing handler
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
//flag
currentState = TransmissionSate.Sent;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region SendThroughTCPSocket
public static void SendThroughTCPSocket(String data)
{
try
{
// Send data to the client
Send(handler, data);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region Stop Server
//Close Sever
public static void StopServer()
{
try
{
// Release the socket.
handler.Shutdown(SocketShutdown.Both);
handler.Close();
//flag
currentState = TransmissionSate.Off;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
}
}
CLIENT SIDE :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Client
{
// State object for receiving data from remote device.
public class StateObject
{
#region instance variables
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
#endregion
}
public class AsynchronousClient
{
public enum TransmissionSate
{
Waiting,
Receiving,
Received,
Sending,
Sent,
Off
}
#region instance variables
// The port number for the remote device.
private const int port = 11000;
#endregion
#region class variables
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
public static IPHostEntry ipHostInfo;
public static IPAddress ipAddress;
public static IPEndPoint remoteEP;
public static Socket client;
//flags
public static TransmissionSate currentState = TransmissionSate.Off;
public static bool isConnected = false;
#endregion
#region Start Client
//Start Client and use it
public static void StartClient()
{
// Connect to a remote device.
try
{
//flag
isConnected = false;
// Establish the remote endpoint for the socket.
// The name of the host
// remote device is the current device
ipHostInfo = Dns.Resolve(Dns.GetHostName());
ipAddress = ipHostInfo.AddressList[0];
remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
//flag
currentState = TransmissionSate.Waiting;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region Stop Client
//Close Client
public static void StopClient()
{
try
{
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
//flag
currentState = TransmissionSate.Off;
isConnected = false;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region Connect
//callback for connection
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
//flag
isConnected = true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region Receive
//Receive data
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
//flag
currentState = TransmissionSate.Receiving;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//callback Receive
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
//flag
currentState = TransmissionSate.Received;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region Send
//Send data
private static void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
//flag
currentState = TransmissionSate.Sending;
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
//Callback send
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
//flag
currentState = TransmissionSate.Sent;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#endregion
#region SendThroughTCPSocket
public static string SendThroughTCPSocket(String data)
{
try
{
// Send data to the remote device.
Send(client, data);
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
return response;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return "<No response>";
}
}
#endregion
}
}
You need to implement a protocol on top of the socket. In your server you're using "" to determine that a message has ended and you only print it out once you get this. However, I can see nowhere in the client where you do this. Ideally you want to have a start of message token and an end of message token so that you can send multiple messages over a socket.
Change your client send method to
private static void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data + "<EOF>");
//flag
currentState = TransmissionSate.Sending;
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
and this should work, but I do recommend implementing a protocol with start and end tokens.
In the receivecallback of the client you have code that never checks for any kind of protocol and simply attempts to keep on receiving data:
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
At some stage your program must check the data to see if there is an end token of some sort.
Try changing this to the following:
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
var endPos = state.sb.ToString().IndexOf("<EOF>");
if (endPos > -1) //we have a complete message. huzzah!
{
response = state.sb.ToString().Substring(0, endPos);
// Signal that all bytes have been received.
receiveDone.Set();
//flag
currentState = TransmissionSate.Received;
}
else
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
I must reiterate: you need to design a protocol if you are going to use raw sockets: a way to identify the start and end of discreet messages.

Asynchronous Socket C# Sending Data but not Recieving

Here is my code . I am successfully sending data but not receiving any data . here is my code below . I am actually sending 23 bytes over the socket and its executing the event but its not receiving the data server is sending back . I want to have persistent bi directional connection to send and receive data
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Collections.Generic;
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
// The port number for the remote device.
private const int port = 18001;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static void StartClient()
{
// Connect to a remote device.
try
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.1.140"), port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
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;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Send(Socket client, String data)
{
// F2 00 DATA FF
List<byte> byteTemp = new List<byte>();
byteTemp.Add(0XF2);
byteTemp.Add(0X00);
byte[] TcpData = Encoding.ASCII.GetBytes("0040005001100180");
foreach (byte eachdata in TcpData)
{
byteTemp.Add(eachdata);
}
byteTemp.Add(0XFF);
byte[] byteData = byteTemp.ToArray();
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
Console.WriteLine("Receieve Call Back Invoked");
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
Console.WriteLine("Receieve End Receieve");
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
Console.WriteLine("Read : " + bytesRead.ToString());
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
Console.WriteLine("Begin Receieve completed");
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
Console.WriteLine("Received data from client");
// Signal that all bytes have been received.
receiveDone.Set();
}
Console.WriteLine("completed receive callback");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
the data has been received, but not utilized as "else" in the above method is not getting executed.

Categories