473,734 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

socket questions

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.microsof t 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
13 2645
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.mi crosoft.com> wrote in message
news:75******** *************** ***********@mic rosoft.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.microsof t 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
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(bu ffer, 0, sock.Available, SocketFlags.Non e);
byte[] response = new byte[i];
Array.Copy(buff er,0,response,0 ,i);
//Console.WriteLi ne("Out of Transact : " +
Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
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.mi crosoft.com> wrote in message
news:75******** *************** ***********@mic rosoft.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.microsof t 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
> 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_socke t.AcceptTcpClie nt(); //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.mi crosoft.com> wrote in message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
byte[] response = new byte[i];
Array.Copy(buff er,0,response,0 ,i);
//Console.WriteLi ne("Out of Transact : " +
Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
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.mi crosoft.com> wrote in message news:75******** *************** ***********@mic rosoft.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.microsof t 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
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.Synchroni zed( unsyncq);

TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start( );
while( true)
{
socket = listener.Accept TcpClient();
connectionQueue .Enqueue( socket);

workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
workingthread.S tart();
}
}

public void TheConnectionHa ndler()
{

TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();

}
Cheers,

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

"coloradowebdev " <co************ @discussions.mi crosoft.com> wrote in message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
byte[] response = new byte[i];
Array.Copy(buff er,0,response,0 ,i);
//Console.WriteLi ne("Out of Transact : " +
Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
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.mi crosoft.com> wrote in
message
news:75******** *************** ***********@mic rosoft.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.microsof t 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
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.Synchroni zed( unsyncq);

TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start( );
while( true)
{
socket = listener.Accept TcpClient();
connectionQueue .Enqueue( socket);

workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
workingthread.S tart();
}
}

public void TheConnectionHa ndler()
{

TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();

}
Cheers,

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

"coloradowebdev " <co************ @discussions.mi crosoft.com> wrote in message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
byte[] response = new byte[i];
Array.Copy(buff er,0,response,0 ,i);
//Console.WriteLi ne("Out of Transact : " +
Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
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.mi crosoft.com> wrote in
message
news:75******** *************** ***********@mic rosoft.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.microsof t 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
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_socke t.AcceptTcpClie nt(); //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.mi crosoft.com> wrote in message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
byte[] response = new byte[i];
Array.Copy(buff er,0,response,0 ,i);
//Console.WriteLi ne("Out of Transact : " +
Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
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.mi crosoft.com> wrote in message news:75******** *************** ***********@mic rosoft.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.microsof t 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
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.mi crosoft.com> wrote in message
news:C3******** *************** ***********@mic rosoft.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.Synchroni zed( unsyncq);

TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start( );
while( true)
{
socket = listener.Accept TcpClient();
connectionQueue .Enqueue( socket);

workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
workingthread.S tart();
}
}

public void TheConnectionHa ndler()
{

TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();

}
Cheers,

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

"coloradowebdev " <co************ @discussions.mi crosoft.com> wrote in
message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
> byte[] response = new byte[i];
> Array.Copy(buff er,0,response,0 ,i);
> //Console.WriteLi ne("Out of Transact : " +
> Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
> 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.mi crosoft.com> wrote in
>> message
>> news:75******** *************** ***********@mic rosoft.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.microsof t 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
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)...al so, 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(ob ject data)
{
TcpClient client = null;
NetworkStream netStream = null;

try
{
//TransFlag.Reset ();

Console.WriteLi ne("In client thread");
TCPBaseSettings cs = TCPSettings();
client = new TcpClient(cs.Ho st, cs.Port);
netStream = client.GetStrea m();
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,Buffer Size);
if (bytesRcvd > 0)
{
Console.WriteLi ne("Received Bytes: " +
bytesRcvd.ToStr ing());
if (ContainsCR(buf fer))
{
Console.WriteLi ne("Buffer contains CR");
Console.WriteLi ne("Thread: " +
Thread.CurrentT hread.Name);
RxBytes = new byte[bytesRcvd];
Array.Copy(buff er, 0, RxBytes, 0, bytesRcvd);
//TransFlag.Set() ;
hasresponse = true;
break;
}
else
{
//Console.WriteLi ne("Buffer does not contain CR");
}
}
}

}
catch (Exception ex)
{
Console.WriteLi ne("ClientThrea d Exception: " + ex.Message);
//throw ex;
}
finally
{
netStream.Close ();
client.Close();
}
Console.WriteLi ne("ClientThrea d Exiting: " +
Thread.CurrentT hread.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.mi crosoft.com> wrote in message
news:C3******** *************** ***********@mic rosoft.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.Synchroni zed( unsyncq);

TcpClient socket;
TcpListener listener = new TcpListener( Config.Port);
listener.Start( );
while( true)
{
socket = listener.Accept TcpClient();
connectionQueue .Enqueue( socket);

workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
workingthread.S tart();
}
}

public void TheConnectionHa ndler()
{

TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();

}
Cheers,

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

