Click here to Skip to main content
15,888,401 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to use WeakEventManager with reflection Pin
Pete O'Hanlon24-Jan-16 21:25
mvePete O'Hanlon24-Jan-16 21:25 
AnswerRe: How to use WeakEventManager with reflection Pin
Kenneth Haugland25-Jan-16 7:42
mvaKenneth Haugland25-Jan-16 7:42 
GeneralRe: How to use WeakEventManager with reflection Pin
Sascha Lefèvre25-Jan-16 10:28
professionalSascha Lefèvre25-Jan-16 10:28 
QuestionHow to generate Dynamic buttons in C# from Database Values? Pin
Member 1224271723-Jan-16 19:54
Member 1224271723-Jan-16 19:54 
AnswerRe: How to generate Dynamic buttons in C# from Database Values? Pin
Mycroft Holmes23-Jan-16 20:13
professionalMycroft Holmes23-Jan-16 20:13 
GeneralRe: How to generate Dynamic buttons in C# from Database Values? Pin
Member 1224271723-Jan-16 20:25
Member 1224271723-Jan-16 20:25 
GeneralRe: How to generate Dynamic buttons in C# from Database Values? Pin
Mycroft Holmes23-Jan-16 20:30
professionalMycroft Holmes23-Jan-16 20:30 
Questionhow to implement this code for multiple clients using c sharp Pin
Member 1061979722-Jan-16 22:32
Member 1061979722-Jan-16 22:32 
the above c sharp code will send client desktop images to server . it works for single client . we want to implement this for multiple client. please provide help to implement it.

SERVER code:-
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.Remoting;
using System.Linq;
using System.Text;
using System.Net .Sockets ;
using System.Windows.Forms;
using System.Threading ;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;


namespace simpleserver
{
    public partial class Form2 : Form
    {
        private readonly    int port;
        private TcpClient client;
        private TcpListener server;
        //static public int MAX_CONN = 5;
        private NetworkStream mainstream;
        //private string ipList;
        //TcpListener myList = null;


//static int thrd = -1;

        private readonly Thread Listening;
        private readonly Thread Getimg;
        public Form2(int Port)
        {
            port = Port;
            client = new TcpClient();
            Listening = new Thread(StartListening);
            Getimg = new Thread(ReceiveImage);
            InitializeComponent();
        }


        private void StartListening()
        {
            while (!client.Connected)
            {
                //ipList = new string[MAX_CONN];
                //myList = new TcpListener(hostIP, hostPort);
                //myList.Start();

               server.Start();
               client = server.AcceptTcpClient();

               
            }
            Getimg.Start();
            
        }
        private void stop()
        {
            server.Stop();
            client = null;
            if (Listening .IsAlive )Listening .Abort ();
            if ( Getimg .IsAlive )Getimg .Abort ();
        }
        private void ReceiveImage()
        {
            BinaryFormatter binformatter = new BinaryFormatter();
            while (client.Connected)
            {
                mainstream = client.GetStream();
                pictureBox1.Image =(Image ) binformatter.Deserialize(mainstream);
              //  pictureBox2 .Image =(Image )binformatter .Deserialize (mainstream );

            }
 
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            server = new TcpListener(IPAddress .Any ,port  );
            Listening .Start ();
        }

      
    }
}


CLIENT CODE:-
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;

namespace sampleclient
{
    public partial class Form1 : Form
    {
        private readonly TcpClient client = new TcpClient();
        private NetworkStream mainStream;
        private int portNumber;

        private static Image GetDesktop()
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            Bitmap screenshot = new Bitmap(bounds .Width ,bounds .Height,PixelFormat .Format32bppArgb );
            Graphics g = Graphics.FromImage(screenshot);
            g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
            return screenshot;
        }
        private void SendDesktop()
        {
            
            BinaryFormatter  binformatter = new BinaryFormatter ();
            mainStream = client.GetStream();
            binformatter.Serialize (mainStream, GetDesktop());

 
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            portNumber = int.Parse(textBox2.Text);
            try
            {
                client.Connect(textBox1.Text, portNumber);
                MessageBox.Show("connected success");
                timer1.Start();
            }
            catch (Exception)
            {
                MessageBox.Show("fail");
            }
        }

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    if (button2.Text.StartsWith("share")) 
        //    {
        //        timer1.Start();
        //        button2.Text = ("stop sha");
        //    }
        //    else 
        //    {
        //        timer1 .Stop ();
        //        button2 .Text ="share sceen";
        //    }
        //}

        private void timer1_Tick(object sender, EventArgs e)
        {
            SendDesktop ();
        }
    }

}


modified 23-Jan-16 12:12pm.

AnswerRe: how to implement this code for multiple clients using c sharp Pin
OriginalGriff22-Jan-16 23:01
mveOriginalGriff22-Jan-16 23:01 
QuestionRe: how to implement this code for multiple clients using c sharp Pin
Paul Conrad23-Jan-16 6:00
professionalPaul Conrad23-Jan-16 6:00 
GeneralRe: how to implement this code for multiple clients using c sharp Pin
PIEBALDconsult23-Jan-16 6:13
mvePIEBALDconsult23-Jan-16 6:13 
GeneralRe: how to implement this code for multiple clients using c sharp Pin
PIEBALDconsult23-Jan-16 6:16
mvePIEBALDconsult23-Jan-16 6:16 
GeneralRe: how to implement this code for multiple clients using c sharp Pin
Richard Andrew x6423-Jan-16 9:21
professionalRichard Andrew x6423-Jan-16 9:21 
GeneralRe: how to implement this code for multiple clients using c sharp Pin
PIEBALDconsult23-Jan-16 9:24
mvePIEBALDconsult23-Jan-16 9:24 
AnswerSimple answer: add an header before data Pin
Philippe Mori23-Jan-16 8:21
Philippe Mori23-Jan-16 8:21 
AnswerRe: how to implement this code for multiple clients using c sharp Pin
jschell27-Jan-16 9:18
jschell27-Jan-16 9:18 
Questionautomatic test data generators for C# ? Pin
BillWoodruff21-Jan-16 18:15
professionalBillWoodruff21-Jan-16 18:15 
AnswerRe: automatic test data generators for C# ? Pin
Eddy Vluggen22-Jan-16 6:38
professionalEddy Vluggen22-Jan-16 6:38 
GeneralRe: automatic test data generators for C# ? Pin
BillWoodruff22-Jan-16 13:03
professionalBillWoodruff22-Jan-16 13:03 
GeneralRe: automatic test data generators for C# ? Pin
Eddy Vluggen23-Jan-16 0:55
professionalEddy Vluggen23-Jan-16 0:55 
QuestionICustomMarshaler not returning an Out parameter Pin
mavl21-Jan-16 5:16
mavl21-Jan-16 5:16 
AnswerRe: ICustomMarshaler not returning an Out parameter Pin
Luc Pattyn21-Jan-16 15:18
sitebuilderLuc Pattyn21-Jan-16 15:18 
GeneralRe: ICustomMarshaler not returning an Out parameter Pin
mavl21-Jan-16 23:08
mavl21-Jan-16 23:08 
GeneralRe: ICustomMarshaler not returning an Out parameter Pin
Luc Pattyn22-Jan-16 4:40
sitebuilderLuc Pattyn22-Jan-16 4:40 
GeneralRe: ICustomMarshaler not returning an Out parameter Pin
mavl22-Jan-16 8:58
mavl22-Jan-16 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.