ohbutt69

middleware game

Jan 16th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12.  
  13. namespace MiddleWare
  14. {
  15.     public partial class frmMiddleware : Form
  16.     {
  17.         Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  18.         Socket serverClient1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  19.         Socket serverClient2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  20.  
  21.         byte[] sendBuffer = new byte[255];
  22.         byte[] receiveBuffer = new byte[255];
  23.         int connections = 0;
  24.         string guess;
  25.  
  26.         public frmMiddleware()
  27.         {
  28.             InitializeComponent();
  29.             Control.CheckForIllegalCrossThreadCalls = false;
  30.         }// WORKS
  31.  
  32.         private void btnListen_Click(object sender, EventArgs e)
  33.         {
  34.             int port;
  35.             bool tryParsePort = Int32.TryParse(txtPort.Text, out port);
  36.  
  37.             if (tryParsePort == true)
  38.             {
  39.                 IPEndPoint localEndPoint = new IPEndPoint(0, port);
  40.                 server.Bind(localEndPoint);
  41.                 server.Listen(4);
  42.                 server.BeginAccept(new AsyncCallback(OnConnected), null);
  43.  
  44.                 btnStart.Text = "Server Started";
  45.                 btnStart.Enabled = false;
  46.                 frmMiddleware frmMiddleware = new frmMiddleware();
  47.                 frmMiddleware.Text = "Server Started - " + connections + " connected";
  48.             }
  49.             else if (tryParsePort == false)
  50.             {
  51.                 MessageBox.Show("You did not enter a valid port number, please try again.", "Error: socket not established", MessageBoxButtons.OK, MessageBoxIcon.Error);
  52.             }
  53.         }// WORKS
  54.  
  55.         void OnConnected(IAsyncResult result)
  56.         {
  57.             Socket client = server.EndAccept(result);
  58.             connections++;
  59.             server.BeginAccept(new AsyncCallback(OnConnected), null);
  60.             lblConnections.Text = "Connected: " + connections;
  61.             frmMiddleware frmMiddleware = new frmMiddleware();
  62.             frmMiddleware.Text = "Server Started - " + connections + " connected";
  63.  
  64.             receiveBuffer = new byte[255];
  65.             int receive = client.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  66.             Array.Resize(ref receiveBuffer, receive);
  67.             lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " on address: " + client.RemoteEndPoint);
  68.  
  69.             sendBuffer = Encoding.ASCII.GetBytes("Connected to the middleWare Server");
  70.             try
  71.             {
  72.                 client.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataSent), client);
  73.             }
  74.             catch (SocketException)
  75.             {
  76.                 CloseClient(client);
  77.             }
  78.         }
  79.  
  80.         void OnDataSent(IAsyncResult result)
  81.         {
  82.             Socket client = (Socket)result.AsyncState;
  83.  
  84.             try
  85.             {
  86.                 client.EndSend(result);
  87.                 receiveBuffer = new byte[255];
  88.                 client.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
  89.             }
  90.             catch (SocketException)
  91.             {
  92.                 CloseClient(client);
  93.             }
  94.         }
  95.  
  96.         public void CloseClient(Socket client)
  97.         {
  98.             lbGameState.Items.Add("Player " + client.RemoteEndPoint + "has disconnected");
  99.             client.Close();
  100.             connections--;
  101.             lblConnections.Text = "Connected: " + connections;
  102.             frmMiddleware frmMiddleware = new frmMiddleware();
  103.             frmMiddleware.Text = "Server Started - " + connections + " connected";
  104.             server.BeginAccept(new AsyncCallback(OnConnected), null);
  105.         }
  106.  
  107.         void OnDataReceived(IAsyncResult result)
  108.         {
  109.             Socket client = (Socket)result.AsyncState;
  110.             int receive = client.EndReceive(result);
  111.             Array.Resize(ref receiveBuffer, receive);
  112.             string messageFromServer;
  113.  
  114.             if (receive == 0)
  115.             {
  116.                 CloseClient(client);
  117.             }
  118.             else
  119.             {
  120.                 // IF A NUMBER WAS SENT FROM THE CLIENT
  121.                 guess = Encoding.ASCII.GetString(receiveBuffer);
  122.                 lbGameState.Items.Add(client.RemoteEndPoint + " guessed: " + guess);
  123.  
  124.                 try
  125.                 {
  126.                     sendBuffer = new byte[255];
  127.                     sendBuffer = Encoding.ASCII.GetBytes(guess);
  128.                     serverClient1.Send(sendBuffer, 0, sendBuffer.Length, 0);
  129.                 }
  130.                 catch (Exception ex)
  131.                 {
  132.                     MessageBox.Show(ex.Message, "MiddleWare - Send to Server 1");
  133.                 }
  134.  
  135.                 receiveBuffer = new byte[255];
  136.                 receive = serverClient1.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  137.                 Array.Resize(ref receiveBuffer, receive);
  138.                 messageFromServer = Encoding.ASCII.GetString(receiveBuffer);
  139.                 lbGameState.Items.Add(messageFromServer);
  140.  
  141.                 if (messageFromServer != "Server is down")
  142.                 {
  143.                     try
  144.                     {
  145.                         sendBuffer = new byte[255];
  146.                         sendBuffer = Encoding.ASCII.GetBytes(messageFromServer);
  147.                         client.Send(sendBuffer, 0, sendBuffer.Length, 0);
  148.                     }
  149.                     catch (Exception ex)
  150.                     {
  151.                         MessageBox.Show(ex.Message, "MiddleWare - Send to Client 1.1");
  152.                     }
  153.                 }
  154.                 else if (messageFromServer == "Server is down")
  155.                 {
  156.                     try
  157.                     {
  158.                         sendBuffer = new byte[255];
  159.                         sendBuffer = Encoding.ASCII.GetBytes("Server 1 could not be reached");
  160.                         client.Send(sendBuffer, 0, sendBuffer.Length, 0);
  161.                     }
  162.                     catch (Exception ex)
  163.                     {
  164.                         MessageBox.Show(ex.Message, "MiddleWare - Send to Client 1.2");
  165.                     }
  166.                 }
  167.  
  168.                 try
  169.                 {
  170.                     sendBuffer = new byte[255];
  171.                     sendBuffer = Encoding.ASCII.GetBytes(guess);
  172.                     serverClient2.Send(sendBuffer, 0, sendBuffer.Length, 0);
  173.                 }
  174.                 catch (Exception ex)
  175.                 {
  176.                     MessageBox.Show(ex.Message, "MiddleWare - Send to Server 2");
  177.                 }
  178.  
  179.                 receiveBuffer = new byte[255];
  180.                 receive = serverClient2.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  181.                 Array.Resize(ref receiveBuffer, receive);
  182.                 messageFromServer = Encoding.ASCII.GetString(receiveBuffer);
  183.                 lbGameState.Items.Add(messageFromServer);
  184.  
  185.                 if (messageFromServer != "Server is down")
  186.                 {
  187.                     try
  188.                     {
  189.                         sendBuffer = new byte[255];
  190.                         sendBuffer = Encoding.ASCII.GetBytes(messageFromServer);
  191.                         client.Send(sendBuffer, 0, sendBuffer.Length, 0);
  192.                     }
  193.                     catch (Exception ex)
  194.                     {
  195.                         MessageBox.Show(ex.Message, "MiddleWare - Send to Client 2.1");
  196.                     }
  197.                 }
  198.                 else if (messageFromServer == "Server is down")
  199.                 {
  200.                     try
  201.                     {
  202.                         sendBuffer = new byte[255];
  203.                         sendBuffer = Encoding.ASCII.GetBytes("Server 2 could not be reached");
  204.                         client.Send(sendBuffer, 0, sendBuffer.Length, 0);
  205.                     }
  206.                     catch (Exception ex)
  207.                     {
  208.                         MessageBox.Show(ex.Message, "MiddleWare - Send to Client 2.2");
  209.                     }
  210.                 }
  211.                 client.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
  212.             }
  213.         }
  214.  
  215.         void ConnectServer1()
  216.         {
  217.             IPAddress ipAddress;
  218.             int port;
  219.             bool tryParseIP = IPAddress.TryParse(txtServer1IP.Text, out ipAddress);
  220.             bool tryParsePort = Int32.TryParse(txtServer1Port.Text, out port);
  221.  
  222.             if (tryParseIP == true && tryParsePort == true)
  223.             {
  224.                 IPEndPoint server1EndPoint = new IPEndPoint(ipAddress, port);
  225.                 try
  226.                 {
  227.                     serverClient1.Connect(server1EndPoint);
  228.  
  229.                     lblServer1Status.Text = "Connected to: " + serverClient1.RemoteEndPoint;
  230.                     lblServer1Status.ForeColor = Color.Green;
  231.  
  232.                     sendBuffer = new byte[255];
  233.                     sendBuffer = Encoding.ASCII.GetBytes("Connected to middleware server - Connection 1");
  234.                     serverClient1.Send(sendBuffer, 0, sendBuffer.Length, 0);
  235.  
  236.                     receiveBuffer = new byte[255];
  237.                     int receive = serverClient1.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  238.                     Array.Resize(ref receiveBuffer, receive);
  239.                     lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " 1");
  240.                 }
  241.                 catch (Exception ex)
  242.                 {
  243.                     MessageBox.Show(ex.Message, "middleware - connect server 1");
  244.                 }
  245.             }
  246.             else
  247.             {
  248.                 MessageBox.Show("You did not enter a valid port or IP address, please try again.", "Error: Connection to server not established", MessageBoxButtons.OK, MessageBoxIcon.Error);
  249.             }
  250.         }
  251.  
  252.         void ConnectServer2()
  253.         {
  254.             IPAddress ipAddress;
  255.             int port;
  256.             bool tryParseIP = IPAddress.TryParse(txtServer2IP.Text, out ipAddress);
  257.             bool tryParsePort = Int32.TryParse(txtServer2Port.Text, out port);
  258.  
  259.             if (tryParseIP == true && tryParsePort == true)
  260.             {
  261.                 IPEndPoint server2EndPoint = new IPEndPoint(ipAddress, port);
  262.                 try
  263.                 {
  264.                     serverClient2.Connect(server2EndPoint);
  265.  
  266.                     lblServer2Status.Text = "Connected to: " + serverClient2.RemoteEndPoint;
  267.                     lblServer2Status.ForeColor = Color.Green;
  268.  
  269.                     sendBuffer = new byte[255];
  270.                     sendBuffer = Encoding.ASCII.GetBytes("Connected to middleware server - Connection 2");
  271.                     serverClient2.Send(sendBuffer, 0, sendBuffer.Length, 0);
  272.  
  273.                     receiveBuffer = new byte[255];
  274.                     int receive = serverClient2.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  275.                     Array.Resize(ref receiveBuffer, receive);
  276.                     lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " 2");
  277.                 }
  278.                 catch (Exception ex)
  279.                 {
  280.                     MessageBox.Show(ex.Message, "middleware - connect server 2");
  281.                 }
  282.             }
  283.         }
  284.  
  285.         private void btnServerConnect_Click(object sender, EventArgs e)
  286.         {
  287.             ConnectServer1();
  288.             ConnectServer2();
  289.         }
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment