Hi.. It's Me the Thread Starter.
First of All, I'm sorry about my Mistype subject. It should be
UDP connection not
TCP connection. It's my stupid fault :'(
Anyway, Here's my Code that i have
SERVER SIDE
//The commands for interaction between the server and the client
enum Command
{
Login, //Log into the server
Logout, //Logout of the server
Null //No command
}
public partial class KioskMonitoringServer : Form
{
struct ClientInfo
{
public EndPoint endpoint; //Socket of the client
public string strName; //Name by which the client logged into the server
public string strClientStatus;
public string strClientOS;
public string strClientApp;
}
//The collection of all clients logged into the room (an array of type ClientInfo)
ArrayList clientList;
//The main socket on which the server listens to the clients
Socket serverSocket;
byte[] byteData = new byte[1024];
public KioskMonitoringServer()
{
InitializeComponent();
clientList = new ArrayList();
}
private void btnListening_Click(object sender, EventArgs e)
{
try
{
CheckForIllegalCrossThreadCalls = false;
//i'm using UDP sockets
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
//Assign the any IP of the machine and listen on port on TextBox
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPort.Text));
//Bind this address to the server
serverSocket.Bind(ipEndPoint);
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender;
//Start receiving data
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
txtLog.AppendText("Start Listening on " + txtPort.Text + "\n");
btnListening.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnReceive(IAsyncResult ar)
{
memoClientDetail.Clear();
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.EndReceiveFrom(ar, ref epSender);
//Transform the array of bytes received from the user into an
//intelligent form of object Data
Data msgReceived = new Data(byteData);
//We will send this object in response the users request
Data msgToSend = new Data();
byte[] message;
switch (msgReceived.cmdCommand)
{
case Command.Login:
//When a user logs in to the server then we add her to our
//list of clients
ClientInfo clientInfo = new ClientInfo();
clientInfo.endpoint = epSender;
clientInfo.strName = msgReceived.strName;
clientInfo.strClientStatus = msgReceived.strClientStatus;
clientInfo.strClientOS = msgReceived.strClientOS;
clientInfo.strClientApp = msgReceived.strClientApp;
clientList.Add(clientInfo);
listClient.Items.Add(msgReceived.strName);
memoClientDetail.AppendText(msgReceived.strClientStatus);
memoClientDetail.AppendText(msgReceived.strClientOS);
memoClientDetail.AppendText(msgReceived.strClientApp);
break;
case Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.strName == msgReceived.strName)
{
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
listClient.Items.Remove(msgReceived.strName);
break;
}
txtLog.AppendText(msgToSend.strMessage + "\r\n");
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
CLIENT SIDE
enum Command
{
Login, //Log into the server
Logout, //Logout of the server
Null //No command
}
public partial class KioskMonitoringClient : Form
{
public Socket clientSocket;
public EndPoint epServer;
public string strName;
public string m_ApplicationName = "Notepad";
public bool m_ApplicationIsRunning = false;
public KioskMonitoringClient()
{
InitializeComponent();
}
private ArrayList GetProcesses() //This method is used for Generating Current running process
{
ArrayList procList = new ArrayList();
try
{
Process[] process = Process.GetProcesses();
foreach (Process proc in process)
{
procList.Add(proc);
}
return procList;
}
catch (Exception)
{
return null;
}
}
private string CheckRunningProcess(string process) // this Function is used for checking if an application (i'm using notepad.exe) is running or not
{
try
{
this.SuspendLayout();
m_ApplicationIsRunning = false;
ArrayList arrProcess = new ArrayList();
arrProcess = this.GetProcesses();
Process proc;
for (int i = 0; i < arrProcess.Count; i++)
{
proc = new Process();
proc = (Process)arrProcess[i];
if (String.Compare(process.ToUpper(), proc.ProcessName.ToUpper()) == 0)
{
m_ApplicationIsRunning = true;
break;
}
}
if (m_ApplicationIsRunning)
return "Running";
else
return "Not started";
}
catch (Exception)
{
return "Not started";
}
}
private void btnConnect_Click(object sender, EventArgs e) // Connecting to server
{
strName = Dns.GetHostName(); //Get Computer name
try
{
//Using UDP sockets
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
//IP address of the server machine
IPAddress ipAddress = IPAddress.Parse(txtServerIP.Text);
//Server is listening on current txtServerPort.Text
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(txtServerPort.Text));
epServer = (EndPoint)ipEndPoint;
Data msgToSend = new Data();
OperatingSystemInfo osInfo = new OperatingSystemInfo();
#region " Send Data"
msgToSend.cmdCommand = Command.Login;
msgToSend.strName = strName;
msgToSend.strMessage = null;
msgToSend.strClientStatus = "Client Status : Online\n";
msgToSend.strClientOS = "Operating System Info : " + osInfo.GetOSName() + osInfo.GetOSProductType() + osInfo.GetOSServicePack() + "\n";
msgToSend.strClientApp = "Notepad Application Status : " + this.CheckRunningProcess(m_ApplicationName) + "\n";
#endregion
byte[] byteData = msgToSend.ToByte();
//Login to the server
clientSocket.BeginSendTo(byteData, 0, byteData.Length,
SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSclient",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnSend(IAsyncResult ar)
{
try
{
clientSocket.EndSend(ar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void KioskMonitoringClient_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void KioskMonitoringClient_FormClosing(object sender, FormClosingEventArgs e) // Logout from server
{
try
{
//Send a message to logout of the server
Data msgToSend = new Data();
msgToSend.cmdCommand = Command.Logout;
msgToSend.strName = strName;
msgToSend.strMessage = null;
byte[] b = msgToSend.ToByte();
clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
clientSocket.Close();
}
catch (ObjectDisposedException)
{ }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}