Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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*/
- //Server:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- namespace ChatServer
- {
- public class Program
- {
- static void Main(string[] args)
- {
- const int port = 54321;
- TcpListener listener = null; //just something so we can close it in the finally-brackets
- try
- {
- //start listen to Port
- Console.WriteLine("Start server...");
- listener = new TcpListener(IPAddress.Any, port); //Every Computer is allowed
- listener.Start();
- Console.WriteLine("Server started!");
- //Buffer for reading data, sadly this time we have to define a specific number, be sure it is enough!
- byte[] buffer = new byte[256]; //1 byte is 1 letter
- while (true) //just an endless loop that is supposed to connect more clients, because it also ends the
- //connection but we'll save this for later, you could ignore it... I think...
- {
- //establish connection
- Console.WriteLine("Listening on port " + port);
- Console.WriteLine("Waiting for connection...");
- TcpClient client = listener.AcceptTcpClient();
- Console.WriteLine("Connection Established!");
- //get the Data-Flow
- NetworkStream inOut = client.GetStream();
- int i; //need this to get the number of bytes
- //Loop to receive data by client. Like "if the bytes (that contain our message) you receive is NOT 0..."
- while ((i = inOut.Read(buffer, 0, buffer.Length)) != 0)
- {
- //convert the bytes to string and show it
- string data = Encoding.ASCII.GetString(buffer, 0, i);
- Console.WriteLine("Received: " + data + " with " + i + " bytes");
- //Write Back the text to the client
- Console.WriteLine("Sending back: " + data);
- inOut.Write(buffer, 0, i); //we don't have to convert it back to bytes because this is what we already received
- }
- //Close connection
- client.Close();
- Console.WriteLine("Connection closed");
- Console.ReadKey();
- }
- }
- catch
- {
- Console.WriteLine("Error");
- }
- finally
- {
- //stop listening for new clients
- listener.Stop();
- }
- }
- }
- }
- //Client
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Security;
- using System.Security.Permissions;
- namespace ChatClient
- {
- public class Program
- {
- static void Main(string[] args)
- {
- TcpClient client = null; //again it is here so we have defined SOMETHING for it to use it later to close the connection
- try
- {
- //type the Server IP
- Console.WriteLine("Insert IP from Server: ");
- string ip = Console.ReadLine();
- //open connection and get something for the DataStream
- client = new TcpClient(ip, 54321);
- NetworkStream inOut = client.GetStream();
- Console.WriteLine("Connection established!");
- while (true) //again an endless loop but this time we need it^^
- {
- //read text to send
- Console.WriteLine("Something to send to server: ");
- string message = Console.ReadLine();
- //convert to bytes and send
- byte[] bytes = ASCIIEncoding.ASCII.GetBytes(message);
- Console.WriteLine("Sending...");
- inOut.Write(bytes, 0, bytes.Length); //byte (the message in bytes), 0 (no idea what), bytes-Length (Length of letters)
- //read back
- byte[] bytesToRead = new byte[client.ReceiveBufferSize];
- int answer = inOut.Read(bytesToRead, 0, client.ReceiveBufferSize);
- //convert and output
- string answerString = ASCIIEncoding.ASCII.GetString(bytesToRead, 0, answer);
- Console.WriteLine("From Server: " + answerString);
- }
- }
- catch
- {
- Console.WriteLine("Error");
- Console.ReadKey();
- }
- finally
- {
- client.Close();
- }
- }
- }
- }
- /* 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