By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,484 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,484 IT Pros & Developers. It's quick & easy.

socket questions

P: n/a
i am working on basically a proxy server that handles requests via remoting
from clients and executes transactions against a third-party server via TCP.

the remoting site works like a champ. my problem is executing the
transactions against the remote server and returning the response to the
remoting client. i can open the socket fine and, if i am executing one
transaction at a time, everything works great. it's when my proxy server
tries to execute multiple transactions, i'm missing something on the socket
end. i have tried both synchronous and asynchronous, but it just seems that
the socket stops listening after one response is received and ignores the
other pending transactions.

i've tried a number of different approaches, and posted messages on the
forums.microsoft board. i have to believe that someone, somewhere has build
a transaction-type system like this one. but most of the samples and advice
that i have received have all pointed to a server listening for and accepting
multiple clients, instead of one application connecting to a remote server
with a number of concurrent (client) connections.

if anyone has any useful links or suggestions, it would be appreciated.
thanks in advance.
Dec 5 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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" <co************@discussions.microsoft.com> wrote in message
news:75**********************************@microsof t.com...
i am working on basically a proxy server that handles requests via remoting
from clients and executes transactions against a third-party server via
TCP.

the remoting site works like a champ. my problem is executing the
transactions against the remote server and returning the response to the
remoting client. i can open the socket fine and, if i am executing one
transaction at a time, everything works great. it's when my proxy server
tries to execute multiple transactions, i'm missing something on the
socket
end. i have tried both synchronous and asynchronous, but it just seems
that
the socket stops listening after one response is received and ignores the
other pending transactions.

i've tried a number of different approaches, and posted messages on the
forums.microsoft board. i have to believe that someone, somewhere has
build
a transaction-type system like this one. but most of the samples and
advice
that i have received have all pointed to a server listening for and
accepting
multiple clients, instead of one application connecting to a remote server
with a number of concurrent (client) connections.

if anyone has any useful links or suggestions, it would be appreciated.
thanks in advance.

Dec 5 '05 #2

P: n/a
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <co************@discussions.microsoft.com> wrote in message
news:75**********************************@microsof t.com...
i am working on basically a proxy server that handles requests via remoting
from clients and executes transactions against a third-party server via
TCP.

the remoting site works like a champ. my problem is executing the
transactions against the remote server and returning the response to the
remoting client. i can open the socket fine and, if i am executing one
transaction at a time, everything works great. it's when my proxy server
tries to execute multiple transactions, i'm missing something on the
socket
end. i have tried both synchronous and asynchronous, but it just seems
that
the socket stops listening after one response is received and ignores the
other pending transactions.

i've tried a number of different approaches, and posted messages on the
forums.microsoft board. i have to believe that someone, somewhere has
build
a transaction-type system like this one. but most of the samples and
advice
that i have received have all pointed to a server listening for and
accepting
multiple clients, instead of one application connecting to a remote server
with a number of concurrent (client) connections.

if anyone has any useful links or suggestions, it would be appreciated.
thanks in advance.


Dec 5 '05 #3

P: n/a
> i've actually tried a number of different approaches, which i will try to

synchronous or asynchronous doesnt matter, if the server is just programmed
to accept one connection than it'll just accpt one connection. It could take
multiple connections but maybe its not programmed that way. Have you
contacted the person who built the server? I think the way it used to work
serially, as you said, its working that same way using tcp and may not take
concurrent users/connections.
Just for your information, If I have to program a server to accept multiple
clients I do something like this:

while ( true )
{
TcpClient client = listening_socket.AcceptTcpClient(); //blocking call
// ... do processing with client, maybe open it in a new thread for
communicating, and come back to listening mode.

}

But it seems that they accept a connection, than communicates with the
client and when the client disconnects 'than' go into listening state
again, so to do one transaction at a time.

Ab.
http://joehacker.blogspot.com

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <co************@discussions.microsoft.com> wrote in message news:75**********************************@microsof t.com...
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP.

the remoting site works like a champ. my problem is executing the
transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server tries to execute multiple transactions, i'm missing something on the
socket
end. i have tried both synchronous and asynchronous, but it just seems that
the socket stops listening after one response is received and ignores the other pending transactions.

i've tried a number of different approaches, and posted messages on the forums.microsoft board. i have to believe that someone, somewhere has
build
a transaction-type system like this one. but most of the samples and
advice
that i have received have all pointed to a server listening for and
accepting
multiple clients, instead of one application connecting to a remote server with a number of concurrent (client) connections.

if anyone has any useful links or suggestions, it would be appreciated. thanks in advance.


Dec 6 '05 #4

P: n/a
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <co************@discussions.microsoft.com> wrote in
message
news:75**********************************@microsof t.com...
>i am working on basically a proxy server that handles requests via
>remoting
> from clients and executes transactions against a third-party server via
> TCP.
>
> the remoting site works like a champ. my problem is executing the
> transactions against the remote server and returning the response to
> the
> remoting client. i can open the socket fine and, if i am executing one
> transaction at a time, everything works great. it's when my proxy
> server
> tries to execute multiple transactions, i'm missing something on the
> socket
> end. i have tried both synchronous and asynchronous, but it just seems
> that
> the socket stops listening after one response is received and ignores
> the
> other pending transactions.
>
> i've tried a number of different approaches, and posted messages on the
> forums.microsoft board. i have to believe that someone, somewhere has
> build
> a transaction-type system like this one. but most of the samples and
> advice
> that i have received have all pointed to a server listening for and
> accepting
> multiple clients, instead of one application connecting to a remote
> server
> with a number of concurrent (client) connections.
>
> if anyone has any useful links or suggestions, it would be appreciated.
> thanks in advance.


Dec 6 '05 #5

P: n/a
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <co************@discussions.microsoft.com> wrote in
message
news:75**********************************@microsof t.com...
>i am working on basically a proxy server that handles requests via
>remoting
> from clients and executes transactions against a third-party server via
> TCP.
>
> the remoting site works like a champ. my problem is executing the
> transactions against the remote server and returning the response to
> the
> remoting client. i can open the socket fine and, if i am executing one
> transaction at a time, everything works great. it's when my proxy
> server
> tries to execute multiple transactions, i'm missing something on the
> socket
> end. i have tried both synchronous and asynchronous, but it just seems
> that
> the socket stops listening after one response is received and ignores
> the
> other pending transactions.
>
> i've tried a number of different approaches, and posted messages on the
> forums.microsoft board. i have to believe that someone, somewhere has
> build
> a transaction-type system like this one. but most of the samples and
> advice
> that i have received have all pointed to a server listening for and
> accepting
> multiple clients, instead of one application connecting to a remote
> server
> with a number of concurrent (client) connections.
>
> if anyone has any useful links or suggestions, it would be appreciated.
> thanks in advance.


Dec 6 '05 #6

P: n/a
their server does accept multiple connections. i can watch my transactions
being processed on their server, its just that my client (when it tries to
send multiple transactions and wait for a response) doesn't handle the
responses properly (their server tries to send the responses, my client just
stops listening, or whatever).

"Abubakar" wrote:
i've actually tried a number of different approaches, which i will try to


synchronous or asynchronous doesnt matter, if the server is just programmed
to accept one connection than it'll just accpt one connection. It could take
multiple connections but maybe its not programmed that way. Have you
contacted the person who built the server? I think the way it used to work
serially, as you said, its working that same way using tcp and may not take
concurrent users/connections.
Just for your information, If I have to program a server to accept multiple
clients I do something like this:

while ( true )
{
TcpClient client = listening_socket.AcceptTcpClient(); //blocking call
// ... do processing with client, maybe open it in a new thread for
communicating, and come back to listening mode.

}

But it seems that they accept a connection, than communicates with the
client and when the client disconnects 'than' go into listening state
again, so to do one transaction at a time.

Ab.
http://joehacker.blogspot.com

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:B2**********************************@microsof t.com...
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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" <co************@discussions.microsoft.com> wrote in message news:75**********************************@microsof t.com...
>i am working on basically a proxy server that handles requests via remoting > from clients and executes transactions against a third-party server via > TCP.
>
> the remoting site works like a champ. my problem is executing the
> transactions against the remote server and returning the response to the > remoting client. i can open the socket fine and, if i am executing one > transaction at a time, everything works great. it's when my proxy server > tries to execute multiple transactions, i'm missing something on the
> socket
> end. i have tried both synchronous and asynchronous, but it just seems > that
> the socket stops listening after one response is received and ignores the > other pending transactions.
>
> i've tried a number of different approaches, and posted messages on the > forums.microsoft board. i have to believe that someone, somewhere has
> build
> a transaction-type system like this one. but most of the samples and
> advice
> that i have received have all pointed to a server listening for and
> accepting
> multiple clients, instead of one application connecting to a remote server > with a number of concurrent (client) connections.
>
> if anyone has any useful links or suggestions, it would be appreciated. > thanks in advance.


Dec 6 '05 #7

P: n/a
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:B2**********************************@microsof t.com...
> 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[i];
> Array.Copy(buffer,0,response,0,i);
> //Console.WriteLine("Out of Transact : " +
> Comcast.Common.Encoding.HexEncoder.ToString(respon se));
> 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.
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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" <co************@discussions.microsoft.com> wrote in
>> message
>> news:75**********************************@microsof t.com...
>> >i am working on basically a proxy server that handles requests via
>> >remoting
>> > from clients and executes transactions against a third-party server
>> > via
>> > TCP.
>> >
>> > the remoting site works like a champ. my problem is executing the
>> > transactions against the remote server and returning the response to
>> > the
>> > remoting client. i can open the socket fine and, if i am executing
>> > one
>> > transaction at a time, everything works great. it's when my proxy
>> > server
>> > tries to execute multiple transactions, i'm missing something on the
>> > socket
>> > end. i have tried both synchronous and asynchronous, but it just
>> > seems
>> > that
>> > the socket stops listening after one response is received and
>> > ignores
>> > the
>> > other pending transactions.
>> >
>> > i've tried a number of different approaches, and posted messages on
>> > the
>> > forums.microsoft board. i have to believe that someone, somewhere
>> > has
>> > build
>> > a transaction-type system like this one. but most of the samples
>> > and
>> > advice
>> > that i have received have all pointed to a server listening for and
>> > accepting
>> > multiple clients, instead of one application connecting to a remote
>> > server
>> > with a number of concurrent (client) connections.
>> >
>> > if anyone has any useful links or suggestions, it would be
>> > appreciated.
>> > thanks in advance.
>>
>>
>>


Dec 6 '05 #8

P: n/a
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);
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:B2**********************************@microsof t.com...
> 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[i];
> Array.Copy(buffer,0,response,0,i);
> //Console.WriteLine("Out of Transact : " +
> Comcast.Common.Encoding.HexEncoder.ToString(respon se));
> 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.
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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" <co************@discussions.microsoft.com> wrote in
>> message
>> news:75**********************************@microsof t.com...
>> >i am working on basically a proxy server that handles requests via
>> >remoting
>> > from clients and executes transactions against a third-party server
>> > via
>> > TCP.
>> >
>> > the remoting site works like a champ. my problem is executing the
>> > transactions against the remote server and returning the response to
>> > the
>> > remoting client. i can open the socket fine and, if i am executing
>> > one
>> > transaction at a time, everything works great. it's when my proxy
>> > server
>> > tries to execute multiple transactions, i'm missing something on the
>> > socket
>> > end. i have tried both synchronous and asynchronous, but it just
>> > seems
>> > that
>> > the socket stops listening after one response is received and
>> > ignores
>> > the
>> > other pending transactions.
>> >
>> > i've tried a number of different approaches, and posted messages on
>> > the
>> > forums.microsoft board. i have to believe that someone, somewhere
>> > has
>> > build
>> > a transaction-type system like this one. but most of the samples
>> > and
>> > advice
>> > that i have received have all pointed to a server listening for and
>> > accepting
>> > multiple clients, instead of one application connecting to a remote
>> > server
>> > with a number of concurrent (client) connections.
>> >
>> > if anyone has any useful links or suggestions, it would be
>> > appreciated.
>> > thanks in advance.
>>
>>
>>


Dec 6 '05 #9

