1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Xml;
using System.Xml.Linq;
//using XMLGenerator;
using XmlTools;
using System.Collections.Generic;
// Command Message Ping
namespace TestCase2
{
class Program
{
private static StreamWriter sw;
private static bool performPing = true;
static void Main(string[] args)
{
try
{
int myClock;
sw = new StreamWriter("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\Logs\\TestCase2_log_" + DateTime.Now.Millisecond.ToString() + ".xml", true);
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
IPHost.AddressList[0].ToString();
IPAddress localAddress = IPAddress.Parse(IPHost.AddressList[0].ToString());
//IPAddress localAddress = IPAddress.Parse("127.0.0.1");
// Define the kind of socket we want: Internet, Stream, TCP
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Define the address we want to claim: the first IP address found earlier, at port 2200
IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 2200);
// Bind the socket to the end point
listenSocket.Bind(ipEndpoint);
// Start listening, only allow 10 connection to queue at the same time
listenSocket.Listen(10);
listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);
//Console.WriteLine("Server is waiting on socket {0}", listenSocket.LocalEndPoint);
myClock = 0;
while (true)
{
// Write a message and sleep for 2 seconds
//Console.WriteLine("Busy Waiting - Clock: " + myClock.ToString() + " seconds");
Thread.Sleep(2000);
myClock += 2;
if (myClock == 200)
{
sw.Close();
Environment.Exit(1);
}
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception: {0}", e.ToString());
}
}
public static void ReceiveCallback(IAsyncResult AsyncCall)
{
try
{
Socket listener = (Socket)AsyncCall.AsyncState;
Socket client = listener.EndAccept(AsyncCall);
if (performPing)
{
// Sending Command Message Ping
Byte[] message = XmlConverter.ConvertStringToByteArray("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\CommandMessage_Ping_Request.xml");
sw.WriteLine("Sending Ping Command to " + client.RemoteEndPoint.ToString() + "on " + DateTime.Now.Millisecond.ToString());
sw.WriteLine(System.Text.Encoding.UTF8.GetString(message));
Console.WriteLine("Sending Ping Command to {0}", client.RemoteEndPoint);
//Console.WriteLine(System.Text.Encoding.UTF8.GetString(message));
client.Send(message);
performPing = false;
for (int x = 0; x < 4000; x++)
{
// Receiving
byte[] myBuffer = new byte[100000];
client.Receive(myBuffer);
BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
binWriter.Write(myBuffer);
binWriter.Close();
sw.WriteLine("Received Connection from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
Console.ReadKey(true);
Thread.Sleep(50);
x += 1;
}
}
else
{
// Receiving response
byte[] myBuffer2 = new byte[100000];
client.Receive(myBuffer2);
//Console.WriteLine("Receiving response from {0}", client.RemoteEndPoint);
sw.WriteLine("Received response from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));
BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
binWriter.Write(myBuffer2.Length);
binWriter.Write(myBuffer2);
binWriter.Close();
Console.WriteLine("Received response from {0}", client.RemoteEndPoint);
//Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));
}
//// At the end of the connection, we need to tell the OS that we can receive another call
listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
} |
Partager