• (3 Pages)
  • +
  • 1
  • 2
  • 3

Basic Client/Server Chat Application in C#

#31 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Posted 12 September 2011 - 10:33 AM

This is going to basically sound like a newbie question, but I just have to ask...

I've managed to go all this time without ever actually using hashtables. What advantage do you find in using them / What was the reason for using them here? It almost seems like a Hash table is primitive Dictionary<>, similar to how an array[] is a primitive form of a List<>.

Looking at the MSDN page for hash tables it looks like they work like Dictionary<> and even use dictionaryentry class.

Am I missing a big advantage them them?

 Hashtable openWith = new Hashtable();

        // Add some elements to the hash table. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // The Add method throws an exception if the new key is 
        // already in the hash table.
        try
        {
            openWith.Add("txt", "winword.exe");
        }
        catch
        {
            Console.WriteLine("An element with Key = \"txt\" already exists.");

// The Item property is the default property, so you 
        // can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

        // The default Item property can be used to change the value
        // associated with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

        // If a key does not exist, setting the default Item property
        // for that key adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
        }

        // When you use foreach to enumerate hash table elements,
        // the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        foreach( DictionaryEntry de in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
        }
        }

This post has been edited by tlhIn`toq: 12 September 2011 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

#32 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Posted 12 September 2011 - 10:58 AM

You had a couple lines that produce warning in the catch statements of the methods:
public static void SendMsgToAll(string nick, string msg)
and
public static void SendSystemMessage(string msg) .

I'm not a fan of unresolved warnings, especially without a comment as to why they exist: They make co-workers wonder if you meant something peculiar.

So I updated them to this:

#pragma warning disable 1717
e44 = e44;// Warning = variable set equal to itself. WTF?  Presumable a breakpoint position so the developer can still read the content of the error message
#pragma warning restore 1717


I thought I would throw it out there for others.
Was This Post Helpful? 0
  • +
  • -

#33 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3562
  • View blog
  • Posts: 14,989
  • Joined: 12-June 08

Posted 12 September 2011 - 10:59 AM

@tlhIn: In theory the hash takes a key and translates it into a unique index and everything is nice and neat, but that rarely happens. So most hashtables include some sort of 'collision' detection and navigation. If two different keys has out to the same index how does the object handle that? That and it takes in mixed object types with no problem versus say a basic integer or string.

Some nice C# examples:
http://www.dotnetperls.com/hashtable
Was This Post Helpful? 0
  • +
  • -

#34 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Posted 08 October 2011 - 03:34 PM

Moved to own thread
http://www.dreaminco...e-not-starting/

This post has been edited by tlhIn`toq: 08 October 2011 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#35 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Posted 08 October 2011 - 03:46 PM

Move to own thread
http://www.dreaminco...e-not-starting/

This post has been edited by tlhIn`toq: 08 October 2011 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#36 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Posted 09 October 2011 - 10:39 AM

Wonderful tutorial - but I must be a bloody idiot.
After 3 days I cannot seem to incorporate this into the simplest of tiny testing applications.

With all firewalls off and set to allow all incoming connections it seems that the tcpClient gets forcibly aborted / disposed of, very quickly.

Every now and then if I am very fast on the keyboard I can send my nickname and have the server respond with "Clint has joined" but more often than not I get this error as soon as I try to respond with my name.

Any ideas what could cause this?

Attached Image

This post has been edited by tlhIn`toq: 09 October 2011 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#37 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 81
  • Joined: 21-May 08

Posted 19 October 2011 - 01:54 PM

i think your server class without threads will work if the client and server are not on the same pc because all clients will be connected at ip:address "127.0.0.1" local loop.
Was This Post Helpful? 0
  • +
  • -

#38 bengof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-October 11

Posted 23 October 2011 - 05:03 PM

Great application, Thank you for sharing this.
Was This Post Helpful? 0
  • +
  • -

#39 samitkg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-January 12

Posted 24 January 2012 - 03:18 AM

View Postcat_sin, on 15 January 2009 - 11:19 AM, said:

View PostPsychoCoder, on 18 Sep, 2007 - 07:08 AM, said:

As I said in the end of the tutorial, the ChatServer.cs files needs to be made a part of its own application, so the server can run. I provided the functionality of the server in a class file. The client application isn't going to run because there isn't a chat server to connect to

Quote

NOTE: You're going to want to take the ChatServer Class and possibly make an application out of that as well. I have it as a class file as I'm using a different implementation of the server.


Hi, PsychoCoder.
I'm new to C# also. I wondering what do you mean by
"ChatServer.cs files needs to be made a part of its own application"

do you mean that I need to create a server program by using the ChatServer.cs class?

I'm using Microsoft Visual C# 2008 express edition. When I download the zip file and open the ChatServer.sln and start debug, this error come out.
"No connection could be made because the target machine actively refused it 127.0.0.1:4296"

I understand that this is due to server not running, but how can make the server run? I'm sorry if this is a stupid question and hope to get your answer soon. Thanks :)

Regards,
cat_sin


Tell me that how to configure the port on chatserver and how run,cause whenever i try to run that code it's givin the error msg--"No connection could be made because the target machine actively refused it 127.0.0.1:4296"

Regards,
samitkg
Was This Post Helpful? 0
  • +
  • -

#40 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3562
  • View blog
  • Posts: 14,989
  • Joined: 12-June 08

Posted 24 January 2012 - 07:59 AM

@samitkg - try looking into your firewall settings.

https://blogs.msdn.c...Redirected=true
Was This Post Helpful? 0
  • +
  • -

#41 DarkForm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-March 11

Posted 30 January 2012 - 03:08 PM

I must have failed to read somewhere, but does chat application only work with LAN or can I change the IP and port to my server so people can use this over the internet?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3