P: n/a
Hi,

Are ou sure that the server does support multithreading? and it does it
correctly?

Maybe it's the server where the problems lies.
Also make sure you know the server's protocol
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
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);
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:C3**********************************@microsof t.com...
>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.
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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
>>
>> "coloradowebdev" <co************@discussions.microsoft.com> wrote in
>> message
>> news:B2**********************************@microsof t.com...
>> > 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[i];
>> > Array.Copy(buffer,0,response,0,i);
>> > //Console.WriteLine("Out of Transact : " +
>> > Comcast.Common.Encoding.HexEncoder.ToString(respon se));
>> > 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.
>> >
>> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
>> >
>> >> 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" <co************@discussions.microsoft.com> wrote
>> >> in
>> >> message
>> >> news:75**********************************@microsof t.com...
>> >> >i am working on basically a proxy server that handles requests via
>> >> >remoting
>> >> > from clients and executes transactions against a third-party
>> >> > server
>> >> > via
>> >> > TCP.
>> >> >
>> >> > the remoting site works like a champ. my problem is executing
>> >> > the
>> >> > transactions against the remote server and returning the response
>> >> > to
>> >> > the
>> >> > remoting client. i can open the socket fine and, if i am
>> >> > executing
>> >> > one
>> >> > transaction at a time, everything works great. it's when my
>> >> > proxy
>> >> > server
>> >> > tries to execute multiple transactions, i'm missing something on
>> >> > the
>> >> > socket
>> >> > end. i have tried both synchronous and asynchronous, but it just
>> >> > seems
>> >> > that
>> >> > the socket stops listening after one response is received and
>> >> > ignores
>> >> > the
>> >> > other pending transactions.
>> >> >
>> >> > i've tried a number of different approaches, and posted messages
>> >> > on
>> >> > the
>> >> > forums.microsoft board. i have to believe that someone,
>> >> > somewhere
>> >> > has
>> >> > build
>> >> > a transaction-type system like this one. but most of the samples
>> >> > and
>> >> > advice
>> >> > that i have received have all pointed to a server listening for
>> >> > and
>> >> > accepting
>> >> > multiple clients, instead of one application connecting to a
>> >> > remote
>> >> > server
>> >> > with a number of concurrent (client) connections.
>> >> >
>> >> > if anyone has any useful links or suggestions, it would be
>> >> > appreciated.
>> >> > thanks in advance.
>> >>
>> >>
>> >>
>>
>>
>>


Dec 6 '05 #10

P: n/a
i am pretty sure it does. there is a logging application on the server that
monitors the transactions, and i can see them all coming in and responding,
to the point where if i send 4 transactions and 2 of them take 15 seconds and
2 of them take 2 seconds, i see all 4 requests coming in, then the 2 "2
second" ones responding and then the 2 "15 second" ones responding. the
server uses sequence numbers so that's how i am matching them up. i've also
had a network sniffer set up and can actually see the responses coming back.

the server uses TCP, if that is what you mean. its set up to listen to a
specific port, and if i run a netstat -a (on the client and remote server) i
can see the actual connections with the established ports.

if at the base level my TcpClient should send and receive only on its own
connection, maybe its my threading that isn't working properly. in my head,
i should be able to set up 100 threads, each with its own TcpClient, and each
should send then receive without any overlapping, right?

i appreciate you input on this.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Are ou sure that the server does support multithreading? and it does it
correctly?

Maybe it's the server where the problems lies.
Also make sure you know the server's protocol
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
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);
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:C3**********************************@microsof t.com...
>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.
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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
>>
>> "coloradowebdev" <co************@discussions.microsoft.com> wrote in
>> message
>> news:B2**********************************@microsof t.com...
>> > 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[i];
>> > Array.Copy(buffer,0,response,0,i);
>> > //Console.WriteLine("Out of Transact : " +
>> > Comcast.Common.Encoding.HexEncoder.ToString(respon se));
>> > 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.
>> >
>> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
>> >
>> >> 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" <co************@discussions.microsoft.com> wrote
>> >> in
>> >> message
>> >> news:75**********************************@microsof t.com...
>> >> >i am working on basically a proxy server that handles requests via
>> >> >remoting

