Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- namespace MiddleWare
- {
- public partial class frmMiddleware : Form
- {
- Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- Socket serverClient1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- Socket serverClient2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- byte[] sendBuffer = new byte[255];
- byte[] receiveBuffer = new byte[255];
- int connections = 0;
- string guess;
- public frmMiddleware()
- {
- InitializeComponent();
- Control.CheckForIllegalCrossThreadCalls = false;
- }// WORKS
- private void btnListen_Click(object sender, EventArgs e)
- {
- int port;
- bool tryParsePort = Int32.TryParse(txtPort.Text, out port);
- if (tryParsePort == true)
- {
- IPEndPoint localEndPoint = new IPEndPoint(0, port);
- server.Bind(localEndPoint);
- server.Listen(4);
- server.BeginAccept(new AsyncCallback(OnConnected), null);
- btnStart.Text = "Server Started";
- btnStart.Enabled = false;
- frmMiddleware frmMiddleware = new frmMiddleware();
- frmMiddleware.Text = "Server Started - " + connections + " connected";
- }
- else if (tryParsePort == false)
- {
- MessageBox.Show("You did not enter a valid port number, please try again.", "Error: socket not established", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }// WORKS
- void OnConnected(IAsyncResult result)
- {
- Socket client = server.EndAccept(result);
- connections++;
- server.BeginAccept(new AsyncCallback(OnConnected), null);
- lblConnections.Text = "Connected: " + connections;
- frmMiddleware frmMiddleware = new frmMiddleware();
- frmMiddleware.Text = "Server Started - " + connections + " connected";
- receiveBuffer = new byte[255];
- int receive = client.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " on address: " + client.RemoteEndPoint);
- sendBuffer = Encoding.ASCII.GetBytes("Connected to the middleWare Server");
- try
- {
- client.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataSent), client);
- }
- catch (SocketException)
- {
- CloseClient(client);
- }
- }
- void OnDataSent(IAsyncResult result)
- {
- Socket client = (Socket)result.AsyncState;
- try
- {
- client.EndSend(result);
- receiveBuffer = new byte[255];
- client.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
- }
- catch (SocketException)
- {
- CloseClient(client);
- }
- }
- public void CloseClient(Socket client)
- {
- lbGameState.Items.Add("Player " + client.RemoteEndPoint + "has disconnected");
- client.Close();
- connections--;
- lblConnections.Text = "Connected: " + connections;
- frmMiddleware frmMiddleware = new frmMiddleware();
- frmMiddleware.Text = "Server Started - " + connections + " connected";
- server.BeginAccept(new AsyncCallback(OnConnected), null);
- }
- void OnDataReceived(IAsyncResult result)
- {
- Socket client = (Socket)result.AsyncState;
- int receive = client.EndReceive(result);
- Array.Resize(ref receiveBuffer, receive);
- string messageFromServer;
- if (receive == 0)
- {
- CloseClient(client);
- }
- else
- {
- // IF A NUMBER WAS SENT FROM THE CLIENT
- guess = Encoding.ASCII.GetString(receiveBuffer);
- lbGameState.Items.Add(client.RemoteEndPoint + " guessed: " + guess);
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes(guess);
- serverClient1.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Server 1");
- }
- receiveBuffer = new byte[255];
- receive = serverClient1.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- messageFromServer = Encoding.ASCII.GetString(receiveBuffer);
- lbGameState.Items.Add(messageFromServer);
- if (messageFromServer != "Server is down")
- {
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes(messageFromServer);
- client.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Client 1.1");
- }
- }
- else if (messageFromServer == "Server is down")
- {
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes("Server 1 could not be reached");
- client.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Client 1.2");
- }
- }
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes(guess);
- serverClient2.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Server 2");
- }
- receiveBuffer = new byte[255];
- receive = serverClient2.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- messageFromServer = Encoding.ASCII.GetString(receiveBuffer);
- lbGameState.Items.Add(messageFromServer);
- if (messageFromServer != "Server is down")
- {
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes(messageFromServer);
- client.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Client 2.1");
- }
- }
- else if (messageFromServer == "Server is down")
- {
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes("Server 2 could not be reached");
- client.Send(sendBuffer, 0, sendBuffer.Length, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "MiddleWare - Send to Client 2.2");
- }
- }
- client.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
- }
- }
- void ConnectServer1()
- {
- IPAddress ipAddress;
- int port;
- bool tryParseIP = IPAddress.TryParse(txtServer1IP.Text, out ipAddress);
- bool tryParsePort = Int32.TryParse(txtServer1Port.Text, out port);
- if (tryParseIP == true && tryParsePort == true)
- {
- IPEndPoint server1EndPoint = new IPEndPoint(ipAddress, port);
- try
- {
- serverClient1.Connect(server1EndPoint);
- lblServer1Status.Text = "Connected to: " + serverClient1.RemoteEndPoint;
- lblServer1Status.ForeColor = Color.Green;
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes("Connected to middleware server - Connection 1");
- serverClient1.Send(sendBuffer, 0, sendBuffer.Length, 0);
- receiveBuffer = new byte[255];
- int receive = serverClient1.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " 1");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "middleware - connect server 1");
- }
- }
- else
- {
- 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);
- }
- }
- void ConnectServer2()
- {
- IPAddress ipAddress;
- int port;
- bool tryParseIP = IPAddress.TryParse(txtServer2IP.Text, out ipAddress);
- bool tryParsePort = Int32.TryParse(txtServer2Port.Text, out port);
- if (tryParseIP == true && tryParsePort == true)
- {
- IPEndPoint server2EndPoint = new IPEndPoint(ipAddress, port);
- try
- {
- serverClient2.Connect(server2EndPoint);
- lblServer2Status.Text = "Connected to: " + serverClient2.RemoteEndPoint;
- lblServer2Status.ForeColor = Color.Green;
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes("Connected to middleware server - Connection 2");
- serverClient2.Send(sendBuffer, 0, sendBuffer.Length, 0);
- receiveBuffer = new byte[255];
- int receive = serverClient2.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer) + " 2");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "middleware - connect server 2");
- }
- }
- }
- private void btnServerConnect_Click(object sender, EventArgs e)
- {
- ConnectServer1();
- ConnectServer2();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment