F
FLASHCODER
Guest
I have 2 Sockets in server and client respectively (where client connect to server), on server i have one class that contains some informations of each client like IP, Socket and ID.
For each connection accepted i add the last client connected to this List, this way i have a list (that is a object) of clients. And also each client is added in one ListView where is assigned the object referent to this client, on Tag property of ListViewItem. This is one of way that can help later if you want send a message to a specific client.
Like you can see this List of clients is referent only to 1 Socket that listen in a determinated port number. Now already that i have 2 Sockets in each side (server and client), how i can make if i want send a message from second Socket of server to the second Socket on client? for example, is possible assign more than 1 object to Tag property of ListViewItem (only a idea)?
Below is my code (server, compatible only with 1 Socket and 1 object (List of client)):
Form:
namespace mainForm
{
public partial class frmMain : Form
{
Listener server;
Thread start;
public frmMain()
{
InitializeComponent();
server = new Listener();
}
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
start = new Thread(listen);
start.Start();
}
private void listen()
{
server.BeginListen(101); // Select a port to listen
server.Received += new Listener.ReceivedEventHandler(server_Received);
server.Disconnected += new Listener.DisconnectedEventHandler(server_Disconnected);
}
private void server_Disconnected(Listener l, Info i)
{
Invoke(new _Remove(Remove), i);
}
private void server_Received(Listener l, Info i, string received)
{
string[] cmd = received.Split('|');
int inc;
for (inc = 0; inc < cmd.Length; inc++)
{
switch (cmd[inc].ToString())
{
case "CMD1":
Invoke(new _Add(Add), i, cmd[2] + " - " + cmd[3]);
break;
case "CMD2":
// Other code here
break;
}
}
}
private delegate void _Add(Info i, string computer);
private void Add(Info i, string computer)
{
string[] splitIP = i.RemoteAddress.Split(':');
ListViewItem item = new ListViewItem();
item.Text = i.ID.ToString();
item.SubItems.Add(splitIP[0]);
item.SubItems.Add(computer);
item.Tag = i;
lvConnections.Items.Add(item);
}
private delegate void _Remove(Info i);
private void Remove(Info i)
{
foreach (ListViewItem item in lvConnections.Items)
{
if ((Info)item.Tag == i)
{
item.Remove();
break;
}
}
}
private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in lvConnections.SelectedItems)
{
Info client = (Info)item.Tag;
client.Send("DISCONNECT" + Environment.NewLine);
}
}
Listener:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Listener
{
Socket s;
public List<Info> clients;
public delegate void ReceivedEventHandler(Listener l, Info i, string received);
public event ReceivedEventHandler Received;
public delegate void DisconnectedEventHandler(Listener l, Info i);
public event DisconnectedEventHandler Disconnected;
bool listening = false;
public Listener()
{
clients = new List<Info>();
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public bool Running
{
get { return listening; }
}
public void BeginListen(int port)
{
s.Bind(new IPEndPoint(IPAddress.Any, port));
s.Listen(100);
s.BeginAccept(new AsyncCallback(AcceptCallback), s);
listening = true;
}
public void StopListen()
{
if (listening == true)
{
s.Close();
listening = false;
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket handler = (Socket)ar.AsyncState;
Socket sock = handler.EndAccept(ar);
Info i = new Info(sock);
clients.Add(i);
sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
}
void ReadCallback(IAsyncResult ar)
{
Info i = (Info)ar.AsyncState;
try
{
int rec = i.sock.EndReceive(ar);
if (rec != 0)
{
string data = Encoding.UTF8.GetString(i.buffer, 0, rec);
Received(this, i, data);
}
else
{
Disconnected(this, i);
return;
}
i.sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
}
catch (Exception ex)
{
Disconnected(this, i);
i.sock.Close();
clients.Remove(i);
}
}
}
Info:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Info
{
public Socket sock;
public Guid ID;
public string RemoteAddress;
public byte[] buffer = new byte[8192];
public Info(Socket sock)
{
this.sock = sock;
ID = Guid.NewGuid();
RemoteAddress = sock.RemoteEndPoint.ToString();
}
public void Send(string data)
{
byte[] buffer = Encoding.UTF8.GetBytes(data);
sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
{
sock.EndSend(ar);
}), buffer);
}
}
Continue reading...
For each connection accepted i add the last client connected to this List, this way i have a list (that is a object) of clients. And also each client is added in one ListView where is assigned the object referent to this client, on Tag property of ListViewItem. This is one of way that can help later if you want send a message to a specific client.
Like you can see this List of clients is referent only to 1 Socket that listen in a determinated port number. Now already that i have 2 Sockets in each side (server and client), how i can make if i want send a message from second Socket of server to the second Socket on client? for example, is possible assign more than 1 object to Tag property of ListViewItem (only a idea)?
Below is my code (server, compatible only with 1 Socket and 1 object (List of client)):
Form:
namespace mainForm
{
public partial class frmMain : Form
{
Listener server;
Thread start;
public frmMain()
{
InitializeComponent();
server = new Listener();
}
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
start = new Thread(listen);
start.Start();
}
private void listen()
{
server.BeginListen(101); // Select a port to listen
server.Received += new Listener.ReceivedEventHandler(server_Received);
server.Disconnected += new Listener.DisconnectedEventHandler(server_Disconnected);
}
private void server_Disconnected(Listener l, Info i)
{
Invoke(new _Remove(Remove), i);
}
private void server_Received(Listener l, Info i, string received)
{
string[] cmd = received.Split('|');
int inc;
for (inc = 0; inc < cmd.Length; inc++)
{
switch (cmd[inc].ToString())
{
case "CMD1":
Invoke(new _Add(Add), i, cmd[2] + " - " + cmd[3]);
break;
case "CMD2":
// Other code here
break;
}
}
}
private delegate void _Add(Info i, string computer);
private void Add(Info i, string computer)
{
string[] splitIP = i.RemoteAddress.Split(':');
ListViewItem item = new ListViewItem();
item.Text = i.ID.ToString();
item.SubItems.Add(splitIP[0]);
item.SubItems.Add(computer);
item.Tag = i;
lvConnections.Items.Add(item);
}
private delegate void _Remove(Info i);
private void Remove(Info i)
{
foreach (ListViewItem item in lvConnections.Items)
{
if ((Info)item.Tag == i)
{
item.Remove();
break;
}
}
}
private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in lvConnections.SelectedItems)
{
Info client = (Info)item.Tag;
client.Send("DISCONNECT" + Environment.NewLine);
}
}
Listener:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Listener
{
Socket s;
public List<Info> clients;
public delegate void ReceivedEventHandler(Listener l, Info i, string received);
public event ReceivedEventHandler Received;
public delegate void DisconnectedEventHandler(Listener l, Info i);
public event DisconnectedEventHandler Disconnected;
bool listening = false;
public Listener()
{
clients = new List<Info>();
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public bool Running
{
get { return listening; }
}
public void BeginListen(int port)
{
s.Bind(new IPEndPoint(IPAddress.Any, port));
s.Listen(100);
s.BeginAccept(new AsyncCallback(AcceptCallback), s);
listening = true;
}
public void StopListen()
{
if (listening == true)
{
s.Close();
listening = false;
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket handler = (Socket)ar.AsyncState;
Socket sock = handler.EndAccept(ar);
Info i = new Info(sock);
clients.Add(i);
sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
}
void ReadCallback(IAsyncResult ar)
{
Info i = (Info)ar.AsyncState;
try
{
int rec = i.sock.EndReceive(ar);
if (rec != 0)
{
string data = Encoding.UTF8.GetString(i.buffer, 0, rec);
Received(this, i, data);
}
else
{
Disconnected(this, i);
return;
}
i.sock.BeginReceive(i.buffer, 0, i.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), i);
}
catch (Exception ex)
{
Disconnected(this, i);
i.sock.Close();
clients.Remove(i);
}
}
}
Info:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Info
{
public Socket sock;
public Guid ID;
public string RemoteAddress;
public byte[] buffer = new byte[8192];
public Info(Socket sock)
{
this.sock = sock;
ID = Guid.NewGuid();
RemoteAddress = sock.RemoteEndPoint.ToString();
}
public void Send(string data)
{
byte[] buffer = Encoding.UTF8.GetBytes(data);
sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
{
sock.EndSend(ar);
}), buffer);
}
}
Continue reading...