2 Replies - 869 Views - Last Post: 06 April 2016 - 06:18 PM Rate Topic: -----

#1 computerguy3100   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 04-June 12

Please help with c# IM Client

Posted 06 April 2016 - 12:48 PM

Hello, I have been writing a console-based instant messaging client in c#.

While it is functional, there are some things I need help with. I am trying to make it so that my client and server send the name of the user along with the senders message, while I have successfully done this with the server, I cannot get it to work with the client. In the server code, I am trying to make it automatically put a space after the username but with no success. Any help would be appreciated, thank you very much.

Compiler used: Microsoft Visual C# 2015. If possible, I would like to change the starting message in the server so it says "username is connected" as opposed to just the username. Thank you.

Server code:

using System;
using System.Net.Sockets;
using System.IO;
public class ServerSocket1
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("Please enter your name");
            string name = "";
            name=Console.ReadLine();


            bool status = true;
            string servermessage = "";
            string clientmessage = "";
            TcpListener tcpListener = new TcpListener(443);
            tcpListener.Start();
            Console.WriteLine(name);
            Socket socketForClient = tcpListener.AcceptSocket();
            Console.WriteLine("Client Connected");
            NetworkStream networkStream = new NetworkStream(socketForClient);
            StreamWriter streamwriter = new StreamWriter(networkStream);
            StreamReader streamreader = new StreamReader(networkStream);
            while (status)
            {
                if (socketForClient.Connected)
                {
                    servermessage = streamreader.ReadLine();
                    Console.WriteLine("Client:" + servermessage);
                    if ((servermessage == "bye"))
                    {
                        status = false;
                        streamreader.Close();
                        networkStream.Close();
                        streamwriter.Close();
                        return;
                        Console.Write("The server has dissconnected. The Conversation os over.");

                    }
                    Console.Write(clientmessage);
                    clientmessage = Console.ReadLine();
                    streamwriter.Write(name);
           
                    streamwriter.WriteLine(clientmessage);
                    streamwriter.Flush();
                }
            }
            streamreader.Close();
            networkStream.Close();
            streamwriter.Close();
            socketForClient.Close();
            Console.WriteLine("Exiting");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}



Client code:
using System;
using System.Net.Sockets;
using System.IO;
public class ClientSocket1
{
    static void Main(string[] args)
    {
        TcpClient socketForServer;
        bool status = true;
        try
        {
            Console.WriteLine("Please type the IPv4 address of your chat buddy");
            string ip;
            ip = Console.ReadLine();
            socketForServer = new TcpClient(ip, 443);
            Console.WriteLine("Connected to Server");
            Console.WriteLine("What is your name?");
            string name = "";
            name = Console.ReadLine();

        }
        catch
        {
            Console.WriteLine("Failed to Connect to server{0}:999", "localhost");
            return;
        }
        NetworkStream networkStream = socketForServer.GetStream();
        StreamReader streamreader = new StreamReader(networkStream);
        StreamWriter streamwriter = new StreamWriter(networkStream);
        try
        {
            string clientmessage = "";
            string servermessage = "";
            while (status)
            {
                Console.Write("Client:");
                clientmessage = Console.ReadLine();
                if ((clientmessage == "bye") || (clientmessage == "BYE"))
                {
                    status = false;
                    streamwriter.WriteLine("bye");
                    streamwriter.Flush();
                }
                if ((clientmessage != "bye") && (clientmessage != "BYE"))
                {
                    streamwriter.WriteLine(clientmessage);
                    streamwriter.Flush();
                    servermessage = streamreader.ReadLine();
                    streamwriter.Write(name);
                    Console.WriteLine("Server:" + servermessage);
                }
            }
        }
        catch
        {
            Console.WriteLine("Exception reading from the server");
        }
        streamreader.Close();
        networkStream.Close();
        streamwriter.Close();
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Please help with c# IM Client

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7354
  • View blog
  • Posts: 24,843
  • Joined: 05-May 12

Re: Please help with c# IM Client

Posted 06 April 2016 - 04:40 PM

Can you show us what output you are currently getting for both sides?

Out of curiosity, why are you having difficulty changing your connection notification message, or in putting a space after the username? String formatting is something that you should have learned way before you even started dabbling in socket programming. What book or tutorial are you following that would fall to teach you this?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Please help with c# IM Client

Posted 06 April 2016 - 06:18 PM

See FAQ 10: I'm making a client/server chat program and ...




tlhIn`toq's FAQ list - Updated Oct 2015


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, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
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.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

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

Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?


WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.


FAQ (Frequently Asked Questions - Updated Nov 2015
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1