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
|
private static AutoResetEvent autoConnectEvent = new AutoResetEvent(false);
private Boolean connected = false;
private Socket clientSocket;
private IPEndPoint endPoint;
private const Int32 ReceiveOperation = 1, SendOperation = 0;
private static AutoResetEvent[] autoSendReceiveEvents = new AutoResetEvent[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
public void loadData()
{
endPoint = new IPEndPoint(IPAddress.Parse("192.168.5.195"), 4503);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.UserToken = clientSocket;
args.RemoteEndPoint = endPoint;
args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
if (!clientSocket.ConnectAsync(args))
{
if (args.SocketError != SocketError.Success)
{
HtmlPage.Window.Alert("Echec de la connexion du socket au servuer ");
}
}
}
// Calback for connect operation
private void OnConnect(object sender, SocketAsyncEventArgs e)
{
HtmlPage.Window.Alert("Début onConnect");
// Signals the end of connection.
autoConnectEvent.Set();
HtmlPage.Window.Alert("Socket Error : " + e.SocketError);
// Set the flag for socket connected.
connected = (e.SocketError == SocketError.Success);
}
// Exchange a message with the host.
internal String SendReceive(String message)
{
HtmlPage.Window.Alert("Début send Receive");
if (connected)
{
HtmlPage.Window.Alert("je suis bien connecté !");
// Create a buffer to send.
Byte[] sendBuffer = Encoding.Unicode.GetBytes(message);
// Prepare arguments for send/receive operation.
SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();
completeArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
completeArgs.UserToken = clientSocket;
completeArgs.RemoteEndPoint = endPoint;
completeArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);
// Start sending asyncronally.
clientSocket.SendAsync(completeArgs);
// Wait for the send/receive completed.
AutoResetEvent.WaitAll(autoSendReceiveEvents);
// Return data from SocketAsyncEventArgs buffer.
return Encoding.Unicode.GetString(completeArgs.Buffer, completeArgs.Offset, completeArgs.BytesTransferred);
}
else
{
HtmlPage.Window.Alert("Je ne suis pas connecté !");
//throw new SocketException((Int32)SocketError.NotConnected);
return "";
}
}
private void OnSend(object sender, SocketAsyncEventArgs e)
{
// Signals the end of send.
autoSendReceiveEvents[ReceiveOperation].Set();
if (e.SocketError == SocketError.Success)
{
if (e.LastOperation == SocketAsyncOperation.Send)
{
// Prepare receiving.
Socket s = e.UserToken as Socket;
byte[] receiveBuffer = new byte[255];
e.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
e.Completed +=
new EventHandler<SocketAsyncEventArgs>(OnReceive);
s.ReceiveAsync(e);
}
}
else
{
ProcessError(e);
}
}
private void ProcessError(SocketAsyncEventArgs e)
{
HtmlPage.Window.Alert("Dans process Error");
Socket s = e.UserToken as Socket;
if (s.Connected)
{
// close the socket associated with the client
try
{
s.Shutdown(SocketShutdown.Both);
}
catch (Exception)
{
// throws if client process has already closed
}
finally
{
if (s.Connected)
{
s.Close();
}
}
}
// Throw the SocketException
throw new SocketException((Int32)e.SocketError);
}
private void OnReceive(object sender, SocketAsyncEventArgs e)
{
// Signals the end of receive.
autoSendReceiveEvents[SendOperation].Set();
} |
Partager