Related
I'm currently developing a server that deals with clients using a consumer/producer approach with threads and a blocking collection as shown below:
public class NetworkClient
{
private bool _started = false;
private int _clientId;
private readonly Socket _socket;
private static AutoResetEvent _lastMessageWasAckd = new AutoResetEvent(true);
private static BlockingCollection<byte[]> _messageQueue = new BlockingCollection<byte[]>();
public NetworkClient(Socket socket, int clientId)
{
this._socket = socket;
this._clientId = clientId;
}
public int getClientID() {
return _clientId;
}
public void SendMessage(string message)
{
Console.WriteLine("Adding to player's sending queue " + _clientId);
_messageQueue.Add(Encoding.ASCII.GetBytes(message));
}
public void Start()
{
Thread receiver = new Thread(new ThreadStart(ReceivingThreadProc));
Thread sender = new Thread(new ThreadStart(SendingThreadProc));
receiver.Start();
sender.Start();
this._started = true;
}
public void Stop()
{
this._started = false;
}
private void ReceivingThreadProc()
{
byte[] bytes = new byte[1024];
string data;
try
{
while (_started && _socket.Connected)
{
int numByte = this._socket.Receive(bytes);
data = Encoding.ASCII.GetString(bytes, 0, numByte);
if (numByte == 0)
{
break;
}
if (data == "ACK")
{
_lastMessageWasAckd.Set();
continue;
}
// Acknowledge the message
_socket.Send(Encoding.ASCII.GetBytes("ACK"));
ServerReceiver.onEvent(this._clientId, data);
}
}
catch (Exception e)
{
this._socket.Close();
}
}
private void SendingThreadProc()
{
while (_started && _socket.Connected)
{
_lastMessageWasAckd.WaitOne();
byte[] message = _messageQueue.Take();
Console.WriteLine("Sending the following message to client number: " + _clientId);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(message));
_socket.Send(message);
_lastMessageWasAckd.Reset();
}
}
}
There will be an instance of NetworkClient created for every client that connects for the server. The issue is that sometimes a message is queued to be sent to client 1 (this is confirmed by the Console.Writeline in the SendMessage method) however that message is sent to client 0 (Shown by the console writeline in the SendingThreadProc method) instead. Is this due to a thread safety issue, or am I missing something entirely? This typically happens when two messages are sent right after one another.
Any help would be greatly appreciated.
EDIT:
As many people rightly pointed out I haven't added where I call SendMessage I'll put this class down below:
class NetworkServer
{
private int latestClient = 0;
private ServerReceiver _serverReceiver;
private static readonly Dictionary<int, NetworkClient> clients = new Dictionary<int, NetworkClient>();
public NetworkServer()
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 5656);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._serverReceiver = new ServerReceiver();
this._serverReceiver.start();
try
{
listener.Bind(endpoint);
listener.Listen(10);
while (true)
{
Socket clientSocket = listener.Accept();
this.OnClientJoin(clientSocket);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void SendClientMessage(int clientId, string package, CommandType commandType,
Dictionary<string, object> data = null)
{
if (data == null)
{
data = new Dictionary<string, object>();
}
SendClientMessageRaw(clientId, new NetworkCommand(clientId, package, commandType, data).ToJson());
}
public static void SendClientMessageRaw(int id, string message)
{
Console.WriteLine("Supposed to send to client number " + clients[id].getClientID());
clients[id].SendMessage(message);
}
private void OnClientJoin(Socket socket)
{
// Add client to array, perform handshake?
NetworkClient networkClient = new NetworkClient(socket, latestClient);
clients.Add(latestClient, networkClient);
Console.WriteLine("player added :" + latestClient);
networkClient.Start();
latestClient++;
if (latestClient == 2)
{
SendClientMessage(1, "Test", CommandType.Ping, null);
}
}
Could it be because your message queue is static and therefore shared between all NetworkClients? Meaning client A can pick up a message for client B?
Might be as easy as removing static from the properties.
I'm starting a socket program, and am in the process of setting up a Server and two types of Clients (a requester and an arbiter). I'm in the middle of testing the connections, but they aren't quite working. Right now I just have a button for each form: an "Accept" button for the Arbiter and "Request" for the Requester. Each button should cause a popup on the other form, but neither is working. Also, I've noticed that when I close all programs, the Server is still running in my processes. What am I doing wrong?
Below is the Server code:
namespace FPPLNotificationServer
{
class Server
{
static Socket listenerSocket;
static List<ClientData> _clients;
static void Main(string[] args)
{
Console.WriteLine("Starting server on " + Packet.GetIP4Address());
listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clients = new List<ClientData>();
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(Packet.GetIP4Address()), 4242);
listenerSocket.Bind(ip);
Thread listenThread = new Thread(ListenThread);
listenThread.Start();
}
static void ListenThread()
{
for (;;)
{
listenerSocket.Listen(0);
_clients.Add(new ClientData(listenerSocket.Accept()));
}
}
public static void Data_IN(object cSocket)
{
Socket clientSocket = (Socket)cSocket;
byte[] Buffer;
int readBytes;
for (;;)
{
try
{
Buffer = new byte[clientSocket.SendBufferSize];
readBytes = clientSocket.Receive(Buffer);
if(readBytes > 0)
{
Packet packet = new Packet(Buffer);
DataManager(packet);
}
}catch(SocketException ex)
{
Console.WriteLine("Client Disconnected");
}
}
}
public static void DataManager(Packet p)
{
switch (p.packetType)
{
case Packet.PacketType.Notification:
foreach(ClientData c in _clients)
{
c.clientSocket.Send(p.ToBytes());
}
break;
}
}
}
class ClientData
{
public Socket clientSocket;
public Thread clientThread;
public string id;
public ClientData()
{
this.id = Guid.NewGuid().ToString();
clientThread = new Thread(Server.Data_IN);
clientThread.Start(clientSocket);
SendRegistrationPacket();
}
public ClientData(Socket clientSocket)
{
this.clientSocket = clientSocket;
this.id = Guid.NewGuid().ToString();
clientThread = new Thread(Server.Data_IN);
clientThread.Start(clientSocket);
SendRegistrationPacket();
}
public void SendRegistrationPacket()
{
Packet p = new Packet(Packet.PacketType.Registration, "server");
p.Gdata.Add(id);
clientSocket.Send(p.ToBytes());
}
}
}
ServerData
namespace FPPLNotificationServerData
{
[Serializable]
public class Packet
{
public List<String> Gdata;
public int packetInt;
public bool packetBool;
public string senderID;
public PacketType packetType;
public string PlantName, ProductSegment, ProductCustomer;
public int PlantNumber;
public string ProductNumber, ProductAltNumber;
public string ProductDiscription;
public int ProductLine;
public string ProductClass, ProductLocation;
public int ProductMcDFactor;
public Packet(PacketType type, String senderID)
{
Gdata = new List<string>();
this.senderID = senderID;
this.packetType = type;
}
public Packet(byte[] packetBytes)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(packetBytes);
Packet p = (Packet)bf.Deserialize(ms);
ms.Close();
this.Gdata = p.Gdata;
this.senderID = p.senderID;
this.packetType = p.packetType;
this.packetBool = p.packetBool;
this.packetInt = p.packetInt;
}
public byte[] ToBytes()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, this);
byte[] bytes = ms.ToArray();
ms.Close();
return bytes;
}
public static string GetIP4Address()
{
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach(IPAddress i in ips)
{
if(i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return i.ToString();
}
}
return "127.0.0.1";
}
public enum PacketType
{
Registration,
Chat,
Notification,
Request,
ArbiterDecision,
Accept,
Decline
}
}
}
Request Class:
namespace FPPLRequestClient
{
public partial class frm_Request : Form
{
public static Socket master;
public static string name;
public static string id;
public bool isConnected;
public frm_Request()
{
InitializeComponent();
string IP = "127.0.0.1";
master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEP = new IPEndPoint(IPAddress.Parse(IP), 4242);
try
{
master.Connect(ipEP);
isConnected = true;
}
catch (Exception)
{
isConnected = false;
}
string connectionStatus = isConnected ? "Connected" : "Disconnected";
this.lbl_Status.Text = "Status: " + connectionStatus;
Thread t = new Thread(Data_IN);
t.Start();
}
void Data_IN()
{
byte[] Buffer;
int readBytes;
while (isConnected)
{
try
{
Buffer = new byte[master.SendBufferSize];
readBytes = master.Receive(Buffer);
if(readBytes > 0)
{
DataManager(new Packet(Buffer));
}
}catch(SocketException ex)
{
isConnected = false;
this.Dispose();
}
}
}//END DATA IN
void DataManager(Packet p)
{
switch (p.packetType)
{
case Packet.PacketType.Registration:
id = p.Gdata[0];
break;
case Packet.PacketType.Accept:
//MessageBox.Show(p.ProductNumber);
this.lbl_Status.Text = p.ProductNumber + " accepted";
Invalidate();
break;
}
}
private void btn_Request_Click(object sender, EventArgs e)
{
Packet p = new Packet(Packet.PacketType.Request, id);
p.ProductNumber = "123456";
master.Send(p.ToBytes());
}
}
}
Arbiter Class:
namespace FPPLArbiterClient
{
public partial class frm_Arbiter : Form
{
public static Socket master;
public static string name;
public static string id;
public bool isConnected;
public frm_Arbiter()
{
InitializeComponent();
string IP = "127.0.0.1";
master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEP = new IPEndPoint(IPAddress.Parse(IP), 4242);
try
{
master.Connect(ipEP);
isConnected = true;
}
catch (Exception)
{
isConnected = false;
}
string connectionStatus = isConnected ? "Connected" : "Disconnected";
this.lbl_Status.Text = "Status: " + connectionStatus;
Thread t = new Thread(Data_IN);
t.Start();
}
void Data_IN()
{
byte[] Buffer;
int readBytes;
while (isConnected)
{
try
{
Buffer = new byte[master.SendBufferSize];
readBytes = master.Receive(Buffer);
if(readBytes > 0)
{
DataManager(new Packet(Buffer));
}
}catch(SocketException ex)
{
isConnected = false;
this.Dispose();
}
}
}//END DATA IN
void DataManager(Packet p)
{
switch (p.packetType)
{
case Packet.PacketType.Registration:
id = p.Gdata[0];
break;
case Packet.PacketType.Request:
MessageBox.Show(p.ProductNumber + " Requested from " + p.senderID);
break;
}
}
private void btn_Accept_Click(object sender, EventArgs e)
{
MessageBox.Show("Sending acceptance of 126456");
Packet p = new Packet(Packet.PacketType.Accept, id);
p.ProductNumber = "123456";
master.Send(p.ToBytes());
}
}
}
This is my first dive into socket programming.
To start with your last question first, Receive will block until data becomes available unless you specify a timeout. Since your threads are foreground threads, this will prevent your application from terminating. See https://msdn.microsoft.com/en-us/library/8s4y8aff(v=vs.110).aspx. Either use a timeout, and/or make your threads background threads causing them to terminate when you close your application's main thread. Set the created thread's IsBackground property to true to achieve this. (Also, in the article above notice the paragraph about Shutdown and the Receive method returning an empty array. This is your hint to gracefully close the connection on your side).
The TCP/IP stack will send data at its own discretion (Nagle's algorithm), meaning you'll occasionally receive a buffer containing several or partial messages. Since you have "silent" error handling in your thread, perhaps your thread terminates prematurely because of a corrupted message? Place everything you receive in a buffer and check the buffer for complete messages in a separate step/thread before passing them on to your message handler.
No clear answers here I'm afraid, but if the check for corrupted messages doesn't help, look at the Socket samples on MSDN. It's probably just a tiny detail you're missing.
you are making a fundamental and common TCP error. TCP is a byte oriented streaming protocol, not message oriented. Your receive code assumes that it receives one Packet when it reads. This is not guaranteed, you might receive 1 byte, 20 bytes, or whatever. You must loop in the receive till you get all of one message. This means you have to know when you have read it all. Either there needs to be a header or some sentinel at the end.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've programmed a TCP/IP server with c# .NET Sockets. Using local network it works fine, but when i try to use it over the internet the client can't connect to the server.
I'm sure i've opened my port (14999) in the router and in windows firewall(both, client and server) and i also mapped my computer port 14999 to the one on the router.
Even with this i get "An existing co
nnection was forcibly closed by the remote host." When my client application try to connect over the internet to my server.
There is one thing i noticed .
When i have my server debugging with Visual Studio, and i use http://www.yougetsignal.com/tools/open-ports/ to check the 14999 port, the code hits a breakpoint.
I've been stucked with this ours, is there anybody who knows what can i do ?
Thanks a lot everyone!
Here is my client app code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
public class Person
{
public string name;
public int age;
}
// 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 = 14999;
// 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
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.GetHostEntry("MY PUBLIC IP");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(AddressFamily.Unspecified,
SocketType.Stream, ProtocolType.Tcp);
for (;;)
{
// Create a TCP/IP socket.
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
User user = new User() { name = "asdfasdfasdf", adress = "asdfasdfas", country = "asdfasdf", email = "example#example.com", locality = "asdfasdf", pass = "asdfasdf", state = "aasdfasd", surname = "asdfasdfasdf", telfNum = 123123 };
loginPublic login = new loginPublic() { email = "example#example.com", pass = "asdfasdfasdfas" };
accion accion = new accion() { act = 2, data = login };
var die = new JavaScriptSerializer().Serialize(accion);
//string guy = SPHFS.EncryptStringAES(die, "HFSIsAwesome12#.");
string guy = die;
// Send test data to the remote device.
Send(client, guy);
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
respuesta Resp = new JavaScriptSerializer().Deserialize<respuesta>(response);
Console.WriteLine("Message : {0} and Result : {1}", Resp.Message, Resp.Result);
Thread.Sleep(100);
// Write the response to the console.
}
// Release the socket.
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
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());
Console.ReadLine();
}
}
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,
ReceiveCallback, state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
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,
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());
Console.ReadLine();
}
}
private static void Send(Socket client, String data)
{
for(;;)
{
try
{
// 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,
SendCallback, client);
break;
}
catch
{
}
}
}
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;
}
public class User
{
public string name { get; set; }
public string surname { get; set; }
public string adress { get; set; }
public string locality { get; set; }
public string country { get; set; }
public string state { get; set; }
public string email { get; set; }
public int telfNum { get; set; }
public string pass { get; set; }
public LicenceDAO licencia { get; set; }
}
public class LicenceDAO
{
public decimal payment;
public DateTime nextPayment;
public bool state;
public string administrator;
}
public class accion
{
public int act;
public string key;
public object data;
}
public class respuesta
{
public bool Result;
public string Message;
}
public class loginPublic
{
public string email;
public string pass;
}
}
}
You are having an issue because you are attempting to connect to a server on your local network via the external address using NAT (hairpinning). Normally you should connect to your server application using the internal address if available, external connections would still work using the external address through NAT. If this is just an issue for testing and your router does not support NAT hairpinning, NAT loopback, or NAT reflection, you can either look around for ways to get around the hairpin (usually by setting up your own DNS) or have someone help you test from an external connection.
I'm kind of new to this ask-answer thing, please let me know if I've done any mistakes. I have this server program, running on my laptop. Server is a console application;
class Program
{
static void Main(string[] args)
{
Server sv = new Server("192.168.2.69", 52025);
sv.OnClientAccept += new ClientAcceptHandler(sv_OnClientAccept);
sv.OnClientMessage += new ClientMessageHandler(sv_OnClientMessage);
sv.Start();
}
static void sv_OnClientMessage(Server s, System.Net.Sockets.TcpClient client, string message)
{
Console.WriteLine("Message received from client " + GetClientIp(client) + ": \"" + message + "\"");
string msgToAll = message;
s.SendToAllClients(msgToAll);
Console.WriteLine("Message sent: \"" + message + "\" to the clients:");
foreach (TcpClient cl in s.Clients)
{
Console.WriteLine(GetClientIp(cl));
}
}
static void sv_OnClientAccept(Server s, System.Net.Sockets.TcpClient client)
{
Console.WriteLine("Client Accepted : " + GetClientIp(client));
}
static string GetClientIp(TcpClient Client)
{
return ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString();
}
}
The Server class;
public delegate void ClientAcceptHandler(Server s, TcpClient client);
public delegate void ClientMessageHandler(Server s, TcpClient client, string message);
public class Server
{
private List<TcpClient> clients = new List<TcpClient>();
object lockObjc;
private TcpListener server;
private string ip;
private int port;
private Thread accepter, listener;
public List<TcpClient> Clients { get { return this.clients; } set { this.clients = value; } }
public event ClientAcceptHandler OnClientAccept;
public event ClientMessageHandler OnClientMessage;
public Server(string IP, int Port)
{
ip = IP;
port = Port;
}
private void ClientAccepted(TcpClient Client)
{
if (OnClientAccept != null)
{
OnClientAccept.Invoke(this, Client);
}
}
private void ClientMessaged(TcpClient Client, string Message)
{
if (OnClientMessage != null)
{
OnClientMessage.Invoke(this, Client, Message);
}
}
public void Start()
{
lockObjc = new object();
IPAddress ipAddress = IPAddress.Parse(ip);//127.0.0.1 -> local || "192.168.2.69" -> laptop
server = new TcpListener(ipAddress, port);//2014 -> local || 52025
server.Start();
accepter = new Thread(AcceptClients);
accepter.Start();
listener = new Thread(Listen);
listener.Start();
}
private void AcceptClients()
{
while (true)
{
TcpClient attachedCl = server.AcceptTcpClient();
if (attachedCl != null)
{
lock (lockObjc)
{
if (!clients.Contains(attachedCl))
{
clients.Add(attachedCl);
ClientAccepted(attachedCl);
}
}
}
}
}
private void Listen()
{
while (true)
{
for (int i = 0; i < clients.Count; i++)
{
TcpClient client = clients[i];
lock (lockObjc)
{
try
{
StreamReader clientIn = new StreamReader(client.GetStream());
string msg = clientIn.ReadLine();
if (msg != string.Empty)
{
ClientMessaged(client, msg);
}
}
catch
{
}
}
}
}
}
private bool send(TcpClient Client, string Msg)
{
bool b = true;
try
{
TcpClient client = Client;
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
clientOut.WriteLine(Msg);
}
catch
{
b = false;
}
return b;
}
public bool SendToACLient(TcpClient Client, string Message)
{
return send(Client, Message);
}
public bool SendToAClient(int i, string Message)
{
return send(clients[i], Message);
}
public bool SendToAllClients(string Message)
{
bool b = true;
for (int i = 0; i < clients.Count; i++)
{
b = b && send(clients[i], Message);
}
return b;
}
public bool SendToAllClientsExcept(TcpClient Client, string Message)
{
int x = clients.IndexOf(Client);
bool b = true;
for (int i = 0; i < clients.Count; i++)
{
if (i != x)
{
b = b && send(clients[i], Message);
}
}
return b;
}
}
Since I'm really new to server-client relationships, I don't know if the server part is OK, but it seems OK to me programmatically (But I'm also amateur programer, no real education).
And the client part is;
public delegate void ServerMessageHandler(Client Receiver, string Message);
public class Client
{
private StreamReader clientIn;
private StreamWriter clientOut;
private TcpClient client;
private Thread listener;
private object lockObj;
public event ServerMessageHandler OnServerMessage;
public bool Connected { get { return (client != null && client.Connected); } }
public Client()
{
lockObj = new object();
client = new TcpClient();
listener = new Thread(Listen);
}
private void ServerMessaged(string Message)
{
if (OnServerMessage != null)
{
OnServerMessage.Invoke(this, Message);
}
}
private void Listen()
{
string serverMsg;
while (true)
{
try
{
serverMsg = clientIn.ReadLine();
if (serverMsg != string.Empty)
{
ServerMessaged(serverMsg);
}
}
catch
{
}
}
}
private void start()
{
listener.Start();
}
public bool Connect(string Ip, int Port)
{
client.Connect(Ip, Port);//"192.168.2.69", 52025
if (client.Connected)
{
clientIn = new StreamReader(client.GetStream());
clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
start();
}
return client.Connected;
}
public void Send(string Message)
{
if (client.Connected)
{
clientOut.WriteLine(Message);
}
}
}
Here's the form that I run Client class;
public partial class Form1 : Form
{
string s;
Client c;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
c = new Client();
c.OnServerMessage += new ServerMessageHandler(c_OnServerMessage);
bool b = c.Connect("192.168.2.69", 52025);
if (b)
{
s += "Connected to the server: 192.168.2.69:52025\r\n";
}
timer1.Start();
}
void c_OnServerMessage(Client Receiver, string Message)
{
s += "Server: " + Message + "\r\n";
}
private void button1_Click(object sender, EventArgs e)
{
c.Send(textBox1.Text);
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox2.Text = s;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
c.Send(textBox1.Text);
}
}
}
This form has two textboxes, first one is the one which takes user input (textBox1), the second one is the one which shows server responds. Why I'm not using Console? Because I've tried it in a way which I was waiting an input from user (Console.ReadLine) which as far as I know locks the console until user presses enter, even tough I'm using a thread for listening the server, I wasn't able to show the server answer on console.
So, I decided to create my own console (sort-of). The reason I run a timer to show messages is I get an error when I try to change the textbox text from a method that is being called by a thread (which is the c_OnServerMessage method, being called, indirectly, from the Listener thread from Client class)
But the problem I get here is this: I run the server on my laptop, I also run one client on my laptop which goes perfectly fine until I open up another client on another computer. At that point things go this way: I send a message from a client, the message reaches the server and is sent back. When I send another message, that message does not reach the server until I send a message from Client 2. And when I send a message from Client 2, the server gets the message from Client 2 even tough I sent the message from Client 1 first.
So basically what happens is, if I run 2 clients, I cannot send 2 messages in a row from a client, it kind of acts like a chess game where there is a turn for playing. I really don't understand the reason.
This problem is probably from a basic mistake but I had to write this whole message to be able to express myself. Thank you for your patient and your helps.
EDIT:
Problem solved thanks to this: TCP server with multiple Clients
My problem was the Listen method from Server class. The Listen method is being called by a thread, but still it runs a for loop in itself. Which if loop stops and waits for something, thread wouldn't make any difference. Thanks again, i think i will keep question, because i do not know what should happen to solved problems.
I am trying to learn more about sockets and threading in c#.
I have come across a lot of good resources online to help get me started.
The program I made so far, is a simple "man-in-the-middle" application.
It's designed as the following: client <--> [application] <--> server
Given the following code, how can I prevent this thread from running at 100% CPU?
How can I have the thread wait and block for data and not exit when the client / server is idle?
while (true)
{
lock (ClientState)
{
checkConnectionStatus(client, server);
}
if (clientStream.CanRead && clientStream.DataAvailable)
{
Byte[] bytes = new Byte[(client.ReceiveBufferSize)];
IAsyncResult result = clientStream.BeginRead(bytes, 0, client.ReceiveBufferSize, null, null);
int size = clientStream.EndRead(result);
sendData(bytes, serverStream, size);
}
if (serverStream.CanRead && serverStream.DataAvailable)
{
Byte[] bytes = new byte[(server.ReceiveBufferSize)];
IAsyncResult result = serverStream.BeginRead(bytes, 0, server.ReceiveBufferSize, null, null);
int size = serverStream.EndRead(result);
sendData(bytes, clientStream, size);
}
}
EDIT: Decided to post the entire "Connection.cs" class for anyone interested. I'm a beginner programming, so I know there are some bad coding practices here. Basically this whole class is ran in another thread and should die when the connection (to either the client socket or server socket) drops.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
namespace TCPRelay
{
public class Connection
{
public delegate void delThreadSafeHandleException(System.Exception ex);
public delegate void ConnectionDelegate(Connection conn);
public int DataGridIndex;
Main pMain;
public TcpClient client { get; set; }
public TcpClient server { get; set; }
public String ClientState { get; set; }
public string ListenPort { get; set; }
public string remotePort { get; set; }
public string listenAddress { get; set; }
public string remoteAddress { get; set; }
private TcpListener service { get; set; }
private Main Form
{
get
{
return pMain;
}
}
private NetworkStream clientStream { get; set; }
private NetworkStream serverStream { get; set; }
public Connection(TcpClient client, TcpClient server)
{
clientStream = client.GetStream();
serverStream = server.GetStream();
}
public Connection(String srcAddress, int srcPort, String dstAddress, int dstPort, Main caller)
{
try
{
pMain = caller;
TcpListener _service = new TcpListener((IPAddress.Parse(srcAddress)), srcPort);
//Start the client service and add to connection property
_service.Start();
service = _service;
//Set other useful parameters
listenAddress = srcAddress;
ListenPort = srcPort.ToString();
remoteAddress = dstAddress;
remotePort = dstPort.ToString();
this.ClientState = "Listening";
}
catch (Exception ex)
{
pMain.HandleException(ex);
Thread.CurrentThread.Abort();
}
}
private TcpClient getServerConnection(String address, int port)
{
TcpClient client = new TcpClient(address, port);
if (client.Connected)
{
return client;
}
else
{
throw new Exception(
String.Format("Unable to connect to {0} on port {0}",
address,
port)
);
}
}
private void sendData(Byte[] databuf, NetworkStream stream, int size)
{
bool waiting = true;
while (waiting)
{
if (stream.CanWrite)
{
waiting = false;
stream.Write(databuf, 0, size);
}
else { throw new Exception("Unable to write to network stream"); }
}
}
//Main Looping and data processing goes here
public void ProcessClientRequest()
{
try
{
//Wait for a connection to the client
TcpClient client = service.AcceptTcpClient();
//Get the streams and set the peer endpoints
this.clientStream = client.GetStream();
this.client = client;
//Now that we have a client, lets connect to our server endpoint
TcpClient server = getServerConnection(remoteAddress, int.Parse(remotePort));
//Set some useful parameters
this.server = server;
this.serverStream = server.GetStream();
}
catch (Exception ex)
{
lock (ClientState)
{
this.ClientState = ex.Message;
}
CloseConnection();
Thread.CurrentThread.Abort();
}
while (true)
{
lock (ClientState)
{
checkConnectionStatus(client, server);
}
if (clientStream.CanRead && clientStream.DataAvailable)
{
Byte[] bytes = new Byte[(client.ReceiveBufferSize)];
IAsyncResult result = clientStream.BeginRead(bytes, 0, client.ReceiveBufferSize, null, null);
int size = clientStream.EndRead(result);
sendData(bytes, serverStream, size);
}
if (serverStream.CanRead && serverStream.DataAvailable)
{
Byte[] bytes = new byte[(server.ReceiveBufferSize)];
IAsyncResult result = serverStream.BeginRead(bytes, 0, server.ReceiveBufferSize, null, null);
int size = serverStream.EndRead(result);
sendData(bytes, clientStream, size);
}
}
}
private void checkConnectionStatus(TcpClient _client, TcpClient _server)
{
try
{
if (_client.Client.Poll(0, SelectMode.SelectRead))
{
byte[] buff = new byte[1];
if (_client.Client.Receive(buff, SocketFlags.Peek) == 0)
{
this.ClientState = "Closed";
CloseConnection();
Thread.CurrentThread.Abort();
}
}
else if (_server.Client.Poll(0, SelectMode.SelectRead))
{
byte[] buff = new byte[1];
if (_server.Client.Receive(buff, SocketFlags.Peek) == 0)
{
this.ClientState = "Closed";
CloseConnection();
Thread.CurrentThread.Abort();
}
}
else { this.ClientState = "Connected"; }
}
catch (System.Net.Sockets.SocketException ex)
{
this.ClientState = ex.SocketErrorCode.ToString();
CloseConnection();
Thread.CurrentThread.Abort();
}
}
public void CloseConnection()
{
if (clientStream != null)
{
clientStream.Close();
clientStream.Dispose();
}
if (client != null)
{
client.Close();
}
if (serverStream != null)
{
serverStream.Close();
serverStream.Dispose();
}
if (server != null)
{
server.Close();
}
if (service != null)
{
service.Stop();
}
}
}
}
I also have a "Main" form and a "ConnectionManager" class that i'm playing around with.
The most efficient way of handling this would be to issue a read with a callback, on each stream.
After issuing both reads, sit waiting forever on an object that you use to signal that the thread should stop its work (a ManualResetEvent is the traditional one to use - can be used to signal many threads at once).
When data is received, the OS will call your callback function(s), and you would do your processing in there and then (importantly) queue another read.
This means that your thread is forever idle, waiting on a signal object that tells it that it's time to go away (in a "wake up - time to die" kind of way), and is only ever doing work when the OS tells it that there is data to process.
To be REALLY friendly, you would also do the writes asynchronously, so that one connection cannot starve the other of processing time (in the current implementation, if one write blocks, the other stream never gets serviced).
Finally, to be super good, you would encapsulate this behaviour in an object that takes as a parameter the stream to use, and then simply instantiate two of them, instead of having two streams and doing everything twice in the main code.
After accepting the socket in the middle man I do the following:
private void WaitForData()
{
try
{
if (socketReadCallBack == null)
{
socketReadCallBack = new AsyncCallback(OnDataReceived);
}
ReceiveState rState = new ReceiveState();
rState.Client = mySocket;
mySocket.BeginReceive(rState.Buffer, 0, rState.Buffer.Length, SocketFlags.None,
new AsyncCallback(socketReadCallBack), rState);
}
catch (SocketException excpt)
{
// Process Exception
}
}
Receive State is:
public class ReceiveState
{
public byte[] Buffer = new byte[1024]; //buffer for network i/o
public int DataSize = 0; //data size to be received by the server
public bool DataSizeReceived = false; //whether prefix was received
public MemoryStream Data = new MemoryStream(); //place where data is stored
public Socket Client; //client socket
}
Once data is received my routine "OnDataReceived" processes it. I'm not experiencing any CPU problems with this.
Same code used for both the client and middleman.