Dec 6 '05 #11

P: n/a
at this point i'm guessing its just my logic for threading. i did a quick
console application that opened 10 threads that read a web page from a
server, and it behaved as i expected: the GET messages went out, and the
output was displayed as it was received, with all the threads intermingled.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MultiThreadedClient
{
class Program
{
static void Main(string[] args)
{
Thread myThread;
RequestBroker r = new
RequestBroker(IPAddress.Parse("10.153.11.85"));

for (int i = 0; i < 10; i++)
{
myThread = new Thread(new ThreadStart(r.Connect));
myThread.Start();
}
}
private class RequestBroker
{
private IPAddress remoteHost;
public AutoResetEvent ConnectedToAllHosts = new
AutoResetEvent(false);
private int connections;

public RequestBroker(IPAddress remoteHost)
{
this.remoteHost = remoteHost;
connections = 0;
}

public void Connect()
{
TcpClient c = new TcpClient();
int connection = Interlocked.Increment(ref this.connections)
- 1;
try
{
Console.WriteLine("Connecting #" + connection.ToString());
c.Connect(remoteHost, 80);
NetworkStream netStream = c.GetStream();
string request = "GET / HTTP/1.0" + Environment.NewLine
+ Environment.NewLine;
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Console.WriteLine(request);
netStream.Write(bytesSent, 0, bytesSent.Length);

int bytes;
byte[] bytesReceived = new byte[256];
bool done = false;
do
{
bytes = netStream.Read(bytesReceived, 0,
bytesReceived.Length);
if (bytes > 0)
{
Console.WriteLine("Read #" +
connection.ToString() + " for " + bytes.ToString() + " bytes.");
Console.Write("Reading #" +
connection.ToString() + Encoding.ASCII.GetString(bytesReceived, 0, bytes));
Console.WriteLine("Done Reading #" +
connection.ToString());
}

}
while (!done);

}
catch (Exception exception)
{
Console.WriteLine("Exception: " + exception.Message);
}
Console.WriteLine("Closing #" + connection.ToString());
c.Close();
}
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Are ou sure that the server does support multithreading? and it does it
correctly?

Maybe it's the server where the problems lies.
Also make sure you know the server's protocol
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
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);
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:C3**********************************@microsof t.com...
>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.
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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
>>
>> "coloradowebdev" <co************@discussions.microsoft.com> wrote in
>> message
>> news:B2**********************************@microsof t.com...
>> > 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[i];
>> > Array.Copy(buffer,0,response,0,i);
>> > //Console.WriteLine("Out of Transact : " +
>> > Comcast.Common.Encoding.HexEncoder.ToString(respon se));
>> > 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.
>> >
>> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
>> >
>> >> 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" <co************@discussions.microsoft.com> wrote
>> >> in
>> >> message
>> >> news:75**********************************@microsof t.com...
>> >> >i am working on basically a proxy server that handles requests via
>> >> >remoting

Dec 6 '05 #12

P: n/a
Hi,
"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
. i've also
had a network sniffer set up and can actually see the responses coming
back.
Were they correct?
the server uses TCP, if that is what you mean. its set up to listen to a
specific port, and if i run a netstat -a (on the client and remote server)
i
can see the actual connections with the established ports.
No, I mean to what application protocol it uses, the format of the message
it send/receive
if at the base level my TcpClient should send and receive only on its own
connection, maybe its my threading that isn't working properly. in my
head,
i should be able to set up 100 threads, each with its own TcpClient, and
each
should send then receive without any overlapping, right?
Correctly.

The one part I do not like of your code is :

netStream.Read(buffer,0, 256);

Try this, read a single byte at a time and just store it in your buffer,
also what happens if the answer is longer than 256? in your code there is
nothing to prevent/treat this.

Change it like this:
ArrayList bytes = new ArrayList();
byte[] buffer = new byte[1];
while( true )
{
netstream.Read( buffer, 0, 1 )
bytes.Add( buffer[0] )
if ( IsCR ( buffer[0] )
break; // .... do your stuff
}
//return the value
return bytes.ToArray( typeof( byte) );

If CR constains more than one char you have to change a little bit it. but
the above should work,
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
i appreciate you input on this.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Are ou sure that the server does support multithreading? and it does it
correctly?

Maybe it's the server where the problems lies.
Also make sure you know the server's protocol
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"coloradowebdev" <co************@discussions.microsoft.com> wrote in
message
news:C5**********************************@microsof t.com...
> 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);
> }
>
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> 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
>>
>>
>>
>> "coloradowebdev" <co************@discussions.microsoft.com> wrote in
>> message
>> news:C3**********************************@microsof t.com...
>> >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.
>> >
>> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
>> >
>> >> 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
>> >>
>> >> "coloradowebdev" <co************@discussions.microsoft.com> wrote
>> >> in
>> >> message
>> >> news:B2**********************************@microsof t.com...
>> >> > 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[i];
>> >> > Array.Copy(buffer,0,response,0,i);
>> >> > //Console.WriteLine("Out of Transact : " +
>> >> > Comcast.Common.Encoding.HexEncoder.ToString(respon se));
>> >> > 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.
>> >> >
>> >> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
>> >> >
>> >> >> 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" <co************@discussions.microsoft.com>
>> >> >> wrote
>> >> >> in
>> >> >> message
>> >> >> news:75**********************************@microsof t.com...
>> >> >> >i am working on basically a proxy server that handles requests
>> >> >> >via
>> >> >> >remoting

