How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Sep 2008
Posts: 3
Reputation: basslover is an unknown quantity at this point 
Solved Threads: 0
basslover basslover is offline Offline
Newbie Poster

How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

 
0
  #1
Sep 10th, 2008
I have mini project about client-server connection and i'm using TCP Socket to do a connection. The server wants to know if a client has a 'notepad.exe' application running on the client machine.

I already have list of running process on client machine, and i have an algorithm to detect if Notepad is already running or not.

So, I think there will be a 'Refresh' button on server in order to force Client Side to send data (Notepad application status) to Server , but still in Server Side. Do you get my point ? Sent Data is a string data, e.g. "Notepad is already running", or "Notepad has not started yet".

Just Like my Subject Title, I want to know How to send data from client to server via server side ? Could you please give me some Simple Application about forcing client to send data to server via server side. Urgently Needed

Thanks for any help

Best Regards
Yefta Adiya - Indonesia -
C# Developer
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

 
0
  #2
Sep 10th, 2008
There are many examples within MSDN which help, please give some code that you're having issues with and we will assist, as per http://www.daniweb.com/forums/announcement61-2.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: basslover is an unknown quantity at this point 
Solved Threads: 0
basslover basslover is offline Offline
Newbie Poster

Re: How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

 
0
  #3
Sep 10th, 2008
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
  1. //The commands for interaction between the server and the client
  2. enum Command
  3. {
  4. Login, //Log into the server
  5. Logout, //Logout of the server
  6. Null //No command
  7. }
  8.  
  9. public partial class KioskMonitoringServer : Form
  10. {
  11. struct ClientInfo
  12. {
  13. public EndPoint endpoint; //Socket of the client
  14. public string strName; //Name by which the client logged into the server
  15. public string strClientStatus;
  16. public string strClientOS;
  17. public string strClientApp;
  18. }
  19.  
  20. //The collection of all clients logged into the room (an array of type ClientInfo)
  21. ArrayList clientList;
  22.  
  23. //The main socket on which the server listens to the clients
  24. Socket serverSocket;
  25.  
  26. byte[] byteData = new byte[1024];
  27.  
  28. public KioskMonitoringServer()
  29. {
  30. InitializeComponent();
  31. clientList = new ArrayList();
  32. }
  33.  
  34. private void btnListening_Click(object sender, EventArgs e)
  35. {
  36. try
  37. {
  38. CheckForIllegalCrossThreadCalls = false;
  39.  
  40. //i'm using UDP sockets
  41. serverSocket = new Socket(AddressFamily.InterNetwork,
  42. SocketType.Dgram, ProtocolType.Udp);
  43.  
  44. //Assign the any IP of the machine and listen on port on TextBox
  45. IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPort.Text));
  46.  
  47. //Bind this address to the server
  48. serverSocket.Bind(ipEndPoint);
  49.  
  50. IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
  51. //The epSender identifies the incoming clients
  52. EndPoint epSender = (EndPoint)ipeSender;
  53.  
  54. //Start receiving data
  55. serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
  56. SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
  57.  
  58. txtLog.AppendText("Start Listening on " + txtPort.Text + "\n");
  59. btnListening.Enabled = false;
  60. }
  61. catch (Exception ex)
  62. {
  63. MessageBox.Show(ex.Message, "SGSServerUDP",
  64. MessageBoxButtons.OK, MessageBoxIcon.Error);
  65. }
  66. }
  67.  
  68. private void OnReceive(IAsyncResult ar)
  69. {
  70. memoClientDetail.Clear();
  71. try
  72. {
  73. IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
  74. EndPoint epSender = (EndPoint)ipeSender;
  75.  
  76. serverSocket.EndReceiveFrom(ar, ref epSender);
  77.  
  78. //Transform the array of bytes received from the user into an
  79. //intelligent form of object Data
  80. Data msgReceived = new Data(byteData);
  81.  
  82. //We will send this object in response the users request
  83. Data msgToSend = new Data();
  84.  
  85. byte[] message;
  86.  
  87. switch (msgReceived.cmdCommand)
  88. {
  89. case Command.Login:
  90.  
  91. //When a user logs in to the server then we add her to our
  92. //list of clients
  93.  
  94. ClientInfo clientInfo = new ClientInfo();
  95. clientInfo.endpoint = epSender;
  96. clientInfo.strName = msgReceived.strName;
  97. clientInfo.strClientStatus = msgReceived.strClientStatus;
  98. clientInfo.strClientOS = msgReceived.strClientOS;
  99. clientInfo.strClientApp = msgReceived.strClientApp;
  100.  
  101. clientList.Add(clientInfo);
  102.  
  103. listClient.Items.Add(msgReceived.strName);
  104. memoClientDetail.AppendText(msgReceived.strClientStatus);
  105. memoClientDetail.AppendText(msgReceived.strClientOS);
  106. memoClientDetail.AppendText(msgReceived.strClientApp);
  107. break;
  108.  
  109. case Command.Logout:
  110.  
  111. //When a user wants to log out of the server then we search for her
  112. //in the list of clients and close the corresponding connection
  113.  
  114. int nIndex = 0;
  115. foreach (ClientInfo client in clientList)
  116. {
  117. if (client.strName == msgReceived.strName)
  118. {
  119. clientList.RemoveAt(nIndex);
  120.  
  121. break;
  122. }
  123. ++nIndex;
  124. }
  125.  
  126. listClient.Items.Remove(msgReceived.strName);
  127. break;
  128. }
  129.  
  130. txtLog.AppendText(msgToSend.strMessage + "\r\n");
  131.  
  132. serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
  137. }
  138. }

