I'm trying to use generic IP socket objects in .NET. Connecting one of my sockets to another one and sending a few ping-pong messages works just fine.
But now I've implemented a server socket and somehow managed to create Schrödinger's socket.
The server socket handles the incoming connection request just fine, but the connecting socket seems to be connected when I check once but doesn't even have a RemoteEndpoint - though is showing "Connected = true" and it's disconnected when I check the 2nd time.
All sockets are initialized using this function:
private void initSocket() {
if (_socket != null) {
_socket.Dispose();
}
_socket = new Socket(_ownIP.AddressFamily, _socketType, _protocolType);
_socket.Bind(new IPEndPoint(_ownIP, _ownPort));
}
The _socketType is Stream and the _protocolType is Tcp in this case.
The connect function:
public bool connect(string ip, int port, int retries = 3) {
if (_isWaiting) {
throw new InvalidOperationException("Can't connect, when the socket is already waiting for clients.");
}
_wasWaiting = false;
initSocket();
_otherIP = IPAddress.Parse(ip);
_otherPort = port;
IPEndPoint endpoint = new IPEndPoint(_otherIP, _otherPort);
for (int i = 0; i < retries; i++) {
try {
IAsyncResult result = _socket.BeginConnect(endpoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(_connectTimeout, true);
if (_socket.Connected) {
_socket.EndConnect(result);
_isConnected = true;
return true;
}
else {
_socket.EndConnect(result);
}
}
catch (SocketException e) {
Console.WriteLine(e.Message);
}
}
return false;
}
The server uses this wait function to accept more clients:
private void wait() {
while (_isWaiting) {
try {
_socket.Listen(1);
var newSocket = _socket.Accept();
var newClient = new IPSocket(newSocket, _socketType, _protocolType, new MessageReceivedDelegate(clientReceivedMessage), true);
_clients.Add(newClient.PartnerIP + ":" + newClient.PartnerPort, newClient);
clientConnected(newClient.PartnerIP, newClient.PartnerPort);
while (true) {
Thread.Sleep(10);
}
}
catch (System.Net.Sockets.SocketException ex) {
//Need to find out which one is thrown when no client for accepting available
if (ex.ErrorCode == 2) {
}
Thread.Sleep(10);
}
catch {
Thread.Sleep(10);
}
}
}
The newSocket variable is connected at first and has a valid RemoteEndpoint, but the socket that's connected doesn't route back to newSocket.
I thought that maybe the connection resets when the server socket tries to accept new sockets, so I added the infinite wait loop - which didn't help.
So why does the connecting socket not have any RemoteEndpoint? (it's shown as null)
Since people usually recommend to accept the connection, handle data, close connection:
I want to keep the connection alive and I have to - not doing this is at the time not a solution that I'd consider.
Related
I'm trying to fetch some data, which is streamed via an UDP multicast Server. I've coded an C# WPF Application, where i can enter port and IP-address of server. The connection to the server is established sucessfully and i can receive multiple data packages (between 50-500 packages, varies over every try)
I'm calling the receive function via a dispatcher every 33ms. A longer time between the dispatcher event, does not solve the problem.
After a few seconds the UDPClient looses the connection, no data can be received and cannot be established any more.
Here is the function of the button establishing the connection and starts the dispatcher:
public int connectToArtWelder()
{
if (!checkIPAddress())
{
MessageBox.Show("Please enter a valid IP-Address.", "Wrong IP-Address", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
string iPString = tB_weldConIP.Text;
int port = Convert.ToInt32(tB_weldConPort.Text);
IPAddress iPAddress = IPAddress.Parse(iPString);
udpClient.Connect(iPAddress, port);
try
{
dispatcherTimer2.Tick += new EventHandler(Receive);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 0, 0, 33);
dispatcherTimer2.Start();
}
catch
{
}
}
return 0;
}
Here is the receive function:
private void Receive(object sender, EventArgs e)
{
try
{
int condition = udpClient.Available;
if (condition > 0)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var asyncResult = udpClient.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(10));
if (asyncResult.IsCompleted)
{
byte[] receiveBytes = udpClient.EndReceive(asyncResult, ref RemoteIpEndPoint);
double[] d = new double[receiveBytes.Length / 8];
// Do something with data
}
}
else
{
// The operation wasn't completed before the timeout and we're off the hook
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Please let me know if somebody got any equal problem or a solution for my problem.
SOLUTION:
I've just removed the
udpClient.Connect(ipAddress, port);
command and now it's running super smoothly.
I am trying to implement a TCP forwarder in C#. Specifically the application:
Listens to a TCP port and wait for a client,
When a client is connected, connects to a remote host,
Waits for incoming data on the both connections and exchange the data between two endpoints (acts as an proxy),
Closes one connection when the other one get closed by an endpoint.
I have adapted Simple TCP Forwader (by Garcia) to forward a range of ports so that
TCPForwarder.exe 10.1.1.1 192.168.1.100 1000 1100 2000
will forward any packets received on 10.1.1.1 on port 1000-1100 to remote host 192.168.1.100 port 2000-2100. I have employed this to expose a FTP server that is behind a NAT.
By running the above command, the client is able to connect to the FTP server and the following pattern in outputted to the console which is expected (refer to code):
0 StartReceive: BeginReceive
1 StartReceive: BeginReceive
1 OnDataReceive: EndReceive
1 OnDataReceive: BeginReceive
1 OnDataReceive: EndReceive
1 OnDataReceive: Close (0 read)
0 OnDataReceive: EndReceive
0 OnDataReceive: Close (exception)
But after successfully connecting for several times (pressing F5 in Filezilla), no further response is received from TCPForwarder (and FTP Server).
There seems to be two problems with my implementation that I cannot debug:
In that situation, the BeginReceive in StartReceive method gets called, but no data is received from the FTP server. I don't think that could be FTP server issue (it's a ProFTPD server) as it is a well-known FTP server.
Every time a connection is made and closed, the number of threads increases by 1. I don't think garbage collection would fix that. Number of threads is consistently increasing and forcing garabage collector to run doesn't decrease that either. I think there is some leak in my code that is also causing issue #1.
Edit:
Restarting the FTP server didn't fix the problem, so there is definitely a bug in the TCPForwarder.
Some issues pointed out by #jgauffin is fixed in the below code.
Here is the full code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Threading;
namespace TCPForwarder
{
class Program
{
private class State
{
public int ID { get; private set; } // for debugging purposes
public Socket SourceSocket { get; private set; }
public Socket DestinationSocket { get; private set; }
public byte[] Buffer { get; private set; }
public State(int id, Socket source, Socket destination)
{
ID = id;
SourceSocket = source;
DestinationSocket = destination;
Buffer = new byte[8192];
}
}
public class TcpForwarder
{
public void Start(IPEndPoint local, IPEndPoint remote)
{
Socket MainSocket;
try
{
MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MainSocket.Bind(local);
MainSocket.Listen(10);
}
catch (Exception exp)
{
Console.WriteLine("Error on listening to " + local.Port + ": " + exp.Message);
return;
}
while (true)
{
// Accept a new client
var socketSrc = MainSocket.Accept();
var socketDest = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
// Connect to the endpoint
socketDest.Connect(remote);
}
catch
{
socketSrc.Shutdown(SocketShutdown.Both);
socketSrc.Close();
Console.WriteLine("Exception in connecting to remote host");
continue;
}
// Wait for data sent from client and forward it to the endpoint
StartReceive(0, socketSrc, socketDest);
// Also, wait for data sent from endpoint and forward it to the client
StartReceive(1, socketDest, socketSrc);
}
}
private static void StartReceive(int id, Socket src, Socket dest)
{
var state = new State(id, src, dest);
Console.WriteLine("{0} StartReceive: BeginReceive", id);
try
{
src.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
}
catch
{
Console.WriteLine("{0} Exception in StartReceive: BeginReceive", id);
}
}
private static void OnDataReceive(IAsyncResult result)
{
State state = null;
try
{
state = (State)result.AsyncState;
Console.WriteLine("{0} OnDataReceive: EndReceive", state.ID);
var bytesRead = state.SourceSocket.EndReceive(result);
if (bytesRead > 0)
{
state.DestinationSocket.Send(state.Buffer, bytesRead, SocketFlags.None);
Console.WriteLine("{0} OnDataReceive: BeginReceive", state.ID);
state.SourceSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
}
else
{
Console.WriteLine("{0} OnDataReceive: Close (0 read)", state.ID);
state.SourceSocket.Shutdown(SocketShutdown.Both);
state.DestinationSocket.Shutdown(SocketShutdown.Both);
state.DestinationSocket.Close();
state.SourceSocket.Close();
}
}
catch
{
if (state!=null)
{
Console.WriteLine("{0} OnDataReceive: Close (exception)", state.ID);
state.SourceSocket.Shutdown(SocketShutdown.Both);
state.DestinationSocket.Shutdown(SocketShutdown.Both);
state.DestinationSocket.Close();
state.SourceSocket.Close();
}
}
}
}
static void Main(string[] args)
{
List<Socket> sockets = new List<Socket>();
int srcPortStart = int.Parse(args[2]);
int srcPortEnd = int.Parse(args[3]);
int destPortStart = int.Parse(args[4]);
List<Thread> threads = new List<Thread>();
for (int i = 0; i < srcPortEnd - srcPortStart + 1; i++)
{
int srcPort = srcPortStart + i;
int destPort = destPortStart + i;
TcpForwarder tcpForwarder = new TcpForwarder();
Thread t = new Thread(new ThreadStart(() => tcpForwarder.Start(
new IPEndPoint(IPAddress.Parse(args[0]), srcPort),
new IPEndPoint(IPAddress.Parse(args[1]), destPort))));
t.Start();
threads.Add(t);
}
foreach (var t in threads)
{
t.Join();
}
Console.WriteLine("All threads are closed");
}
}
}
The first problem is that the code will continue on connection failure on the destination socket (the accept loop). Use a continue; in the try/catch. There is also no guarantee that the sockets are still up when you invoke the first BeginReceive. Those calls also need to be wrapped.
Always wrap callback methods in a try/catch as your application can fail otherwise (in this case OnDataRecieve).
Fix that and start to write out the exceptions. They will surely give you a hint about what went wrong.
I am new to programming and I am working on an asynchronous client server application.
I can send a message to the server from the client, but when I receive the data to the server (OnDataReceived Method) and try to send the same data back to the client (for testing purposes) I can't.
Not sure what other info I need to give, so please let me know, I don't mean to be vague.
SERVER CODE
public void OnDataReceived(IAsyncResult asyncResult)
{
try
{
SocketPacket socketData = (SocketPacket)asyncResult.AsyncState;
int iRx = 0;
iRx = socketData.currentSocket.EndReceive(asyncResult);
char[] chars = new char[iRx];
Decoder decoder = Encoding.UTF8.GetDecoder();
int charLen = decoder.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
String receivedData = new String(chars);
//BroadCast(receivedData);
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => lbxMessages.Items.Add(receivedData)));
//Updated Code
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => broadcast(receivedData)));
WaitForData(socketData.currentSocket);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public class SocketPacket
{
public Socket currentSocket;
public byte[] dataBuffer = new byte[50];//allowing the 50 digist to be sent at once
}
private void WaitForData(Socket socket)
{
try
{
if (workerCallBack == null)
{
workerCallBack = OnDataReceived;
}
SocketPacket sckPack = new SocketPacket();
sckPack.currentSocket = socket;
socket.BeginReceive(sckPack.dataBuffer, 0, sckPack.dataBuffer.Length, SocketFlags.None, workerCallBack, sckPack);
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
}
Updated in response to Andrew's reply
I have a method that will be invoked when a client is connected
private void OnClientConnect(IAsyncResult asyncResult)
{
try
{
//Here we complete/end the Beginaccept() asynchronous call by
//calling EndAccept() - which returns the reference to a new socket object
workerSocket[clientCount] = listenSocket.EndAccept(asyncResult);
//Let the worker socket do the further processing for the just connected client
WaitForData(workerSocket[clientCount]);
//Now increment the client count
++clientCount;
if (clientCount<4)//allow max 3 clients
{
//Adds the connected client to the list
connectedClients.Add(listenSocket);
String str = String.Format("Client # {0} connected", clientCount);
this.Dispatcher.Invoke((Action)(() =>
{
//Display this client connection as a status message on the GUI
lbxMessages.Items.Add(str);
lblConnectionStatus.Content =clientCount + " Connected";
}));
//Since the main Socket is now free, it can go back and wait for
//other clients who are attempting to connect
listenSocket.BeginAccept(OnClientConnect, null);
}
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException)
{
HandleClientDisconnect(listenSocket);
}
}
(UPDATED)
ADDED METHOD TO BROADCAST MESSAGE RECEIVED BY SERVER BACK TO CLIENT
public void broadcast(string msg)
{
//foreach (Socket item in connectedClients)
//{
Socket broadcastSocket;
broadcastSocket = workerSocket[0]; //sends message to first client connected
byte[] broadcastBytes = null;
broadcastBytes = Encoding.ASCII.GetBytes(msg);
broadcastSocket.Send(broadcastBytes);
//}
}
There is two sockets involved in server-side TCP communication. First socket is a listening socket and you should use it only for accepting new request. Then every time you accept new request from the client you're getting another socket for every connection.
You trying to send data back via listening socket but not via socket that you accepted.
Sergey has it right. If you're looking to have one server handle multiple clients, then you'll need some sort of ServerTerminal class which can listen for new connections and then setup some sort of "connectedclient" class to handle IO to that socket. Your "OnDataReceived" method would be in the connectedclient class.
In your socket accept routine it should look something like:
private void OnClientConnection(IAsyncResult asyn)
{
if (socketClosed)
{
return;
}
try
{
Socket clientSocket = listenSocket.EndAccept(asyn);
ConnectedClient connectedClient = new ConnectedClient(clientSocket, this, _ServerTerminalReceiveMode);
connectedClient.StartListening();
In the accept routine you're passed a socket - i've named this "clientSocket". This is the socket you want to write to, not the listening socket.
i need to send a list of errors trough tcpclient with this code
private bool TryConn(out TcpClient cliente)
{
var client = new TcpClient();
try
{
client.Connect(IpAddress, PortNumber);
cliente = client;
return true;
}
catch
{
cliente = null;
return false;
}
}
public void ProcesssRecovery()
{
//NonMassiveErrorerror= new NonMassiveError();
TcpClient client;
//get error
IEnumerable<NonMassiveError> errorNotNotified = GetUncheckedNonMassiveError();
//check if lista is not empty
if (errorNotNotified .Count() >0 )
{
// check connection
if (TryConn(out client))
{
foreach (NonMassiveError error in errorNotNotified )
{ // sending error<--how detect conn stops
SendMessage(error, client, "asin" , "");
error.Save();
}
}
}
//stop thread 10mins
else
{
Thread.Sleep(TimeSpan.FromMinutes(10));
}
}
}
How can i check if the connection falls down in the foreach to stop sending data??
You should wait a little bit and read the response to the data you sent.
If the server acknowledge then it's ok(Response = 0), if not stop sending.
SendMessage() is your own method, right? Check if socket.Send() in it returns 0 bytes. That means that the socket have gotten disconnected.
Any suggestions on testing a TCP client listener. What I'm looking for is actual network stats like "payload size", "time-out values", "actual payload bytes received from server".
public static void tcpConnect() {
int port = 12345;
IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse("xx.xx.xx.xx"), port);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPGlobalProperties _ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] _tcpConnInfoArray = _ipGlobalProperties.GetActiveTcpConnections();
// bool isAvaiable = true;
try
{
server.Connect(ipep);
}
catch (SocketException e)
{
Console.WriteLine("Unable to connect to server");
Console.WriteLine(e.ToString());
return;
}
NetworkStream ns = new NetworkStream(server);
if (ns.CanRead)
{
foreach (TcpConnectionInformation tcpConnInfo in _tcpConnInfoArray)
{
if (tcpConnInfo.LocalEndPoint.Port == port)
{
//isAvaiable = false;
Console.WriteLine("Error: Can't Read from Port");
break;
}
Console.Write("Local endpoint: {0} ", tcpConnInfo.LocalEndPoint.Address);
Console.Write("Remote endpoint: {0} ", tcpConnInfo.RemoteEndPoint.Address);
Console.Write("{0}", tcpConnInfo.State);
}
}
else
{
Console.WriteLine("Error: Can't Read from Socket");
ns.Close();
return;
}
while (true)
{
string input = Console.ReadLine();
if (input == "exit")
break;
if (ns.CanWrite)
{
Console.WriteLine("NS Can write");
ns.Flush();
}
}
Console.WriteLine("Disconnection from server");
ns.Close();
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
So if I read correctly the real aim is to monitor the connection to see if it is still alive? We all know TCP is a state-full protocol. This means the "socket" must remain open. So you client will have a socket which is open. The TcpClient is just a nice wrapper that give you the ability to read and write to the socket. From the MS API page we can see there is an Active property. As the documentation reads, this is not updated if the remote disconnects... this is too bad and perhaps the problem you have. However they give the advise that if you want to watch the socket you will see if the remote disconnects. You can do this using the Connected property.
I know that not going to be the level of control you want, who wants to just see the TCP Client?? So get right at the Socket!!! once you have the socket you can have full control and viability over the connection. This should make you happy!!