Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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.*/
- /*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.*/
- //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;
- //not sure if you really need them all...
- namespace Server
- {
- class Program
- { //"It is a good habit to use "const" for variables that are not supposed to change" - Tessyrah
- const int port = 54321; /*check out google for ports, you can nearly choose any - but only nearly. You can
- imagine it as a frequency on which you want to send your data*/
- static void Main(string[] args)
- {
- //We define the listener, the piece that is waiting for any PCs that want to connect through the port
- TcpListener listener = new TcpListener(IPAddress.Any, port);
- Console.WriteLine("Listening...");
- listener.Start();
- //Next to the one who is just listening, this is what we call the Socket. The Socket is the connection between
- //the Server and the CLient
- TcpClient client = listener.AcceptTcpClient();
- //We have the listener, the connection itself, now we define the Exchange of the Data and the buffer we need to read it
- NetworkStream nwStream = client.GetStream();
- byte[] buffer = new byte[client.ReceiveBufferSize];
- //store the bytes we get in this integer
- int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
- //convert the data received into a string
- string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
- Console.WriteLine("Received : " + dataReceived);
- //write back the text to the client, stop the listener and close the connection
- Console.WriteLine("Sending back : " + dataReceived);
- nwStream.Write(buffer, 0, bytesRead);
- client.Close();
- listener.Stop();
- Console.ReadLine();
- }
- }
- }
- //Client
- 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 Client
- {
- class Program
- {
- //Because I had the server and Client running on different machines I needed to type in the IP of the server
- const int port = 54321;
- Console.WriteLine("Insert IP from Server: ");
- string ip = Console.ReadLine();
- static void Main(string[] args)
- {
- //data to send to the server
- string textToSend = "Hello network";
- //(no need for a listener here) We need the Socket again and the stream of data + we convert the text into bytes
- TcpClient client = new TcpClient(ip, port);
- NetworkStream nwStream = client.GetStream();
- byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
- //sending the text
- Console.WriteLine("Sending : " + textToSend);
- nwStream.Write(bytesToSend, 0, bytesToSend.Length);
- //read back the text
- byte[] bytesToRead = new byte[client.ReceiveBufferSize];
- int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
- Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
- Console.ReadLine();
- client.Close();
- }
- }
- }
- //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