Tician

C# Networking Server/Client part 1

Jul 15th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. /*First of all you have your project in the project-explorer. To code 2 projects at the same time (and compile them together) you right click the solution in the solution explorer, say "add" and then "new project". Now we have 2 windows to put code into. To compile them together right click the whole solution in the solution explorer again and go to "properties". In the common Properties look for "Startup project" and put the dot into "Multiple Startup Projects". Then set the projects that should start when compiling to "Start" in the appropriated actions.*/
  2.  
  3. /*The first and easiest networking Program looks like this. It sends a defined text to the server, the server sends it back and the client is showing it. This should be a start. The requirement here is to know what IP and Port is.*/
  4.  
  5. //Server:
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.IO;
  12. using System.Net.Sockets;
  13. using System.Net;
  14. using System.Threading;
  15. //not sure if you really need them all...
  16.  
  17. namespace Server
  18. {
  19.     class Program
  20.     {       //"It is a good habit to use "const" for variables that are not supposed to change" - Tessyrah
  21.         const int port = 54321; /*check out google for ports, you can nearly choose any - but only nearly. You can
  22.                                 imagine it as a frequency on which you want to send your data*/
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             //We define the listener, the piece that is waiting for any PCs that want to connect through the port
  27.             TcpListener listener = new TcpListener(IPAddress.Any, port);
  28.             Console.WriteLine("Listening...");
  29.             listener.Start();
  30.            
  31.             //Next to the one who is just listening, this is what we call the Socket. The Socket is the connection between
  32.             //the Server and the CLient
  33.             TcpClient client = listener.AcceptTcpClient();
  34.  
  35.             //We have the listener, the connection itself, now we define the Exchange of the Data and the buffer we need to read it
  36.             NetworkStream nwStream = client.GetStream();
  37.             byte[] buffer = new byte[client.ReceiveBufferSize];
  38.  
  39.             //store the bytes we get in this integer
  40.             int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
  41.  
  42.             //convert the data received into a string
  43.             string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
  44.             Console.WriteLine("Received : " + dataReceived);
  45.  
  46.             //write back the text to the client, stop the listener and close the connection
  47.             Console.WriteLine("Sending back : " + dataReceived);
  48.             nwStream.Write(buffer, 0, bytesRead);
  49.             client.Close();
  50.             listener.Stop();
  51.             Console.ReadLine();
  52.         }
  53.     }
  54. }
  55.  
  56. //Client
  57. using System;
  58. using System.Collections.Generic;
  59. using System.Linq;
  60. using System.Text;
  61. using System.Threading.Tasks;
  62. using System.IO;
  63. using System.Net.Sockets;
  64. using System.Net;
  65. using System.Threading;
  66.  
  67. namespace Client
  68. {
  69.     class Program
  70.     {
  71.         //Because I had the server and Client running on different machines I needed to type in the IP of the server
  72.         const int port = 54321;
  73.         Console.WriteLine("Insert IP from Server: ");
  74.         string ip = Console.ReadLine();
  75.        
  76.         static void Main(string[] args)
  77.         {
  78.             //data to send to the server
  79.             string textToSend = "Hello network";
  80.  
  81.             //(no need for a listener here) We need the Socket again and the stream of data + we convert the text into bytes
  82.             TcpClient client = new TcpClient(ip, port);
  83.             NetworkStream nwStream = client.GetStream();
  84.             byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
  85.  
  86.             //sending the text
  87.             Console.WriteLine("Sending : " + textToSend);
  88.             nwStream.Write(bytesToSend, 0, bytesToSend.Length);
  89.  
  90.             //read back the text
  91.             byte[] bytesToRead = new byte[client.ReceiveBufferSize];
  92.             int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
  93.             Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
  94.             Console.ReadLine();
  95.             client.Close();
  96.         }
  97.     }
  98. }
  99.  
  100. //To see how to use a loop to receive and send back more than just one piece of information look for my other Pastebin!
Add Comment
Please, Sign In to add comment