Dec 7 '05 #13

P: n/a
Is the server available publicly? Can I program against it just to check?

Ab.

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
their server does accept multiple connections. i can watch my transactions being processed on their server, its just that my client (when it tries to
send multiple transactions and wait for a response) doesn't handle the
responses properly (their server tries to send the responses, my client just stops listening, or whatever).

"Abubakar" wrote:
i've actually tried a number of different approaches, which i will try to

synchronous or asynchronous doesnt matter, if the server is just programmed to accept one connection than it'll just accpt one connection. It could take multiple connections but maybe its not programmed that way. Have you
contacted the person who built the server? I think the way it used to work serially, as you said, its working that same way using tcp and may not take concurrent users/connections.
Just for your information, If I have to program a server to accept multiple clients I do something like this:

while ( true )
{
TcpClient client = listening_socket.AcceptTcpClient(); //blocking call // ... do processing with client, maybe open it in a new thread for
communicating, and come back to listening mode.

}

But it seems that they accept a connection, than communicates with the
client and when the client disconnects 'than' go into listening state
again, so to do one transaction at a time.

Ab.
http://joehacker.blogspot.com

"coloradowebdev" <co************@discussions.microsoft.com> wrote in message news:B2**********************************@microsof t.com...
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[i];
Array.Copy(buffer,0,response,0,i);
//Console.WriteLine("Out of Transact : " +
Comcast.Common.Encoding.HexEncoder.ToString(respon se));
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.

"Ignacio Machin ( .NET/ C# MVP )" wrote:

> 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" <co************@discussions.microsoft.com> wrote in

message
> news:75**********************************@microsof t.com...
> >i am working on basically a proxy server that handles requests via

remoting
> > from clients and executes transactions against a third-party
server via
> > TCP.
> >
> > the remoting site works like a champ. my problem is executing the
> > transactions against the remote server and returning the response
to the
> > remoting client. i can open the socket fine and, if i am
executing one
> > transaction at a time, everything works great. it's when my proxy

server
> > tries to execute multiple transactions, i'm missing something on
the > > socket
> > end. i have tried both synchronous and asynchronous, but it just

seems
> > that
> > the socket stops listening after one response is received and ignores the
> > other pending transactions.
> >
> > i've tried a number of different approaches, and posted messages
on the
> > forums.microsoft board. i have to believe that someone, somewhere
has > > build
> > a transaction-type system like this one. but most of the samples and > > advice
> > that i have received have all pointed to a server listening for and > > accepting
> > multiple clients, instead of one application connecting to a

remote server
> > with a number of concurrent (client) connections.
> >
> > if anyone has any useful links or suggestions, it would be

appreciated.
> > thanks in advance.
>
>
>


Dec 8 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.