473,394 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

TcpClient Connect woes

Jim
I'm working on my first .net project using c#. I've been using
activex controls embedded in a web page and wanted to do the same in
..net. My approach has been to create a windows control library and
then reference the dll with a web page as follows:

<html>
<body>
<center>
<object id="UserControl1" height="500" width="700"
classid="http://wiffle/WindowsControlLibrary3.dll#WindowsControlLibrary3. UserControl1">
</object>
</center>
</body>
</html>

A small form seems to work pretty well. Please let me know if this is
not the best way to do the same as an activex control within a web
page.

Having met with success thus far, I decided to attempt to call a web
service with a soap call. We have an IBM product called Redback which
can act as a web service, so I created a SOAP call to it. From
talking to IBM support, I learned that the web service needs to be
called with http/1.0. Sadly there is a bug in the .net framework
where http/1.0 acts like http/1.1 and waits for a 100-continue after
posting the header before posting the body.
http://support.microsoft.com/default...b;en-us;327885 There
is a hotfix if you go through microsoft's gauntlet, but I didn't
figure it was a good idea to write a production application which
depends on applying a hotfix. Strangely, the hotfix came out in 2002,
yet isn't included in the .net framework version 1.1. So I decided to
punt on the SOAP method unless someone has a suggestion for getting
around the problem.

I currently connect to a windows service from my activex controls
which in turn talks to redback. This works great and is very fast, so
I thought I'd try to get a .net application talking to it. I wrote an
object in c# to take care of the communications with my windows
service thinking I could eventually re-write the object to talk SOAP
once things are working. Invoking the object from a windows form
connects and works great. I then added the object to a windows
control library solution. It works, but the connect takes 2-3 minutes
to complete. After the initial connect, it's quite fast, but the
connect wait time is too long. I've seen posts about
TcpClient.Connect being slow from within a windows control library,
but I haven't seen a solution. Under .net 1.0, my windows control
library didn't have permission to make a tcp connection, so my guess
is that the extra wait time is being caused by .net security checks.
So I'm basically at strike 2 with no third option. Here is the code
for my object if that helps. I'd love to hear any suggestions.

