C# Net Sockets multiple transactions problem, with source.

T

Ted

Hello all,

Please could somebody with a little time on their
hands help me with my net sockets program?

The aim is to send multiple transactions via C#
network stream and read them back. The source
is designed to send a 50byte header and read
the result (50bytes). For one run this is fine,
however since i'll be using multiple transactions
i set it up with a windowsform button so i can
test sending the same transaction as many times
as i'd like, and hopefully get the same result.

However, when sending the second transaction (the
same as the first), it fails. I'm not sure if
this is my CICS driver refusing the second
transaction because i sent the same transaction
twice, or my program? Hopefully the former, although
I'm sure I made some test code previously that
worked fine and proved otherwise.

Could someone take a look for me and provide some
advice on the network stream. Most of the code
in this program is borrowed and tailored for my
purposes, if it comes in handy for anyone else
trying a similar thing then cool, but dont thank
me etc.

If you do a search for 'screwing' in the code
you'll find the bit where it fails and catches
an exception. Note i've only been programming in
C# for a week or so, so not entirely sure how
to trace through yet.

Here's the code..
www.amusement-arcade.com/netsock.cs

The code should be compilable, although you'll
need an IP address to connect to, and that's
behind a company firewall, so no good to
yourself, hence, i've hidden it and the port
as x.x.x.x etc..

Best regards,
Jode
Please reply to andrewsj@**removethisspambit**uk.ibm.com
if not in this post.
 
W

William Stacey

Not totally sure as have not tried to run the code as would need to build a
server to send to,etc. However, I you are creating the stream (using
GetStream) on each call to the method. I wonder if after it goes out of
scope the first time, if that is not part of your problem. Try creating a
private form Stream var (like your tcpclient var) and assign it once and use
the stream from then on until you close the stream and tcpclient. Failing
that, I would remove cics from the diag picture and setup tcpListener server
to test against. You could also post the Exception message your getting
(catch and display it) to help us with a little more info. hth
 
T

Ted

If I catch the exceptions and display the messages then
the output looks like this (after three runs)..

::Connecting to RS4
::Connected to x.x.x.x:xxxx
::Sending Transaction : SDTC,TEST SDI FROM MY CODE ,IC,
--SDTC started SD on CICS SYSID PRE8.
::Sending Transaction : SDTC,TEST SDI FROM MY CODE ,IC,
Unable to write data to the transport connection.
::Sending Transaction : SDTC,TEST SDI FROM MY CODE ,IC,
Operation not allowed on non-connected sockets.
 
T

Ted

Thanks William, i thought that too, but i couldnt' work
out how to only create the stream once and for it to
still have visibility to sendTransaction.

Defining it as..

public class WinForm : System.Windows.Forms.Form
{
TcpClient sdiClient = new TcpClient();
Stream networkStream = sdiClient.GetStream();

Fails as 'WinForm.sdiClient denotes a field where a class is expected'.

Which is fair enough I guess, could you let me know how
I should define it?

Regards, Jode
 
S

Sami Vaaraniemi

I spotted one problem in the code. This may or may not be the real problem
here but I think you should fix it anyway. The problem is on this line:

int k=networkStream.Read(returnedOutput,0,returnedOutput.Length);

You are attempting to read returnedOutput.Length bytes from the network
buffer. Due to the nature of TCP streaming the call might return anything
between 1 to returnedOutput.Length bytes.

To fix the problem, call networkStream.Read in a loop until you have
received all the data.

Sami
www.capehill.net
 
W

William Stacey [MVP]

// Private field declarations, visible in all instance methods.
private TcpClient sdiClient;
private Stream networkStream;

public MyClass() //Constructor.
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

sdiClient = new TcpClient();
networkStream = sdiClient.GetStream();
}

As a side note, you may not want to bind your network logic so closely to
your form. Another option would be to create another class that encapulates
the your network logic and the TcpClient and stream in a seperate network
class. You may then have Send and Receive methods on it (et al.) This may
make it easier to share this code with other forms and/or classes (even
console versions of your program.) Also, Sami was right on with the Read
method. You need to loop on receive. Many examples of this in the Help.
hth
 
T

Ted

Sami Vaaraniemi said:
I spotted one problem in the code. This may or may not be the real problem
here but I think you should fix it anyway. The problem is on this line:

int k=networkStream.Read(returnedOutput,0,returnedOutput.Length);

You are attempting to read returnedOutput.Length bytes from the network
buffer. Due to the nature of TCP streaming the call might return anything
between 1 to returnedOutput.Length bytes.

To fix the problem, call networkStream.Read in a loop until you have
received all the data.

Sami
www.capehill.net


Hello both,

Not sure where my earlier reply went to you Sami, however, you're right
i should read it in a loop, but in this case I know that 50bytes are
on the way back to me, when i start running transactions properly the
returning reply tells me within the first 23bytes how long the stream
is anyway. I'll fix this up later

William, thanks for the tip, i've altered the code, and although it
gets further, it errors during a read (even if i attempt to read just
one byte).

The source is www.amusement-arcade.com/netsock.cs
and the output is www.amusement-arcade.com/output.jpg

It looks like i'll have to set up a host and see whatsa goin on, unless
you can spot something else.

Regards, Jode
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top