2

PS: sorry my English I can understand but i'm not so good to write. corrections are very welcome

First of all, I read some answers here and already know my problem... Well I'm here because I'll need to make some pained changes into my server if there is no other solution ...

Here we go.

I have a server and a client listening and answering in the same port. Inside my server, I have only one thread that reads, processes and sends the result. No problem here, it's fine, but my client has multiple threads that is doing the same, and it's causing wrong messages like: one thread send a message and wait the answer, other thread send other message and the first thread capt it as an answer, so the real answer of the first is gives to the second, so all 2 receive wrong messages and cause a big confusion on client.

I'm almost sure that I'll need to use a port to read and one to write or a semaphore, but if I can get around it, it will be very helpful. Any ideas?

My communication class:

public SenderAndRequester(string ipAdress, int port)
{
    client = new TcpClient();
    IPEndPoint ip_end = new IPEndPoint(IPAddress.Parse(ipAdress), port);
    client.Connect(ip_end);
    if (client.Connected)
    {
        stw = new StreamWriter(client.GetStream());
        str = new StreamReader(client.GetStream());
        stw.AutoFlush = true;
        str.DiscardBufferedData();
    }
}
public string communicate(string message)
{
    var comming = str.ReadLineAsync();
    stw.WriteLine(message);
    return comming.Result;
}

and here the class that uses it

 public MyConstructor(){
            com = new Communicator(new SenderAndRequester(ip, port));

            while (!com.InitServer(firstVar,secondVar,...)) ;
            //code continue ...
            mnt = new Task(Tracker, ctsMonitor.Token,    TaskCreationOptions.LongRunning);
            mnt.Start();
        }

class main thread ...

private bool nextStatus()
{
    //code continue..

        if (!com.RequestNewStatus())
        {
            _Error = com.Error + " on communicator";
            return false;
        }
        status = com.ServerStatus; 
        // code continue ...
        return true;

    }

and one of various other threads

private void Tracker()
{
    while (!ctsMonitor.IsCancellationRequested)
    {
        //code continue
        refresh = com.RequestCriticalData();
        Thread.Sleep(100);
    }
}
Nicollas Braga
  • 802
  • 7
  • 27
  • It doesn't sound to me like you need to use multiple ports but that you need to add some logic to your clients to tag your requests & responses with some identifier that would let the server know which client to send the responses to. – Mike Dinescu May 30 '15 at 00:38
  • You need to queue the outbound messages from the client. Since you are running more than a single thread, you have no control over the order in which the client sends the messages, unless you queue them up. – Johnathon Sullinger May 30 '15 at 01:38
  • As Mike mentioned, you could insert a guid or something to uniquely identify the contents. Send it to the server, then have the server process it. When the server is finished, it sends the results back with the same guid. This lets the client know what data the response belongs to. – Johnathon Sullinger May 30 '15 at 01:43
  • But i have the same buffer to read and write, and if i'm waiting and other thread ask, that message will be written in the buffer and taken by the listener of same buffer, don't going to server and make a wrong answer to client listener, maybe separate in tag as u guys talked, ticket+message and add an rule to only get the same ticket. it wouldn't block the buffer to receive written back? how can i control it ?Add every ask to an array and go sending? – Nicollas Braga May 30 '15 at 03:33

0 Answers0