Home Forums Support Send file with progress and stats Reply To: Send file with progress and stats

#1018
Anonymous
Inactive

Hello,
I work at my school, I have a client-server application in C # with Visual Studio 2010 and I have my first client 1 that connects to server. but now I have a problem to connect a server to another client 2 in a VM (Virtual Machine: Virtual Box Windows 7).

Client 2:
* It should be the server connects to the client 2?
* The server sends a message to the client that son located in a virtual machine?
* After sent, the server received a confirmation message from client 2?

So it remains to create of client 2 code that receives a message from the server and client 2 sends a confirmation response to server and client 1?
Please find attached client / server code.
Very thank you in advance and regards.

thank you

client :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DPSBase;
using NetworkCommsDotNet;

namespace SocketProject.Client
{

class Program
{

#region [ Fields ]
private static ConnectionInfo connectionInfo = null;
private static Stopwatch _stopWatch = null;
#endregion

static void Main(string[] args)
{
string serverIP = string.Empty;
int serverPort = 0;
int loopCounter = 1;

TCPConnection serverConnection = null;
string result = string.Empty;

//Enter Params of the connection
Console.WriteLine(“SVP, saisissez l’adresse IP et le port du serveur (127.0.0.1:10000) : “);
Console.WriteLine();
string serverInfo = Console.ReadLine();

serverIP = serverInfo.Split(‘:’).First();
serverPort = int.Parse(serverInfo.Split(‘:’).Last());

while (true)
{
NetworkComms.RemoveGlobalIncomingPacketHandler();
connectionInfo = new ConnectionInfo(serverIP, serverPort);
serverConnection = TCPConnection.GetConnection(connectionInfo);

//Send Message to the others clients
NetworkComms.AppendGlobalIncomingPacketHandler<string>(“SendMessageToOthersClt”, HandleIncomingCltMessage);
//Send Message to the serveur
NetworkComms.AppendGlobalIncomingPacketHandler<string>(“CustomObjectReply”, HandleIncomingSrvMessage);

Console.Write(string.Format(“Entrez votre {0}{1} message : \n “, loopCounter, (loopCounter == 0) ? “er” : “eme”));
string messageToSend = Console.ReadLine();

_stopWatch = new Stopwatch();
if (messageToSend.StartsWith(“SendMsg”))
{
int firstSpace = messageToSend.IndexOf(‘ ‘);
serverConnection.SendObject(“SendMessageToOthersClt”, messageToSend.Substring(firstSpace));
}
else
serverConnection.SendObject(“RequestCustomObject”, messageToSend);

Console.WriteLine();
if (Console.ReadKey(true).Key == ConsoleKey.Q) break;

loopCounter++;
}

NetworkComms.Shutdown();
}

private static void HandleIncomingSrvMessage(PacketHeader header, Connection connection, string incomingMessage)
{
string messageToShow = string.Empty;
string execTime = string.Empty;

messageToShow = “——————————————————————————-\n”;
string[] res = incomingMessage.Trim().Split(‘-’);
if (res.Length > 1)
execTime = ((_stopWatch.Elapsed.Milliseconds * 1000) + int.Parse(res[0])).ToString();
else
execTime = (_stopWatch.Elapsed.Milliseconds * 1000).ToString();

switch (res[1])
{
case “LaunchVM”:
messageToShow += “Virtual Box Lancée après ” + execTime + “(ns)\n”;
break;
case “LaunchVagrant”:
messageToShow += “Vagrant Lancée après ” + execTime + “(ns)\n”;
break;
case “SendMsg”:
messageToShow += “Le message envoyée aprés ” + execTime + “(ns)\n”;
break;
default:
messageToShow += “Le message ‘” + res[1] + “‘ a été reçu par le serveur après ” + execTime + “(ns)\n”;
break;
}
messageToShow += “——————————————————————————-\n”;
Console.Write(messageToShow);
}

private static void HandleIncomingCltMessage(PacketHeader header, Connection connection, string incomingMessage)
{
TimeSpan ts = _stopWatch.Elapsed;
Console.Write(string.Format(“Message reçu de la part d’un autre client : {0} \n “, incomingMessage));
}
}

}

server:
using System;
using NetworkCommsDotNet;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPSBase;
using System.Threading;

namespace SocketProject.Server
{
class Program
{
static void Main(string[] args)
{
NetworkComms.AppendGlobalIncomingPacketHandler<string>(“RequestCustomObject”, (packetHeader, connection, message) =>
{
Stopwatch stopWatchLaunchVagrant = new Stopwatch();
stopWatchLaunchVagrant.Start();

string msgConsole = string.Empty;
string elapsedTime = “”;

string process = “”;
string workingDirectory = “”;
string arg = “”;

msgConsole += “—————————————————————————–\n”;
msgConsole += string.Format(“Message reçu de la part Client {0}, le contenu est : {1} \n”, “[” + connection.ConnectionInfo.NetworkIdentifier.Value + ” ” + connection.ConnectionInfo.LocalEndPoint.Address.ToString() + “:” + connection.ConnectionInfo.LocalEndPoint.Port.ToString() + “]“, message);

if (message == “LaunchVM”)
{
process = @”VirtualBox.exe”;
workingDirectory = @”C:\Program Files\Oracle\VirtualBox\”;
}
else if (message == “LaunchVagrant”)
{
process = “cmd.exe”;
workingDirectory = @”C:\Vagrant\”;
arg = “vagrant up”;
}
else if (message == “LaunchVagrantDistant”)
{
process = “cmd.exe”;
workingDirectory = @”\\dauphine-PC\Vagrant\”;
arg = “vagrant up”;
}
else if (message == “test”)
{
process = “devenv.exe”;
workingDirectory = @”C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\”;
}

ProcessStartInfo psi = new ProcessStartInfo(process)
{
UseShellExecute = true,
RedirectStandardOutput = false,
RedirectStandardInput = false,
RedirectStandardError = false,
CreateNoWindow = true,
WorkingDirectory = workingDirectory
};
if (!(string.IsNullOrEmpty(arg)))
psi.Arguments = “/C ” + arg;
//Process.Start(psi);
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);

stopWatchLaunchVagrant.Stop();
elapsedTime = (p.TotalProcessorTime.TotalMilliseconds * 1000 + stopWatchLaunchVagrant.ElapsedMilliseconds * 1000).ToString();

msgConsole += “Temps d’exécution est : ” + elapsedTime + “\n”;
msgConsole += “—————————————————————————–\n”;

Console.Write(msgConsole);

connection.SendObject(“CustomObjectReply”, elapsedTime + “-” + message);
});

NetworkComms.AppendGlobalIncomingPacketHandler<string>(“SendMessageToOthersClt”, (packetHeader, connection, message) =>
{
string msgConsole = string.Empty;
msgConsole += “—————————————————————————–\n”;
msgConsole += string.Format(“Le Client {0} a envoyé ce message : \” {1} \” aux autres clients \n”, “[” + connection.ConnectionInfo.NetworkIdentifier.Value + ” ” + connection.ConnectionInfo.LocalEndPoint.Address.ToString() + “:” + connection.ConnectionInfo.LocalEndPoint.Port.ToString() + “]“, message);
var allRelayConnections = (from current in NetworkComms.GetExistingConnection() where current != connection select current).ToArray();
foreach (var relayConnection in allRelayConnections)
{
try
{
relayConnection.SendObject(“SendMessageToOthersClt”, message);
}
catch (CommsException) { /* Catch the comms exception, ignore and continue */ }
}
msgConsole += “—————————————————————————–\n”;
Console.Write(msgConsole);
});

NetworkComms.AppendGlobalConnectionEstablishHandler((connection) => { Console.Write(string.Format(” Nouveau client connecté : [IP : {0}, PORT : {1}] \n”, connection.ConnectionInfo.LocalEndPoint.Address.ToString(), connection.ConnectionInfo.LocalEndPoint.Port.ToString())); });

TCPConnection.StartListening(true);
foreach (System.Net.IPEndPoint localEndPoint in TCPConnection.ExistingLocalListenEndPoints())
Console.WriteLine(“Serveur demarré – Infos : {0}:{1}”, localEndPoint.Address, localEndPoint.Port);
Console.WriteLine(“\n Cliquez sur une touche pour fermer le serveur.”);
Console.ReadKey(true);
NetworkComms.Shutdown();
}

public static string elapsedTime { get; set; }

}
}