namespace WindowsApplication15
{
/// <summary>
/// Summary description for RBIntf.
/// </summary>
public class RBIntf
{
TcpClient cl;
public ParamLst Parameters = new ParamLst();
public RtnLst Returns = new RtnLst();
string imethod;
string ihost;
int iport;
public string method
{
get { return this.imethod; }
set { this.imethod = value; }
}
public string host
{
get { return this.ihost; }
set { this.ihost = value; }
}
public int port
{
get { return iport; }
set { iport = value; }
}
public RBIntf(string host,int port)
{
this.ihost = host;
this.iport = port;
// this is where it hangs for 2-3 minutes
// within a windows control library
// it doesn't have when part of a windows form
cl = new TcpClient(this.ihost,this.iport);
NetworkStream stm = this.cl.GetStream();
// wait for initial connect message
byte[] data = new byte[this.cl.ReceiveBufferSize];
int recv;
string stArray;
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
stArray = "";
while (stm.DataAvailable == false);
do
{
recv = stm.Read(data, 0, data.Length);
stArray = stArray + ascii.GetString(data, 0, recv);
}
while (stArray.Substring(stArray.Length-2,2) != "\r\n");
}
public string GetRtn(string IName)
{
string rval;
rval = "";
for (int i=0;i <= Returns.RtnList.Count-1;i++)
{
Rtn R;
R = (Rtn)Returns.RtnList[i];
if (R.GetName() == IName)
{
rval = R.GetValue();
}
}
return rval;
}
public void Execute()
{
string RBCmd;
RBCmd = "M`" + this.method;
for (int i=0;i <= Parameters.ParamList.Count-1;i++)
{
Parm P;
P = (Parm)Parameters.ParamList[i];
RBCmd = RBCmd + "`P`" + P.GetName() + "`" + P.GetValue();
}
for (int i=0;i <= Returns.RtnList.Count-1;i++)
{
Rtn R;
R = (Rtn)Returns.RtnList[i];
RBCmd = RBCmd + "`R`" + R.GetName();
}
RBCmd = RBCmd + "\r\n";
NetworkStream stm = this.cl.GetStream();
byte[] data = new byte[this.cl.ReceiveBufferSize];
int recv;
string stArray;
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
// now send the execute command
byte[] arrData = System.Text.Encoding.ASCII.GetBytes(RBCmd);
stm.Write(arrData,0,RBCmd.Length);
// wait for the command response
while (stm.DataAvailable == false);
stArray = "";
while (stm.DataAvailable == false);
do
{
recv = stm.Read(data, 0, data.Length);
stArray = stArray + ascii.GetString(data, 0, recv);
}
while (stArray.Substring(stArray.Length-2,2) != "\r\n");
// now parse the response
int p;
string Nm;
string Vl;
while (stArray != "")
{
p = stArray.IndexOf("`");
Nm = stArray.Substring(0,p);
stArray = stArray.Substring(p+1,stArray.Length-p-1);
p = stArray.IndexOf("`");
if (p == -1)
{
Vl = stArray;
stArray = "";
}
else
{
Vl = stArray.Substring(0,p);
stArray = stArray.Substring(p+1,stArray.Length-p-1);
}
for (int i=0;i <= Returns.RtnList.Count-1;i++)
{
Rtn R;
R = (Rtn)Returns.RtnList[i];
if (R.GetName() == Nm)
{
R.SetValue(Vl);
}
}
}
}
}
public class ParamLst
{
public ArrayList ParamList = new ArrayList();
public void paramlst()
{
}
public void Add(string Name,string Value)
{
Parm ParmObj = new Parm (Name,Value);
ParamList.Add(ParmObj);
}
public void Clear()
{
ParamList.Clear();
}
}
public class Parm
{
string Name;
string Value;
public Parm(string IName,string IValue)
{
this.Name = IName;
this.Value = IValue;
}
public string GetName()
{
return this.Name;
}
public string GetValue()
{
return this.Value;
}
}

public class RtnLst
{
public ArrayList RtnList = new ArrayList();
public void rtnlst()
{
}
public void Add(string Name)
{
Rtn RtnObj = new Rtn (Name);
RtnList.Add(RtnObj);
}
public void Clear()
{
RtnList.Clear();
}
}
public class Rtn
{
string Name;
string Value;
public Rtn(string IName)
{
this.Name = IName;
this.Value = "";
}
public string GetName()
{
return this.Name;
}
public string GetValue()
{
return this.Value;
}
public void SetValue(string IValue)
{
this.Value = IValue;
}
}
}
Jul 21 '05 #1
0 2907

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

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
5
by: Horst Walter | last post by:
What is wrong here? IPAddress ipAddress = IPAddress.Parse("10.10.20.1"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port); this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE...
0
by: Evan Freeman[C++ Samuri] | last post by:
Recently I found a question online that originated from this group, and those who responded to it were of no help to the poster. So I am sorry that I was not watching this group before, but will...
4
by: WATYF1 | last post by:
Hello. I'm writing a VB.NET app to check email message counts for both POP3 and IMAP4. I'm using TCPClient to connect, and a NetworkStream to send simple commands. It's a very simple bit of code,...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
0
by: Jim | last post by:
I'm working on my first .net project using c#. I've been using activex controls embedded in a web page and wanted to do the same in ..net. My approach has been to create a windows control library...
0
by: zhangke007 | last post by:
Hello, everyone, Currently, I have an simple serial communication application using the serialnet.dll tool from Franson company. What this application does is to read the data through the com...
10
by: Jan Vinten | last post by:
Hi all, I got into some trouble trying to bind to a specific IP address. Code: Private mobjClient As TcpClient mobjClient = New TcpClient(IPAddress.Parse("10.16.104.87").ToString, 8892)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.