Client


SUBMITTED BY: Guest

DATE: Aug. 5, 2014, 9:15 p.m.

FORMAT: C#

SIZE: 4.1 kB

HITS: 24465

  1. //Credits 100% to Tarek Adel (Server)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13. namespace Server___Tutorial
  14. {
  15. public partial class Form1 : Form
  16. {
  17. int i;
  18. TcpListener server = new TcpListener(IPAddress.Any, 1980); // Creates a TCP Listener To Listen to Any IPAddress trying to connect to the program with port 1980
  19. NetworkStream stream; //Creats a NetworkStream (used for sending and receiving data)
  20. TcpClient client; // Creates a TCP Client
  21. byte[] datalength = new byte[4]; // creates a new byte with length 4 ( used for receivng data's lenght)
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. public void ServerReceive()
  27. {
  28. stream = client.GetStream(); //Gets The Stream of The Connection
  29. new Thread(() => // Thread (like Timer)
  30. {
  31. while ((i = stream.Read(datalength, 0, 4)) != 0)//Keeps Trying to Receive the Size of the Message or Data
  32. {
  33. // how to make a byte E.X byte[] examlpe = new byte[the size of the byte here] , i used BitConverter.ToInt32(datalength,0) cuz i received the length of the data in byte called datalength :D
  34. byte[] data = new byte[BitConverter.ToInt32(datalength, 0)]; // Creates a Byte for the data to be Received On
  35. stream.Read(data, 0, data.Length); //Receives The Real Data not the Size
  36. this.Invoke((MethodInvoker)delegate // To Write the Received data
  37. {
  38. txtLog.Text += System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data); // Encoding.Default.GetString(data); Converts Bytes Received to String
  39. });
  40. }
  41. }).Start(); // Start the Thread
  42. }
  43. public void ServerSend(string msg)
  44. {
  45. stream = client.GetStream(); //Gets The Stream of The Connection
  46. byte[] data; // creates a new byte without mentioning the size of it cuz its a byte used for sending
  47. data = Encoding.Default.GetBytes(msg); // put the msg in the byte ( it automaticly uses the size of the msg )
  48. int length = data.Length; // Gets the length of the byte data
  49. byte[] datalength = new byte[4]; // Creates a new byte with length of 4
  50. datalength = BitConverter.GetBytes(length); //put the length in a byte to send it
  51. stream.Write(datalength, 0, 4); // sends the data's length
  52. stream.Write(data, 0, data.Length); //Sends the real data
  53. }
  54. private void btnListen_Click(object sender, EventArgs e)
  55. {
  56. server.Start(); // Starts Listening to Any IPAddress trying to connect to the program with port 1980
  57. MessageBox.Show("Waiting For Connection");
  58. new Thread(() => // Creates a New Thread (like a timer)
  59. {
  60. client = server.AcceptTcpClient(); //Waits for the Client To Connect
  61. MessageBox.Show("Connected To Client");
  62. if (client.Connected) // If you are connected
  63. {
  64. ServerReceive(); //Start Receiving
  65. }
  66. }).Start();
  67. }
  68. private void btnSend_Click(object sender, EventArgs e)
  69. {
  70. if (client.Connected) // if the client is connected
  71. {
  72. ServerSend(txtSend.Text); // uses the Function ClientSend and the msg as txtSend.Text
  73. }
  74. }
  75. }
  76. }
  77. //Credits 100% to Tarek Adel (Server)

comments powered by Disqus