I would like to to create a server application that listens to a remote client via TCPlistener and output the message to a form.
What I've done so far is :
1) in the infrastructure
class
public class TcpListenerAndClient
{
int port;
private Socket tcpSocket = null;
public TcpListener peerListener ;
public TcpListenerAndClient(int Port)
{
this.port = Port;
}
public void OpenTcpListener()
{
peerListener = new TcpListener(IPAddress.Any, port);
peerListener.Start();
}
public void AcceptSocket()
{
tcpSocket = peerListener.AcceptSocket();
}
}
2) in the domain class
public class Peer2PeerCom
{
public Thread ListenerThread ;
int port = 0;
public infraStructure.TcpListenerAndClient TraficPerfServer;
public Peer2PeerCom(int Port)
{
this.port = Port;
TraficPerfServer = new infraStructure.TcpListenerAndClient(port);
}
public void CreateListenr_InAnotherThread()
{
ListenerThread = new Thread(new ThreadStart(socketListener));
ListenerThread.Name = "Listener Thread";
ListenerThread.Priority = ThreadPriority.AboveNormal;
ListenerThread.Start();
}
//public infraStructure.TcpListenerAndClient Tcptool = new infraStructure.TcpListenerAndClient(port);
/// <summary>
/// void listener to be used in thread
/// </summary>
public void socketListener()
{
TraficPerfServer.OpenTcpListener();
while (true)
{
if (!TraficPerfServer.peerListener.Pending())
{
Thread.Sleep(500);
continue;
}
TraficPerfServer.AcceptSocket();
//once we get here , there is message to collect -> need to show it in GUI
}
}
3) in the control class
public void ConnectToIP(int port , string client/**IPAdd**/)
{
StartListenr = new domain.Peer2PeerCom(port);
// set up a Listener in another THread
if (!isConnected)
{
isConnected = true;
StartListenr.CreateListenr_InAnotherThread();
}
}
Now I have to make the GUI fetch the result.
My initial idea is to use this design layer in the design layer (GUI->control->domain->infra)
.
I know I need some kind of data structure that will store the message in the control
(?) and an EVENT
in the GUI
class that will look for the change, but I can't wrap my head around it.