Skip to content
Snippets Groups Projects
Commit 919574ce authored by Anthony Noir's avatar Anthony Noir
Browse files

test(prototype): add

parent 97f30f25
No related branches found
No related tags found
7 merge requests!104devV2 comm,!62Feature - Rejoindre Partie,!61Modification de la classe Comm_calls_Data_Server_Impl afin que...,!53feat : Intégration IHM-Main,!52Dev,!23Rebase comm with data classes,!1test(prototype): add
using Newtonsoft.Json;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace network_test
{
class Client
{
TcpClient clientSocket;
public Client()
{
//create a TCP Client
this.clientSocket = new TcpClient();
}
public void run(string ip, int port)
{
//try to connect
do
{
try
{
this.clientSocket.Connect(ip, port);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
} while (!this.clientSocket.Connected);
//listen to server
Thread tcpListenerThread = new Thread(receiveMessage);
tcpListenerThread.Start();
//send message to the server
string text;
do
{
text = Console.ReadLine();
try
{
sendMessage(text);
}catch (Exception e)
{
Console.WriteLine(e.Message);
text = "/quit";
}
} while (text != "/quit");
//quit
//Console.ReadLine();
tcpListenerThread.Abort();
this.clientSocket.Close();
}
private void sendMessage(string text)
{
Message msg = new Message(text, "client");
NetworkStream nwStream = this.clientSocket.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg));
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
}
private void receiveMessage()
{
while (true)
{
try
{
byte[] bytesToRead = new byte[this.clientSocket.ReceiveBufferSize];
NetworkStream nwStream = this.clientSocket.GetStream();
int bytesRead = nwStream.Read(bytesToRead, 0, this.clientSocket.ReceiveBufferSize);
Message resp = JsonConvert.DeserializeObject<Message>(Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
resp.print();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace network_test {
class Message {
public string content;
public string sender;
public DateTime date;
public Message(string content = "", string sender = "") {
this.content = content;
this.sender = sender;
this.date = DateTime.Now;
}
public void print()
{
Console.WriteLine("[" + this.date.ToString() + "] " + sender + " : " + content);
}
}
}
using System;
namespace network_test
{
class Program
{
static void Main(string[] args)
{
string ip = "127.0.0.1";
int port = 10000;
string choice;
do
{
Console.WriteLine("s : server\nc : client\n");
choice = Console.ReadLine();
} while (choice != "c" && choice != "s");
if (choice == "s")
{
Server server = new Server();
server.run(ip, port);
}
else
{
Client client = new Client();
client.run(ip, port);
}
}
}
}
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using Newtonsoft.Json;
namespace network_test
{
class Server
{
int currentId; //id given to the client
ConcurrentBag<ClientHandler> clients;
public Server()
{
this.currentId = 0;
this.clients = new ConcurrentBag<ClientHandler>();
}
public void run(string ip, int port)
{
//setup server
TcpListener listener = new TcpListener(IPAddress.Parse(ip), port);
TcpClient client = default;
listener.Start();
Console.WriteLine("Listening...");
//handle stop server
Console.CancelKeyPress += delegate {
Console.WriteLine("Quit");
listener.Stop();
};
//listen to new clients
while (true)
{
client = listener.AcceptTcpClient();
//start new Thread
clients.Add(new ClientHandler(client, this.currentId++, this));
}
}
public void broadcast(Message msg)
{
foreach (var client in this.clients)
{
client.sendMessage(msg);
}
}
}
class ClientHandler
{
TcpClient client;
int id;
Server server;
public ClientHandler(TcpClient client, int id, Server server)
{
this.id = id;
this.client = client;
this.server = server;
//this.server = server;
Console.WriteLine("Client " + id + " connected");
Thread thread = new Thread(handling);
thread.Start();
}
private void handling()
{
while (true)
{
try
{
//read message
Message msg = this.receiveMessage();
msg.sender = "Client " + id;
msg.print();
this.server.broadcast(msg);
}
catch (Exception)
{
Console.WriteLine("Client " + this.id + " disconnected");
break;
}
}
}
private Message receiveMessage()
{
NetworkStream nwStream = this.client.GetStream();
byte[] buffer = new byte[this.client.ReceiveBufferSize];
int bytesRead = nwStream.Read(buffer, 0, this.client.ReceiveBufferSize);
Message msg = JsonConvert.DeserializeObject<Message>(Encoding.ASCII.GetString(buffer, 0, bytesRead));
return msg;
}
public void sendMessage(Message msg)
{
try
{
NetworkStream nwStream = this.client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg));
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
}catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment