Click here to Skip to main content
15,891,777 members
Home / Discussions / C#
   

C#

 
GeneralRe: uint.Parse() vs. Convert.ToUint32() Pin
moredip13-Apr-03 0:38
moredip13-Apr-03 0:38 
GeneralRe: uint.Parse() vs. Convert.ToUint32() Pin
Stephane Rodriguez.13-Apr-03 1:15
Stephane Rodriguez.13-Apr-03 1:15 
GeneralRe: uint.Parse() vs. Convert.ToUint32() Pin
moredip13-Apr-03 1:23
moredip13-Apr-03 1:23 
GeneralRe: uint.Parse() vs. Convert.ToUint32() Pin
leppie13-Apr-03 3:00
leppie13-Apr-03 3:00 
GeneralRe: uint.Parse() vs. Convert.ToUint32() Pin
moredip13-Apr-03 3:01
moredip13-Apr-03 3:01 
GeneralTcpClient Problem Pin
Michal Januszczyk12-Apr-03 12:41
sussMichal Januszczyk12-Apr-03 12:41 
GeneralRe: TcpClient Problem Pin
Stephane Rodriguez.12-Apr-03 21:24
Stephane Rodriguez.12-Apr-03 21:24 
GeneralRe: TcpClient Problem Pin
Michal Januszczyk13-Apr-03 11:06
sussMichal Januszczyk13-Apr-03 11:06 
The Client.Connected boolean value allows me to find out whether _my_ code has closed the connection, not the remote side.

I've written a _very_ simple applications: Client and server:
If You can run this, You will see that the client, even after
closing the connection by the remote side can write to (or at least
it thinks so) or read from the stream.

The code:
Client:
static void Main(string[] args)
{
  TcpClient tcpClient = new TcpClient("localhost",3000);
  NetworkStream ns = tcpClient.GetStream();
  byte[] bytesOut = 	Encoding.Unicode.GetBytes("disconnect_me");
  ns.Write(bytesOut,0,bytesOut.Length);

  //now the server has closed TCP connection on its behalf
  //but the client tries to write something again. 
  //I think that some exception should be thrown at furher usage of 
  //the stream, or at least some fields should indicate that the 
  //remote side has ended the connection. 
  //The netstat application correctly identifies the situation 
  //(now instead of "established" , the connection is in 
  //CLOSE_WAIT state) BUT NO ERROR IS REPORTED WHEN  I WRITE TO (or 
  //read from) THE STREAM...
  ns.Write(bytesOut,0,bytesOut.Length);
}


The Server
static void Main(string[] args)
  {
  TcpListener listener = new TcpListener(3000);
  listener.Start();
  Console.WriteLine("The server is listening..");
  TcpClient tcpClient = listener.AcceptTcpClient();
  Console.WriteLine("Some client has connected to the server..");
  NetworkStream ns = tcpClient.GetStream();

  bool listening=true;
  const int MaxSize=100;
  int bytesRead=0;
  byte[] buffer = new Byte[MaxSize];
  while(listening)
  {
	bytesRead = ns.Read(buffer,0,MaxSize);
	string command = Encoding.Unicode.GetString(buffer,0,bytesRead);
	if(command.CompareTo("disconnect_me")==0)
	{
		Console.WriteLine("Client wants to be disconnected");
		listening=false;
	}
  }
  ns.Close(); //now close the stream and TCP connection
  tcpClient.Close();
  Console.WriteLine("The connection with client has been closed.");
  Console.ReadLine();
}


Thanks for any suggestions
GeneralCharacter Codes Pin
J. Dunlap12-Apr-03 10:52
J. Dunlap12-Apr-03 10:52 
GeneralRe: Character Codes Pin
Stephane Rodriguez.12-Apr-03 21:08
Stephane Rodriguez.12-Apr-03 21:08 
Generalreading double encoded by php pack() problem Pin
Member 28242012-Apr-03 10:42
Member 28242012-Apr-03 10:42 
GeneralRe: reading double encoded by php pack() problem Pin
Stephane Rodriguez.12-Apr-03 20:56
Stephane Rodriguez.12-Apr-03 20:56 
GeneralExtended File Properties Pin
Querulant12-Apr-03 8:55
Querulant12-Apr-03 8:55 
GeneralRe: Extended File Properties Pin
Stephane Rodriguez.12-Apr-03 20:50
Stephane Rodriguez.12-Apr-03 20:50 
GeneralThanks Pin
Querulant13-Apr-03 3:44
Querulant13-Apr-03 3:44 
GeneralUsing windows api in C# Pin
rm_babar12-Apr-03 6:40
rm_babar12-Apr-03 6:40 
GeneralRe: Using windows api in C# Pin
Stephane Rodriguez.12-Apr-03 20:46
Stephane Rodriguez.12-Apr-03 20:46 
GeneralRe: Using windows api in C# Pin
James T. Johnson13-Apr-03 5:28
James T. Johnson13-Apr-03 5:28 
GeneralQuestion about Web Services Pin
Zibar12-Apr-03 4:58
sussZibar12-Apr-03 4:58 
GeneralRe: Question about Web Services Pin
Stephane Rodriguez.12-Apr-03 6:30
Stephane Rodriguez.12-Apr-03 6:30 
GeneralRe: Question about Web Services Pin
Cristoff13-Apr-03 0:43
Cristoff13-Apr-03 0:43 
GeneralRe: Question about Web Services Pin
Cristoff13-Apr-03 0:45
Cristoff13-Apr-03 0:45 
QuestionHow to use and custom MS Internet Explore? Pin
Small Rat12-Apr-03 4:28
Small Rat12-Apr-03 4:28 
AnswerRe: How to use and custom MS Internet Explore? Pin
Stephane Rodriguez.12-Apr-03 6:29
Stephane Rodriguez.12-Apr-03 6:29 
GeneralRe: How to use and custom MS Internet Explore? Pin
Small Rat12-Apr-03 6:38
Small Rat12-Apr-03 6:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.