CLIENT SIDE
  1. enum Command
  2. {
  3. Login, //Log into the server
  4. Logout, //Logout of the server
  5. Null //No command
  6. }
  7.  
  8. public partial class KioskMonitoringClient : Form
  9. {
  10. public Socket clientSocket;
  11. public EndPoint epServer;
  12. public string strName;
  13. public string m_ApplicationName = "Notepad";
  14. public bool m_ApplicationIsRunning = false;
  15.  
  16. public KioskMonitoringClient()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private ArrayList GetProcesses() //This method is used for Generating Current running process
  22. {
  23. ArrayList procList = new ArrayList();
  24. try
  25. {
  26. Process[] process = Process.GetProcesses();
  27. foreach (Process proc in process)
  28. {
  29. procList.Add(proc);
  30. }
  31. return procList;
  32. }
  33. catch (Exception)
  34. {
  35. return null;
  36. }
  37. }
  38.  
  39. private string CheckRunningProcess(string process) // this Function is used for checking if an application (i'm using notepad.exe) is running or not
  40. {
  41. try
  42. {
  43. this.SuspendLayout();
  44. m_ApplicationIsRunning = false;
  45. ArrayList arrProcess = new ArrayList();
  46. arrProcess = this.GetProcesses();
  47.  
  48. Process proc;
  49. for (int i = 0; i < arrProcess.Count; i++)
  50. {
  51. proc = new Process();
  52. proc = (Process)arrProcess[i];
  53. if (String.Compare(process.ToUpper(), proc.ProcessName.ToUpper()) == 0)
  54. {
  55. m_ApplicationIsRunning = true;
  56. break;
  57. }
  58. }
  59. if (m_ApplicationIsRunning)
  60. return "Running";
  61. else
  62. return "Not started";
  63. }
  64. catch (Exception)
  65. {
  66. return "Not started";
  67. }
  68. }
  69.  
  70. private void btnConnect_Click(object sender, EventArgs e) // Connecting to server
  71. {
  72. strName = Dns.GetHostName(); //Get Computer name
  73. try
  74. {
  75. //Using UDP sockets
  76. clientSocket = new Socket(AddressFamily.InterNetwork,
  77. SocketType.Dgram, ProtocolType.Udp);
  78.  
  79. //IP address of the server machine
  80. IPAddress ipAddress = IPAddress.Parse(txtServerIP.Text);
  81. //Server is listening on current txtServerPort.Text
  82. IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(txtServerPort.Text));
  83.  
  84. epServer = (EndPoint)ipEndPoint;
  85.  
  86. Data msgToSend = new Data();
  87. OperatingSystemInfo osInfo = new OperatingSystemInfo();
  88.  
  89. #region " Send Data"
  90. msgToSend.cmdCommand = Command.Login;
  91. msgToSend.strName = strName;
  92. msgToSend.strMessage = null;
  93. msgToSend.strClientStatus = "Client Status : Online\n";
  94. msgToSend.strClientOS = "Operating System Info : " + osInfo.GetOSName() + osInfo.GetOSProductType() + osInfo.GetOSServicePack() + "\n";
  95. msgToSend.strClientApp = "Notepad Application Status : " + this.CheckRunningProcess(m_ApplicationName) + "\n";
  96. #endregion
  97.  
  98. byte[] byteData = msgToSend.ToByte();
  99.  
  100. //Login to the server
  101. clientSocket.BeginSendTo(byteData, 0, byteData.Length,
  102. SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
  103. }
  104. catch (Exception ex)
  105. {
  106. MessageBox.Show(ex.Message, "SGSclient",
  107. MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. }
  109. }
  110.  
  111. private void OnSend(IAsyncResult ar)
  112. {
  113. try
  114. {
  115. clientSocket.EndSend(ar);
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
  120. }
  121. }
  122.  
  123. private void KioskMonitoringClient_Load(object sender, EventArgs e)
  124. {
  125. CheckForIllegalCrossThreadCalls = false;
  126. }
  127.  
  128. private void KioskMonitoringClient_FormClosing(object sender, FormClosingEventArgs e) // Logout from server
  129. {
  130. try
  131. {
  132. //Send a message to logout of the server
  133. Data msgToSend = new Data();
  134. msgToSend.cmdCommand = Command.Logout;
  135. msgToSend.strName = strName;
  136. msgToSend.strMessage = null;
  137.  
  138. byte[] b = msgToSend.ToByte();
  139. clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
  140. clientSocket.Close();
  141. }
  142. catch (ObjectDisposedException)
  143. { }
  144. catch (Exception ex)
  145. {
  146. MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
  147. }
  148. }
Last edited by basslover; Sep 10th, 2008 at 4:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

 
0
  #4
Sep 10th, 2008
OK, so, whats the problem you have?
The most important thing to understand is that UDP is connectionless.. so theres no guarantee it is received.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: basslover is an unknown quantity at this point 
Solved Threads: 0
basslover basslover is offline Offline
Newbie Poster

Re: How To Send Data From Client To Server via Server Side (using C# UDP Socket) ?

 
0
  #5
Sep 10th, 2008
It's OK about connectionless.

Here's the problem.

Right now i'm able to send data from client to server via client side. Using OnSend(IAsyncResult ar) on Client Side and OnReceive(IAsyncResult ar) on Server Side.

But, The problem is, Is it possible to send data from client to server via server side ? Or in other word, Is is possible to me (server) forces the client to send data ?

Let's brainstorming. I'm using Windows Form project right now. I think there will be a 'Refresh' button on Server Side UI in order to make a client (which is currently connected) send data to server. But, .. How ??

However, I have noob method to solve this problem, Which is I'm using timer on Client side to send data periodically according to the timer setting. But, i think it is not an effective way to solve this problem because the client has to send data over and over without request from server.

Thanks for any responses...
Last edited by basslover; Sep 10th, 2008 at 11:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: How To Send Data From Client To Server via Server Side (using C# TCP Socket) ?

 
0
  #6
Sep 11th, 2008
only in the sense of your server can say "I want data now" and your client can do the sending..
but again as its connectionless theres no guarentee the client would get it over UDP and no guarentee even if the client responds that the server would get it
Last edited by LizR; Sep 11th, 2008 at 5:44 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC