this is frustrating!
i tried creating a (more simple) client that attempts to send multiple
requests to the remote server. each request is sent and a response is
awaited in its own thread, with the theory being that since each thread
has
its own connection to the remote server, everything would work out.
the transactions are all making it to the server, but my client, again,
is
losing some of the responses. this is my client thread method. it
seems
straight forward: open a TcpClient, send something, wait for something
with
the same client, etc. I was using the ManualReset for handling
timeouts,
but
i'm not even worried about that right now (all of my calls to the
remote
server are returning something)...also, the messages returned from the
remote
server are terminated with 0x0d, so that is what i am using to know
that a
response is finished. since the thread handles one send and receive, i
let
it
terminate once the receive loop ends.
private void ClientThread(object data)
{
TcpClient client = null;
NetworkStream netStream = null;
try
{
//TransFlag.Reset();
Console.WriteLine("In client thread");
TCPBaseSettings cs = TCPSettings();
client = new TcpClient(cs.Host, cs.Port);
netStream = client.GetStream();
netStream.Write((byte[])data, 0, ((byte[])data).Length);
int totalBytesRcvd = 0;
int bytesRcvd = 0;
const int BufferSize = 256;
// Receive buffer.
byte[] buffer = new byte[BufferSize];
while (true)
{
bytesRcvd = netStream.Read(buffer,0,BufferSize);
if (bytesRcvd > 0)
{
Console.WriteLine("Received Bytes: " +
bytesRcvd.ToString());
if (ContainsCR(buffer))
{
Console.WriteLine("Buffer contains CR");
Console.WriteLine("Thread: " +
Thread.CurrentThread.Name);
RxBytes = new byte[bytesRcvd];
Array.Copy(buffer, 0, RxBytes, 0,
bytesRcvd);
//TransFlag.Set();
hasresponse = true;
break;
}
else
{
//Console.WriteLine("Buffer does not contain
CR");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("ClientThread Exception: " +
ex.Message);
//throw ex;
}
finally
{
netStream.Close();
client.Close();
}
Console.WriteLine("ClientThread Exiting: " +
Thread.CurrentThread.Name);
}
:
Hi,
You are right about the server side of the communication, sorry for
that
mistake. You can use it in case you need it.
Each connection use theirs own particular socket instance , even if
they
are connecting to the same remote host/port
in any case multithreading the client is the only possible way of
doing
this.
where i send something and wait for a response,
This depends of the protocol the server is using, do you know how it
handles the communication?
but i need to be able to do
that with 50 simultaneous calls.
Ok do this, get rid of the remoting part and just try your tcp code as
if
you were receiving several remoting requests.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
message
i tried separate threads, as well. the code you posted includes a
tcplistener, which is the server side, right? my proxy server
(which
is
actually a client at this point) will be the one initiating the
conversation
with the remote server, not listening for clients to connect, unless
i
am
missing something.
i was thinking about it (again) last night, and i think my biggest
problem
is that i am trying to make something serial that isn't. with my
COM
ports,
one port is open, handles the transaction, and is closed. with the
socket,
multiple connections use the same socket so it isn't as easy as
listening
to
one channel for a response. the responses seem to be stepping one
each
other
when responses are received. i think what i want is a synchronous
socket
call
where i send something and wait for a response, but i need to be
able
to
do
that with 50 simultaneous calls.
:
Hi,
Are you sure all your variables are local to each thread?
Cause I assume you are using threads when dealing with more than
one
connection no?
This is part of the code I use :
Thread listenerThread;
Queue connectionQueue= null;
protected void ListenerMethod()
{
Thread workingthread;
Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized( unsyncq);
TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start();
while( true)
{
socket = listener.AcceptTcpClient();
connectionQueue.Enqueue( socket);
workingthread = new Thread( new ThreadStart(
TheConnectionHandler));
workingthread.Start();
}
}
public void TheConnectionHandler()
{
TcpClient socket= (TcpClient)connectionQueue.Dequeue();
}
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
in
message
i've actually tried a number of different approaches, which i
will
try
to
summarize below...
synchronous: this works fine for one transaction, but sending any
more
than
one does not (send while waiting for a response from a previous
transaction
causes the read to fail (return nothing))
[Send Synchronous, then]
int i = sock.Receive(buffer, 0, sock.Available,
SocketFlags.None);
byte[] response = new byte
;
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(response));
return response;
asynchronous:
i stripped out the last version of asynchronous reading.
basically,
it
was
sending synchronously while listening asynchronously. when
something
was
received, if it contained the special terminating character, it
would
raise
an event with the server response as the event argument. the
code
was a
combination of asynchronous samples from the MSDN site and other
references
on the net that used BeginReceive.
I know it can't be THIS hard. I'm adding TCP functionality to the
application that was previously all serial based. the logic in
the
serial
based one was check if the port is available, if it is send, lock
the
port
until something is received, return the response, close the port.
i'm
not
a
big TCP/IP person, but i figure there should be some information
out
there
that would put me on the right path.
:
Hi,
post the code you are using to communicate with the remote TCP
server,
are
you using multithread? each thread generating a connection ?
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"coloradowebdev" <[email protected]>
wrote
in
message
i am working on basically a proxy server that handles requests
via
remoting