C# server/multi-client

how to convert single server/ single client to server/multi-client???

Page 1 of 1

4 Replies - 46607 Views - Last Post: 04 July 2010 - 11:44 AM Rate Topic: -----

#1 mido999   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-July 10

C# server/multi-client

Posted 04 July 2010 - 08:02 AM

the server code
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.Threading;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
namespace server
{
    public partial class Form1 : Form
    {
        private Socket connection;
        private Thread readthread;

        private NetworkStream socketstream;
        private BinaryReader reader;
        private BinaryWriter writer;
        public Form1()
        {
            
            InitializeComponent();
            readthread = new Thread( new ThreadStart(RunServer));
            readthread.Start();
        }

        protected void Server_Closing(object sender, CancelEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
        }

        private void send_Click(object sender, EventArgs e)
        {
            try
            {
                writer.Write(textBox1.Text);
                textBox1.Text = "";
            }
            catch (Exception d)
            {
                Console.WriteLine(d);
            }
        }


        public void RunServer()
        {
            TcpListener listener;
            int count = 1;
            try
            {
                listener = new TcpListener(5000);
                listener.Start();

                while (true)
                {
                    textBox1.Text = "waiting for connection";
                    connection = listener.AcceptSocket();
                    socketstream = new NetworkStream(connection);

                    readthread = new Thread(new ThreadStart(funco));
                    readthread.Start();

                    

                    textBox1.AppendText("\n connection" + count + "recieved\n");

                    writer.Write("connection estableshed");

                    String reply = "";
                    do
                        try
                        {
                            

                            reply = reader.ReadString();
                            textBox1.Text = reply;
                        }
                        catch (Exception ff)
                        {
                            Console.WriteLine(ff);
                        } while (reply != "cterminate" && connection.Connected);

                    writer.Close();
                    reader.Close();
                    socketstream.Close();
                    connection.Close();
                    count++;
                    Application.Exit();
                }

            }
            catch (Exception gg)
            {
                Console.WriteLine(gg);
            }
        }



        public void funco()
        {
            writer = new BinaryWriter(socketstream);
            reader = new BinaryReader(socketstream);
        }


    }
}



-------------------------------------------------------------------------------------------------
the client Code
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.Threading;
using System.Net.Sockets;
using System.IO;

namespace client
{
    public partial class Form1 : Form
    {
        private NetworkStream output;
        private BinaryReader reader;
        private BinaryWriter writer;

        private String message = "";
        private Thread readthread;

        public Form1()
        {
            InitializeComponent();
            readthread = new Thread(new ThreadStart(RunClient));
            readthread.Start();
        }


        protected void client_closing(object sender , CancelEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
        }

        private void send_Click(object sender, EventArgs e)
        {
            try
            {
                String msg = tx2.Text;
                tx2.Text = "";
                writer.Write(msg);
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee);
            }

        }


        public void RunClient()
        {
            TcpClient client;

            try
            {
                tx1.Text = "Attempting connection";
                client = new TcpClient();
                client.Connect("192.168.1.101", 5000);

                output = client.GetStream();

                writer = new BinaryWriter(output);
                reader = new BinaryReader(output);
                MessageBox.Show("connection estableshed :D/>!!!");
                do
                {
                    try
                    {
                        message = reader.ReadString();
                        tx1.Text = message;

                    }catch(Exception fd)
                    {
                        MessageBox.Show("error" + fd);
                    }
                }while(message!= "terminate");

                writer.Close();
                reader.Close();
                output.Close();
                client.Close();
                Application.Exit();
            }
            catch (Exception ed)
            {
                MessageBox.Show("error"+ed);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }




    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C# server/multi-client

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# server/multi-client

Posted 04 July 2010 - 08:32 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:


  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: C# server/multi-client

Posted 04 July 2010 - 08:46 AM

The question appears to be in the topic description field (which we should get rid of I think):

"how to convert single server/ single client to server/multi-client???"
Was This Post Helpful? 0
  • +
  • -

#4 mido999   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-July 10

Re: C# server/multi-client

Posted 04 July 2010 - 08:48 AM

thanx for ur help , but i don't have any problems in the code i just want to convert this server/client code to server /multi-client , so i can connect the server to many clients and communicate from the server to many clients & vice versa.
Was This Post Helpful? 0
  • +
  • -

#5 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: C# server/multi-client

Posted 04 July 2010 - 11:44 AM

I won't write the code for you as I'm against that really, but I can tell you exactly how:

First you want to make a loop which listens for a connection, you want to run that loop on a new thread, then, when a connection comes through, you want to have a function to deal with that, and fire off another thread for that.

It is advisable to use some sort of thread pool though, if you can work with the limits.. ThreadPool could work.

You can work either with synchronous or asynchronous functions, but I find it is often easiest to use BeginAccept/EndAccept for the connections (async) and then switch to Receive/Send for the client stuff (sync) but of course, this is entirely subjective to what your needs are.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1