"coloradowebdev " <co************ @discussions.mi crosoft.com> wrote in
message
news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
> byte[] response = new byte[i];
> Array.Copy(buff er,0,response,0 ,i);
> //Console.WriteLi ne("Out of Transact : " +
> Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
> 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.mi crosoft.com> wrote in
>> message
>> news:75******** *************** ***********@mic rosoft.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.microsof t 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
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.mi crosoft.com> wrote in message
news:C5******** *************** ***********@mic rosoft.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)...al so, 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(ob ject data)
{
TcpClient client = null;
NetworkStream netStream = null;

try
{
//TransFlag.Reset ();

Console.WriteLi ne("In client thread");
TCPBaseSettings cs = TCPSettings();
client = new TcpClient(cs.Ho st, cs.Port);
netStream = client.GetStrea m();
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,Buffer Size);
if (bytesRcvd > 0)
{
Console.WriteLi ne("Received Bytes: " +
bytesRcvd.ToStr ing());
if (ContainsCR(buf fer))
{
Console.WriteLi ne("Buffer contains CR");
Console.WriteLi ne("Thread: " +
Thread.CurrentT hread.Name);
RxBytes = new byte[bytesRcvd];
Array.Copy(buff er, 0, RxBytes, 0, bytesRcvd);
//TransFlag.Set() ;
hasresponse = true;
break;
}
else
{
//Console.WriteLi ne("Buffer does not contain
CR");
}
}
}

}
catch (Exception ex)
{
Console.WriteLi ne("ClientThrea d Exception: " + ex.Message);
//throw ex;
}
finally
{
netStream.Close ();
client.Close();
}
Console.WriteLi ne("ClientThrea d Exiting: " +
Thread.CurrentT hread.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.mi crosoft.com> wrote in
message
news:C3******** *************** ***********@mic rosoft.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.Synchroni zed( unsyncq);
>>
>> TcpClient socket;
>> TcpListener listener = new TcpListener( Config.Port);
>> listener.Start( );
>> while( true)
>> {
>> socket = listener.Accept TcpClient();
>> connectionQueue .Enqueue( socket);
>>
>> workingthread = new Thread( new ThreadStart( TheConnectionHa ndler));
>> workingthread.S tart();
>> }
>> }
>>
>> public void TheConnectionHa ndler()
>> {
>>
>> TcpClient socket= (TcpClient)conn ectionQueue.Deq ueue();
>>
>> }
>>
>>
>> Cheers,
>>
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>> "coloradowebdev " <co************ @discussions.mi crosoft.com> wrote in
>> message
>> news:B2******** *************** ***********@mic rosoft.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(bu ffer, 0, sock.Available, SocketFlags.Non e);
>> > byte[] response = new byte[i];
>> > Array.Copy(buff er,0,response,0 ,i);
>> > //Console.WriteLi ne("Out of Transact : " +
>> > Comcast.Common. Encoding.HexEnc oder.ToString(r esponse));
>> > 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.mi crosoft.com> wrote
>> >> in
>> >> message
>> >> news:75******** *************** ***********@mic rosoft.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.microsof t 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1602
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown to client connected to it from a function through a separate thread. and echo all the connections opened and closed. the client script is working fine. it echoes the countdown it recieve from server.
4
2413
by: mfaujour | last post by:
I HAVE THIS PYTHON PROGRAMM: $ cat socpb.py import BaseHTTPServer, SocketServer, os, socket, threading # the server initialisation server = SocketServer.TCPServer((socket.gethostname(), 8088),BaseHTTPServer.BaseHTTPRequestHandler)
1
3077
by: Rolln_Thndr | last post by:
I'm vey new to network programing and have a few rather fundemental questions. I'm creating a very basic UDP proxy server and having a few issues regarding the sockets. Is it possible to change the properties of a socket WITHOUT closing it and creating a new one? Basically, I need to change the port of a bound socket, but the only way I have found to do this so far is to close the exisitng socket and create a new one. This in itself...
2
26516
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1");
5
3683
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
4
18130
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
0
1722
by: Markus S. | last post by:
Hello! I have two questions about Socket.Select(): 1) How is it possible to interrupt Socket.Select()? I'm calling Socket.Select() in a worker thread. When new sockets are added (from another thread) I want to interrupt the Select() so it can get restarted with the new sockets being added. This has to happen quickly so I can't wait for the timeout. Thread.Interrupt() seems to do nothing.
4
4871
by: seets375 | last post by:
Hi, I have two ethernet interfaces on my system, with IPs assigned to the interfaces from different subnets (e.g. eth1 - 10.10.10.10 and eth2 - 20.20.20.20 ). I'm connecting these interfaces to a home gateway or router. The 'eth1' interface connects to LAN interface of the router with IP address 10.10.10.1 and the 'eth2' interface connects to WAN interface of the router with IP address 20.20.20.1. I'm trying a simple socket program...
8
4680
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data using com component. It is just set of some characters like
6
3663
by: ahlongxp | last post by:
socket.makefile() may lose data when "connection reset by peer". and socket.recv() will never lose the data. change the "1" to "0" in the client code to see the difference. confirmed on both windows and linux. so I guess there is a problem with makefile(). # Echo server program
0
8948
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8780
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9246
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8190
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4816
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3264
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.