Advertisement
Tician

C# Networking Server/Client part 2

Jul 15th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. /* This is my second Pastebin for networking with C#. This is what I could find out on how to use Loops to send and Receive Data from Server and Client*/
  2.  
  3. //Server:
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.IO;
  10. using System.Net.Sockets;
  11. using System.Net;
  12. using System.Threading;
  13.  
  14. namespace ChatServer
  15. {
  16.     public class Program
  17.     {
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             const int port = 54321;
  22.             TcpListener listener = null; //just something so we can close it in the finally-brackets
  23.  
  24.             try
  25.             {
  26.                 //start listen to Port
  27.                 Console.WriteLine("Start server...");
  28.                 listener = new TcpListener(IPAddress.Any, port); //Every Computer is allowed
  29.                 listener.Start();
  30.                 Console.WriteLine("Server started!");
  31.  
  32.                 //Buffer for reading data, sadly this time we have to define a specific number, be sure it is enough!
  33.                 byte[] buffer = new byte[256]; //1 byte is 1 letter
  34.  
  35.                 while (true) //just an endless loop that is supposed to connect more clients, because it also ends the
  36.                                 //connection but we'll save this for later, you could ignore it... I think...
  37.                 {
  38.                     //establish connection
  39.                     Console.WriteLine("Listening on port " + port);
  40.                     Console.WriteLine("Waiting for connection...");
  41.                     TcpClient client = listener.AcceptTcpClient();
  42.                     Console.WriteLine("Connection Established!");
  43.  
  44.                     //get the Data-Flow
  45.                     NetworkStream inOut = client.GetStream();
  46.  
  47.                     int i; //need this to get the number of bytes
  48.  
  49.                     //Loop to receive data by client. Like "if the bytes (that contain our message) you receive is NOT 0..."
  50.                     while ((i = inOut.Read(buffer, 0, buffer.Length)) != 0)
  51.                     {
  52.                         //convert the bytes to string and show it
  53.                         string data = Encoding.ASCII.GetString(buffer, 0, i);
  54.                         Console.WriteLine("Received: " + data + " with " + i + " bytes");
  55.  
  56.                         //Write Back the text to the client
  57.                         Console.WriteLine("Sending back: " + data);
  58.                         inOut.Write(buffer, 0, i); //we don't have to convert it back to bytes because this is what we already received
  59.                     }
  60.                     //Close connection
  61.                     client.Close();
  62.                     Console.WriteLine("Connection closed");
  63.                     Console.ReadKey();
  64.                 }      
  65.             }
  66.             catch
  67.             {
  68.                 Console.WriteLine("Error");
  69.             }
  70.             finally
  71.             {
  72.                 //stop listening for new clients
  73.                 listener.Stop();
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. //Client
  80. using System;
  81. using System.Collections.Generic;
  82. using System.Linq;
  83. using System.Text;
  84. using System.Threading.Tasks;
  85. using System.Threading;
  86. using System.IO;
  87. using System.Net;
  88. using System.Net.Sockets;
  89. using System.Security;
  90. using System.Security.Permissions;
  91.  
  92. namespace ChatClient
  93. {
  94.     public class Program
  95.     {
  96.         static void Main(string[] args)
  97.         {
  98.             TcpClient client = null; //again it is here so we have defined SOMETHING for it to use it later to close the connection
  99.  
  100.             try
  101.             {
  102.                 //type the Server IP
  103.                 Console.WriteLine("Insert IP from Server: ");
  104.                 string ip = Console.ReadLine();
  105.  
  106.                 //open connection and get something for the DataStream
  107.                 client = new TcpClient(ip, 54321);
  108.                 NetworkStream inOut = client.GetStream();
  109.                 Console.WriteLine("Connection established!");
  110.  
  111.                 while (true) //again an endless loop but this time we need it^^
  112.                 {
  113.                     //read text to send
  114.                     Console.WriteLine("Something to send to server: ");
  115.                     string message = Console.ReadLine();
  116.  
  117.                     //convert to bytes and send
  118.                     byte[] bytes = ASCIIEncoding.ASCII.GetBytes(message);
  119.                     Console.WriteLine("Sending...");
  120.                     inOut.Write(bytes, 0, bytes.Length); //byte (the message in bytes), 0 (no idea what), bytes-Length (Length of letters)
  121.  
  122.                     //read back
  123.                     byte[] bytesToRead = new byte[client.ReceiveBufferSize];
  124.                     int answer = inOut.Read(bytesToRead, 0, client.ReceiveBufferSize);
  125.  
  126.                     //convert and output
  127.                     string answerString = ASCIIEncoding.ASCII.GetString(bytesToRead, 0, answer);
  128.                     Console.WriteLine("From Server: " + answerString);
  129.                 }              
  130.             }
  131.             catch
  132.             {
  133.                 Console.WriteLine("Error");
  134.                 Console.ReadKey();
  135.             }
  136.             finally
  137.             {
  138.                 client.Close();
  139.             }
  140.         }
  141.     }
  142. }
  143.  
  144. /* I hope this helps some people to understand more it took me a damn lot of time to find all this pieces of information and put it into something I can understand myself. Part 3 will come soon, it will also be a loop but I want to add Threading so our Server can handle each Client better. And I wanna do what so many beginners tried: To have a sprite (like a dot) in one Client that you can move with arrow-Keys (I already have this though), but I want it to synchronize with a second Client (and a second dot of course), so 2 Client can see their own dot and the other one moving around*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement