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

Basic Client/Server Chat Application in C#

#31 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • 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

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • 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 offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,463
  • 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

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • 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

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • 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

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • 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: 82
  • 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 offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,463
  • 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
  • +
  • -

#42 garvit.official  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 03-June 13

Posted 03 June 2013 - 01:41 AM

Hello PsychoCoder :

In the method "SendMsgToAll",I am getting error in these two lines on ChatServer:
Chat.Sockets.TcpClient[] tcpClient = new Chat.Sockets.TcpClient[ChatServer.nickName.Count];
ChatServer.nickName.Values.CopyTo(tcpClient, 0);

Please go through this URL: "http://d.pr/i/3X4x"

Error is: ChatServer() is a method which is not valid in the given context.

I don't know why its not allowing me to access the ChatServer method. Thanks in advance
Was This Post Helpful? -1
  • +
  • -

#43 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Posted 03 June 2013 - 05:44 AM

He's not here to be your debugging servant.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#44 nata_steev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-October 13

Posted 29 October 2013 - 12:58 PM

I downloaded this project and run it
but I have a problem
the problem is
No connection could be made because the target machine actively refused it
on this line // tcpClient.Connect("127.0.0.1", 4296); //
what kind of connection should I run ?
thanks a lot
Was This Post Helpful? 0
  • +
  • -

#45 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Posted 30 October 2013 - 05:16 AM

Its not a project to be downloaded and run. Its a tutorial. Did you read it? Did you try to learn what he was teaching? Or are you just trying to copy/paste your way to a running application?

If you're asking "What kind of connection should I run?" then I doubt you read the article. Because part of it *is* the server you need to run.

Connection refused could be for a lot of reasons. Maybe you aren't running the server portion of it. Maybe your firewall is blocking it.
Was This Post Helpful? 0
  • +
  • -

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