using System;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
public class Client
{
// used to pass state information to delegate
class StateObject
{
internal byte[] sBuffer;
internal Socket sSocket;
internal StateObject(int size, Socket sock) {
sBuffer = new byte[size];
sSocket = sock;
}
}
static void Main(string[] argHostName)
{
try
{
Dialog form = new Dialog(argHostName[0]);
}
catch(System.IndexOutOfRangeException)
{
Console.Write("Usage: client.exe IP_ADDRESS");
MessageBox.Show("Usage: client.exe IP_ADDRESS");
return;
}
Dialog form = new Dialog(argHostName[0]);
Application.Run(form);
}
}
public class Dialog : Form
{
private System.Windows.Forms.Button BtNewGame;
private System.Windows.Forms.Button BtConnect;
private System.Windows.Forms.PictureBox [,] Cell;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
static string hostName;
static Socket clientSocket;
static IPAddress ipAddress;
static IPEndPoint ipEndpoint;
public Dialog(string text)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
hostName = text;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public bool ModRes = false;
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
public static void sendCallback(IAsyncResult asyncSend)
{
Socket clientSocket = (Socket)asyncSend.AsyncState;
int bytesSent = clientSocket.EndSend(asyncSend);
Console.WriteLine(
"{0} bytes sent.",
bytesSent.ToString() );
}
/*
void SendMessage(int index)
{
SendMessageB(index);
ReceiveMessageB();
}
int ReceiveMessage()
{
int index = ReceiveMessageB();
SendMessageB(0);
return(index);
}
*/
void SendMessage(int index)
{
byte[] sendBuffer = Encoding.ASCII.GetBytes(index.ToString());
IAsyncResult asyncSend = clientSocket.BeginSend(
sendBuffer,
0,
sendBuffer.Length,
SocketFlags.None,
new AsyncCallback(sendCallback),
clientSocket);
while(asyncSend.IsCompleted==false) { }
}
int ReceiveMessage()
{
byte [] sBuffer = new byte[25];
IAsyncResult asyncReceive =
clientSocket.BeginReceive(
sBuffer,
0,
sBuffer.Length,
SocketFlags.None,
new AsyncCallback(receiveCallback),
clientSocket);
while(asyncReceive.IsCompleted==false) { }
//Console.Write(Convert.ToInt32(Encoding.ASCII.GetString(sBuffer)));
int res = Convert.ToInt32(Encoding.ASCII.GetString(sBuffer));
return(res);
}
public static void
receiveCallback(IAsyncResult asyncReceive)
{
Socket sSocket =
(Socket)asyncReceive.AsyncState;
int bytesReceived =
sSocket.EndReceive(asyncReceive);
Console.WriteLine(
"{0} bytes received",
bytesReceived.ToString() );
}
void UpdateField(int i, int j, System.Windows.Forms.MouseEventArgs argm)
{
if(argm.Button.ToString()=="Left")
{
SendMessage(0);
ReceiveMessage(); //
SendMessage(i);
ReceiveMessage();
SendMessage(j); //
int bomb;
bomb = ReceiveMessage();
int quantity;
if(bomb==0)
{
//System.Random temp = new Random(
SendMessage(0); //
quantity = ReceiveMessage();
if(quantity > 3)MessageBox.Show("Сервер не соблюдает протокол. Число бомб вокруг не может быть >3");
this.Cell[i,j].Image = Image.FromFile(quantity.ToString() + ".bmp");
this.Cell[i,j].Height = this.Cell[i,j].Image.Height;
this.Cell[i,j].Width = this.Cell[i,j].Image.Width;
}
else if(bomb==1)
{
for(int x =0; x<=4;x++)
{
for(int y =0; y<=4;y++)
{
this.Controls.Remove(this.Cell[x,y]);
}
}
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
MessageBox.Show("Game Over");
this.Controls.Add(BtConnect);
}
else MessageBox.Show("Сервер не соблюдает протокол! Знак присутствия бомбы 1, а отсутствия -0. Других значений не может быть.");
}
else if(argm.Button.ToString()=="Right")
{
SendMessage(1);
ReceiveMessage(); //
SendMessage(i);
ReceiveMessage(); //
SendMessage(j);
this.Cell[i,j].Image = Image.FromFile("flag.bmp");
this.Cell[i,j].Height = this.Cell[i,j].Image.Height;
this.Cell[i,j].Width = this.Cell[i,j].Image.Width;
int won = ReceiveMessage();
if(won==1)
{
for(int x =0; x<=4;x++)
{
for(int y =0; y<=4;y++)
{
this.Controls.Remove(this.Cell[x,y]);
}
}
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
MessageBox.Show("You won!");
this.Controls.Add(BtConnect);
}
}
}
private void Taped00(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(0,0, argm);
}
private void Taped01(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(0,1, argm);
}
private void Taped02(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(0,2, argm);
}
private void Taped03(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(0,3, argm);
}
private void Taped04(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(0,4, argm);
}
private void Taped10(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(1,0, argm);
}
private void Taped11(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(1,1, argm);
}
private void Taped12(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(1,2, argm);
}
private void Taped13(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(1,3, argm);
}
private void Taped14(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(1,4, argm);
}
private void Taped20(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(2,0, argm);
}
private void Taped21(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(2,1, argm);
}
private void Taped22(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(2,2, argm);
}
private void Taped23(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(2,3, argm);
}
private void Taped24(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(2,4, argm);
}
private void Taped30(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(3,0, argm);
}
private void Taped31(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(3,1, argm);
}
private void Taped32(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(3,2, argm);
}
private void Taped33(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(3,3, argm);
}
private void Taped34(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(3,4, argm);
}
private void Taped40(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(4,0, argm);
}
private void Taped41(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(4,1, argm);
}
private void Taped42(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(4,2, argm);
}
private void Taped43(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(4,3, argm);
}
private void Taped44(object myobject, System.Windows.Forms.MouseEventArgs argm)
{
UpdateField(4,4, argm);
}
private void InitializeComponent()
{
this.BtNewGame = new System.Windows.Forms.Button();
this.BtConnect = new System.Windows.Forms.Button();
this.Cell = new System.Windows.Forms.PictureBox[5,5];
this.Cell[0,0] = new System.Windows.Forms.PictureBox();
this.Cell[1,0] = new System.Windows.Forms.PictureBox();
this.Cell[2,0] = new System.Windows.Forms.PictureBox();
this.Cell[3,0] = new System.Windows.Forms.PictureBox();
this.Cell[4,0] = new System.Windows.Forms.PictureBox();
this.Cell[0,1] = new System.Windows.Forms.PictureBox();
this.Cell[1,1] = new System.Windows.Forms.PictureBox();
this.Cell[2,1] = new System.Windows.Forms.PictureBox();
this.Cell[3,1] = new System.Windows.Forms.PictureBox();
this.Cell[4,1] = new System.Windows.Forms.PictureBox();
this.Cell[0,2] = new System.Windows.Forms.PictureBox();
this.Cell[1,2] = new System.Windows.Forms.PictureBox();
this.Cell[2,2] = new System.Windows.Forms.PictureBox();
this.Cell[3,2] = new System.Windows.Forms.PictureBox();
this.Cell[4,2] = new System.Windows.Forms.PictureBox();
this.Cell[0,3] = new System.Windows.Forms.PictureBox();
this.Cell[1,3] = new System.Windows.Forms.PictureBox();
this.Cell[2,3] = new System.Windows.Forms.PictureBox();
this.Cell[3,3] = new System.Windows.Forms.PictureBox();
this.Cell[4,3] = new System.Windows.Forms.PictureBox();
this.Cell[0,4] = new System.Windows.Forms.PictureBox();
this.Cell[1,4] = new System.Windows.Forms.PictureBox();
this.Cell[2,4] = new System.Windows.Forms.PictureBox();
this.Cell[3,4] = new System.Windows.Forms.PictureBox();
this.Cell[4,4] = new System.Windows.Forms.PictureBox();
//this.MouseDown += new MouseEventHandler(Taped);
this.Cell[0,0].MouseDown += new MouseEventHandler(Taped00);
this.Cell[1,0].MouseDown += new MouseEventHandler(Taped10);
this.Cell[2,0].MouseDown += new MouseEventHandler(Taped20);
this.Cell[3,0].MouseDown += new MouseEventHandler(Taped30);
this.Cell[4,0].MouseDown += new MouseEventHandler(Taped40);
this.Cell[0,1].MouseDown += new MouseEventHandler(Taped01);
this.Cell[1,1].MouseDown += new MouseEventHandler(Taped11);
this.Cell[2,1].MouseDown += new MouseEventHandler(Taped21);
this.Cell[3,1].MouseDown += new MouseEventHandler(Taped31);
this.Cell[4,1].MouseDown += new MouseEventHandler(Taped41);
this.Cell[0,2].MouseDown += new MouseEventHandler(Taped02);
this.Cell[1,2].MouseDown += new MouseEventHandler(Taped12);
this.Cell[2,2].MouseDown += new MouseEventHandler(Taped22);
this.Cell[3,2].MouseDown += new MouseEventHandler(Taped32);
this.Cell[4,2].MouseDown += new MouseEventHandler(Taped42);
this.Cell[0,3].MouseDown += new MouseEventHandler(Taped03);
this.Cell[1,3].MouseDown += new MouseEventHandler(Taped13);
this.Cell[2,3].MouseDown += new MouseEventHandler(Taped23);
this.Cell[3,3].MouseDown += new MouseEventHandler(Taped33);
this.Cell[4,3].MouseDown += new MouseEventHandler(Taped43);
this.Cell[0,4].MouseDown += new MouseEventHandler(Taped04);
this.Cell[1,4].MouseDown += new MouseEventHandler(Taped14);
this.Cell[2,4].MouseDown += new MouseEventHandler(Taped24);
this.Cell[3,4].MouseDown += new MouseEventHandler(Taped34);
this.Cell[4,4].MouseDown += new MouseEventHandler(Taped44);
this.SuspendLayout();
//
// BtNewGame
//
this.BtNewGame.Location = new System.Drawing.Point(16, 224);
this.BtNewGame.Name = "BtOk";
this.BtNewGame.Size = new System.Drawing.Size(108, 32);
this.BtNewGame.TabIndex = 0;
this.BtNewGame.Text = "Disconnect";
this.BtNewGame.Click += new System.EventHandler(this.BtNewGame_Click);
//
// BtConnect
//
this.BtConnect.Location = new System.Drawing.Point(160, 224);
this.BtConnect.Name = "BtCancel";
this.BtConnect.Size = new System.Drawing.Size(108, 32);
this.BtConnect.TabIndex = 1;
this.BtConnect.Text = "Connect to server";
this.BtConnect.Click += new System.EventHandler(this.BtConnect_Click);
this.Closing += new CancelEventHandler(Dialog_Closing);
// +=new System.EventHandler(this.MyClosing);
//
// Dialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 266);
this.Controls.Add(this.BtConnect);
//this.Controls.Add(this.BtNewGame);
this.Name = "Dialog";
this.Text = "Dialog";
this.ResumeLayout(false);
}
#endregion
public static void
connectCallback(IAsyncResult asyncConnect)
{
clientSocket =
(Socket)asyncConnect.AsyncState;
clientSocket.EndConnect(asyncConnect);
// arriving here means the operation completed
// (asyncConnect.IsCompleted = true) but not
// necessarily successfully
if( clientSocket.Connected == false )
{
Console.WriteLine( ".client is not connected." );
return;
}
else Console.WriteLine( ".client is connected." );
}
// times out after 2 seconds but operation continues
internal static bool writeDot(IAsyncResult ar)
{
int i = 0;
while( ar.IsCompleted == false )
{
if( i++ > 2000000)
{
Console.WriteLine("Timed out.");
return false;
}
Console.Write(".");
Thread.Sleep(100);
}
return true;
}
private void BtConnect_Click(object sender, System.EventArgs e)
{
for(int i =0; i<=4;i++)
{
for(int j =0; j<=4;j++)
{
this.Cell[i,j].Image = Image.FromFile("unknown.bmp");
this.Cell[i,j].Height = this.Cell[i,j].Image.Height;
this.Cell[i,j].Width = this.Cell[i,j].Image.Width;
this.Cell[i,j].Top = i * 16;
this.Cell[i,j].Left = j * 16;
}
}
for(int i =0; i<=4;i++)
{
for(int j =0; j<=4;j++)
{
this.Controls.Add(this.Cell[i,j]);
}
}
ModRes = true;
ipAddress =
Dns.Resolve(hostName).AddressList[0];
ipEndpoint =
new IPEndPoint(ipAddress, 58963);
clientSocket = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IAsyncResult asyncConnect = clientSocket.BeginConnect(
ipEndpoint,
new AsyncCallback(connectCallback),
clientSocket );
Console.Write("Connection in progress.");
if( writeDot(asyncConnect) == true )
{
// allow time for callbacks to
// finish before the program ends
Thread.Sleep(1000);
}
this.Controls.Remove(BtConnect);
this.ResumeLayout(true);
}
// Close();
private void BtNewGame_Click(object sender, System.EventArgs e)
{
ModRes = false;
// clientSocket.Shutdown(SocketShutdown.Both);
// clientSocket.Close();
// Close();
}
private void Dialog_Closing(object sender, CancelEventArgs e)
{
if(clientSocket.Connected==true)
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
}