Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
what i am trying to do is create a chat!

my main is a console application that receives information.

<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 5000);
            server.Start();
            Console.WriteLine("Server started");
            string word = "";
            savedObject saved = new savedObject();

            while (true)
            {
                TcpClient connection = server.AcceptTcpClient();
                Console.WriteLine("connection accepted");
                ThreadPool.QueueUserWorkItem(saved.ProssecClient, connection);
            }
        }
    }
}





class savedObject
{
    string word;

    public string AllWords(string sit)
    {

        word += sit + " ";

        return word;
    }


    public string word2()
    {
        return word;
    }
    StreamReader[] sr1 = new StreamReader[100];
    StreamWriter[] sw1 = new StreamWriter[100];
    int m;
    int a;
    Dictionary<string, TcpClient> con = new Dictionary<string, TcpClient>();
    Dictionary<string, StreamReader> conReader = new Dictionary<string, StreamReader>();
    Dictionary<string, StreamWriter> conWriter = new Dictionary<string, StreamWriter>();
    
   
    public void ProssecClient(object o)
    {
        ///Useless Variables
        string name = "";
        string word2 = "";
        TcpClient connection = o as TcpClient;
        if (connection == null)
        {
            return;
        }

        // Streams I/O
        StreamReader sr = new StreamReader(connection.GetStream());
        sr1[a++] = new StreamReader(connection.GetStream());
        sw1[m++] = new StreamWriter(connection.GetStream());
        //Console.WriteLine("Type in Your Name: ");
       // name = Console.ReadLine();

        name = sr.ReadLine();
        Console.WriteLine(name+" has joined the server");

        //Info----> Dictionary


        con.Add(name, connection);
        conReader.Add(name, sr1[a]);
        conWriter.Add(name, sw1[m]);


        //Read & Write to everyone!!!!!
        try
        {
            while (true)
            {
                int i = 0;
                
                word2 = AllWords(sr.ReadLine());

                for (i = 0; i < 10; i++)
                {
                    if (sw1[i] != null)
                    {
                        sw1[i].WriteLine(name+" : "+word2);

                        sw1[i].Flush();

                    }

                }

            }
        }
        catch { Console.WriteLine(  name+ " client left"); }
    }

}

</code> 


This code works with the clients code. here is the clients side code. it is capable of sending and receiving messages from

other users:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient connection = new TcpClient("127.0.0.1", 5000);
            StreamReader sr = new StreamReader(connection.GetStream());
            StreamWriter sw = new StreamWriter(connection.GetStream());
       
            string name2 = "";
            while (true)
            {
                Console.WriteLine("Enter your name and press submit");
                name2=Console.ReadLine();
                if (name2 != "")
                {
                    sw.WriteLine(name2);
                    break;
                }
            }
            Console.WriteLine("Loop is over");
            Thread t2 = new Thread(Reader);
           
            t2.IsBackground = true;
            t2.Start(connection);

            while (true)
            {
                sw.WriteLine(Console.ReadLine());
                sw.Flush();
            }
        }
  


        public static void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
                return;
            StreamReader sr = new StreamReader(con.GetStream());
            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine();
                }

                Console.WriteLine(sr.ReadLine());
                Console.WriteLine();
            }
        }
    }
}


My goal here is to convert the client side code to a winform. so far i did that, but it gets stuck when i press the button:

<pre lang="msil">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient connection;
        StreamReader sr;
        StreamWriter sw;
        public Form1()
        {
            InitializeComponent();
            try
            {
                connection = new TcpClient("127.0.0.1", 5000);
                sr = new StreamReader(connection.GetStream());
                sw = new StreamWriter(connection.GetStream());
            }
            catch
            {
                MessageBox.Show("Couldnt form a connection");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                sw.WriteLine(richTextBox1.Text);
                sw.Flush();
                richTextBox2.Text = sr.ReadLine();
            }
            catch
            {
                MessageBox.Show("Cant Type Message");
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
        }
    }
}






i dont need a sample of a code that i dont understand, i just need to put make the winform send and accept data.
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-11 8:04am    
You are afraid of code you may not understand? Want to talk about it?
OriginalGriff 3-Feb-11 8:33am    
You are doing therapy as well now? :laugh:
Dmitry Makovetskiy 3-Feb-11 8:53am    
what is there to laugh.. why dont you look at my "simple" code and give me the solution if that is so easy? (or at least give me a direction if you struggle)
OriginalGriff 3-Feb-11 8:58am    
If you look at the indentation levels of the messages, you will see that the comment was directed to SAKryukov, not to you.
If you still feel after looking more carefully that I have been offensive to you, then I apologise.
Dmitry Makovetskiy 3-Feb-11 9:02am    
i dont understand winforms , i am new to winfroms, what is there to be offended about.

i did all the work on console application already. it is done..now winfrom is the problem

WinForms is completely different way of doing things:

A console app is generally procedural: loops, and an obvious execution flow.

WinForms isn't: it is event driven. That means that the order in which things happen is outside your control much of the time. Additionally, you can't run in loops any more, because other events can't happen.

So when you use a procedural routine such as sr.ReadLine you have to be sure there is data there to receive. If there isn't, everything will hang until there is. And I mean everything. Buttons won't work, displays won't update, etc.

You need to go back to the beginning, forget your chat application for a while, and try to get your head around what happens in WinForms apps.
 
Share this answer
 
Comments
Dmitry Makovetskiy 3-Feb-11 9:27am    
my server is a console application, cant it remain a console application, while the client server is the winfrom..

i am not sure how i cant neglect threads in my server console application, cause it keeps accepting connections and communicate with them..

what do you think of my server application? should i leave it as it is?

why does my client winform application refuses to read info from the server?
I think you need to convert the data to be send/receive as byte array.That's how it works when you send/receive data over the network.The below article clearly explains this.Take a look.

TCP/IP Chat Application Using C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900