Hi.
I am having problems with the TCPClient Class in .NET (1.1)
If i connect to the server app from either mobile device or the Windows Client application, it of course connects successfully assuming the server is up and running.
However, if i quit/stop the service of the server app and try to connect from the client application - you would expect for it not to work. However, it does! It seems to connect but the problem starts when you send messages to the connection destination (when the destination app is closed/service has been stopped)
When sending the data to the Server (when its offline) it will of course throw an exception which I handle (unable to process communication or something...)
However, if i step through in the debugger - it acts as it should!! It will through an exception when it tries to establish a connection to the Server when the Server is offline.
Does anyone know why It means I cannot tell the user honestly if the app has connected to the server properly.
The way my app works:
----------
[server]
Listens for incoming communication and acts up on it from the messages sent in bytes[] (using the tcp listener class)
Responds to Client commands (such as "am I connected to the server " - in this case Server will send the command back "yes you are conneted to the server successfully")
[client]
Checks if destination exists using the TCPClient class.
If it exists, then it automatically connects when your are making the object (TCPClient)
Sends data when it successfully "connects" to server when making the object. It sends a string, for example, that tells the server "oh, im the client. Do you have anything to say "
Just then the Server will respond to the message
----------
I just do not understand why, when you send data initially from client to server, it sends it but after that when you send more data it throws the error. Surely it should tell me if it has connected to the destination successfully when you are creating the TCPClient object or is that only to see if it can successfully connect to the destination (computer, port)
is there a way to also check to see if it has successfully, and truthfully, connected to the server application, instead of telling fibs then the 2nd time round when the client assumes its connected, it will throw an error stating that it cannot process the data
Thank-you :)

Connecting problem
D.O.
however here is this code:
//ConnManClass
public void DoConnectServer()
{
this.theCommunicationManager.ClientProtocol = new System.Net.Sockets.TcpClient(this.TheConnectionSettings.GetServerName(), this.TheConnectionSettings.GetPortNumber()); byte[] msgToSend = GetBytes("[-]CConnect[-]");
this.theCommunicationManager.SendData(msgToSend);
}
//CommMan
public void SendData(byte[] dataToSendToDestination)
{
this.theNetworkStream = this.theClientProtocol.GetStream();
if(this.theNetworkStream.CanWrite)
{
Thread.Sleep(200);
this.theNetworkStream.Write(theDataToSend, 0, theDataToSend.Length);
this.theNetworkStream.Flush();
}
}
pretty much that.
When the server application server has not been started (in other words its not listening) it seems to send the "CConnect" data fine until you send any other message after that... it then produces an exception (naturally) to say that it could not write to the stream, simply because the server is not listening/does not exist
however when I step through the debugger - the first time it sends the message "CConnect" - it produces the error straight away, as it should!
as the network configuration physically - I have a wireless router (using wired cable) and of course it has a firewall built in but the port I am wanting to connect to is open for the server app to "listen" to that port.
tcorrigan
On my machine, I get the exception
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
at Client.Main()
I don't know how you could be connecting to a server when it is not listening.
If you continue to experience the problem capture a network trace between the machines and see what kind of TCP frames are being exchanged.
Here is my server code
using
System;using
System.Net;using
System.Net.Sockets;using
System.IO;using
System.Text;public
class Server{
public static void Main(){
try{
IPAddress LocalIP =
null;IPHostEntry HostEntry = Dns.GetHostEntry(Dns.GetHostName());
foreach(IPAddress IP in HostEntry.AddressList){
if(IP.AddressFamily == AddressFamily.InterNetwork){
LocalIP = IP;
break;}
}
TcpListener Server =
new TcpListener(LocalIP, 6000);Server.Start();
TcpClient Client = Server.AcceptTcpClient();
Console.WriteLine("Connected....");NetworkStream ClientStream = Client.GetStream();
byte[] buffer = new byte[256]; int Read = ClientStream.Read(buffer, 0, buffer.Length); Console.WriteLine("Read from Client: {0}", Encoding.ASCII.GetString(buffer, 0, Read)); Console.WriteLine("Press Enter to End"); Console.ReadLine();}
catch (Exception ex){
Console.WriteLine(ex);}
}
}
Here is my client
using
System;using
System.Net;using
System.Net.Sockets;using
System.IO;using
System.Text;public
class Client{
public static void Main(){
try{
TcpClient Client =
new TcpClient("dgortilt", 6000); Console.WriteLine("Connected....");NetworkStream ClientStream = Client.GetStream();
String s = "-Hello From Client-"; byte[] buffer = Encoding.ASCII.GetBytes(s);ClientStream.Write(buffer, 0, buffer.Length);
Console.WriteLine("Press Enter to End"); Console.ReadLine();}
catch (Exception ex){
Console.WriteLine(ex);}
}
}
Jesús López
Could you please post a simple sample code that demonstrates this behavior
Thanks
D Berrett
Kurre A
Would appreciate any reply
however the temp solution I have come up with is to do a "timeout" period.. using a timer. if within x seconds it does not recieve the first msg from the server, it will notify the user
but i would like to do it more profesionally and also would like to know what the solution to this problem is...