TCP Socket Tutorial (C#, Visual Studio, Networked Console Apps)

Pubblicato il: 20 agosto 2019
sul canale di: wolfs cry games
59,642
761

In this video, you'll learn how to send data over the internet from a client-sided app to a server-sided app. This data transfer allows you to send byte[] or string data between devices and the setup lets your two devices have a conversation. It's essential to know this if you're trying to set up a low level (high control) server.

Edit: This is the change to make if you want to make your server accessible to public. They will connect to your public IP and you have to set up port forwarding on your router. The screenshot is how the server code changes.
https://drive.google.com/file/d/1T_rn...

Many current networking solutions out there will allow you to send data through "magic" so I hope to clarify what's going on in those solutions through this video.



Here's the code discussed in the tutorial. As a commenter pointed out, one line was unnecessary, so I consolidated two lines into one to eliminate it.

client app code

using System;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace ClientSocketApp
{
class Program
{

static void Main(string[] args)
{
connection:
try
{
TcpClient client = new TcpClient("127.0.0.1", 1302);
string messageToSend = "My name is Neo";
int byteCount = Encoding.ASCII.GetByteCount(messageToSend + 1);
byte[] sendData = Encoding.ASCII.GetBytes(messageToSend);

NetworkStream stream = client.GetStream();
stream.Write(sendData, 0, sendData.Length);
Console.WriteLine("sending data to server...");

StreamReader sr = new StreamReader(stream);
string response = sr.ReadLine();
Console.WriteLine(response);

stream.Close();
client.Close();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("failed to connect...");
goto connection;
}
}
}
}


server app code
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace ServerSocketApp
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 1302);
listener.Start();
while (true)
{
Console.WriteLine("Waiting for a connection.");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Client accepted.");
NetworkStream stream = client.GetStream();
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
try
{
byte[] buffer = new byte[1024];
stream.Read(buffer, 0, buffer.Length);
int recv = 0;
foreach (byte b in buffer)
{
if (b!=0)
{
recv++;
}
}
string request = Encoding.UTF8.GetString(buffer, 0, recv);
Console.WriteLine("request received: "+ request);
sw.WriteLine("You rock!");
sw.Flush();
}
catch(Exception e)
{
Console.WriteLine("Something went wrong.");
sw.WriteLine(e.ToString());
}
}
}
}
}


In questa pagina del sito puoi guardare il video online TCP Socket Tutorial (C#, Visual Studio, Networked Console Apps) della durata di ore minuti seconda in buona qualità , che l'utente ha caricato wolfs cry games 20 agosto 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 59,642 volte e gli è piaciuto 761 spettatori. Buona visione!