I'm trying to use a socket (System.Net.Socket, even tried TcpListener/Client/Etc.) to be able to listen for data, while it's waiting to or already sending data.
I've done the following:
public byte[] bytesIn;
public byte[] bytesOut;
public Socket transmitter;
public Socket receiver;
public Comlink(String ipAddress, int portNum)
{
try
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress), 11000);
transmitter = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
transmitter.Listen(10);
receiver = transmitter.Accept();
receiver.Receive(bytesIn);
try
{
transmitter.Connect(remoteEP);
this.bytesOut = Encoding.ASCII.GetBytes("<SYNCUP>");
int bytesSent = transmitter.Send(this.bytesOut);
this.connected = true;
}
} // (Omitted my exception handling.)
}
The Receive() function:
this.bytesIn = new byte[1024];
Socket receiver = transmitter.Accept();
int bytesReceived = receiver.Receive(this.bytesIn);
Code that binds it:
while(comlink.connected)
{
comlink.Receive();
handlePacket(comlink.bytesIn);
}
I'm just not entirely sure if I'm using the right process to receive data, as that's always been the complaint from .NET. I need data to be handled as it comes in.
Something like a two-way street: Data can freely come in (without the program having to move back to Receive() and wait for the data), it gets handled. Data can move freely from the client and to the server, as well. I know TCP sockets are bidirectional, but I need to improve this class so that I can enjoy the full benefits of having a free, open road.