how to implement this code for multiple clients using c sharp
-
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:-
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 (); }
-
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:-
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 (); }
Apart from that being terrible code - VS default names for everything, commented out blocks, assumptions that users never type mistakes, ignoring exception detail when you do catch them, and so forth - we have no idea what you mean by "make it work for multiple clients", or what you problem with doing it yourself is. And when you add that we aren't here to do your job for you I think it's very unlikely that you will get what you want with this question. Instead, sit down and work out exactly what you are trying to do, and work out what you need to implement in order to do it. Then make a list of what you do and don;t know how to do, and implement the stuff you do. For the stuff you don't start with google, and ask specific questions when you can't find what you need, or don't understand whatyou find. But just saving "provide help to implement it" isn't going to get you very far...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
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:-
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 (); }
First of all, have you considered using
code
pre tags when posting code? It makes it much easier to read. Second, what have you attempted so far?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
-
First of all, have you considered using
code
pre tags when posting code? It makes it much easier to read. Second, what have you attempted so far?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
(remedied)
-
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:-
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 (); }
As I suppose you have found, a point-to-point socket connection is not what you want for this. You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.
-
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:-
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 (); }
Well, even though as others as pointed out, this is not good code... One easy answer is simply that each client code append say a block of a given length before the data for identification purpose (assuming this is for educative purpose).
Philippe Mori
-
As I suppose you have found, a point-to-point socket connection is not what you want for this. You will likely find success by using a Web Service or similar, but I expect a database connection would be even simpler because you wouldn't actually need to implement anything on the server.
PIEBALDconsult wrote:
but I expect a database connection would be even simpler
Isn't it not good to expose the database across the network like that?
The difficult we do right away... ...the impossible takes slightly longer.
-
PIEBALDconsult wrote:
but I expect a database connection would be even simpler
Isn't it not good to expose the database across the network like that?
The difficult we do right away... ...the impossible takes slightly longer.
I assume it's a local network, but if not, then a Web Service may provide better security.
-
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:-
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 (); }
Context for the solution helps. So is this a commercial solution, something for school, experimenting, etc. As others have noted there are standard idioms that you can research for doing client/server setups. If you want to roll you own then 1. Separate out communication from processing. Different classes. So you need classes that 'send' a message and classes that 'receive' a message. Then different classes that package a 'desktop image' as a message 2. On the server side when a server accepts a client connection then the server spins up a new thread to handle that request (for messages between the original client and the server.) It does that for each client and perhaps for each message that a unique client sends. A quick glance at your code suggests that you are not creating a new thread for each request.