ohbutt69

client game

Jan 16th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 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.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace Client
  13. {
  14.     public partial class frmClient : Form
  15.     {
  16.  
  17.         byte[] sendBuffer = new byte[255];
  18.         byte[] receiveBuffer = new byte[255];
  19.         Socket clientSocket;
  20.  
  21.         public frmClient()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void btnJoin_Click(object sender, EventArgs e)// WORKS
  27.         {
  28.             clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  29.  
  30.             IPAddress ipAddress;
  31.             int port;
  32.             bool tryParseIP = IPAddress.TryParse(txtAddress.Text, out ipAddress);
  33.             bool tryParsePort = Int32.TryParse(txtPort.Text, out port);
  34.  
  35.             if (tryParseIP == true && tryParsePort == true)
  36.             {
  37.                 try
  38.                 {
  39.                     IPEndPoint clientEndpoint = new IPEndPoint(ipAddress, port);
  40.                     clientSocket.Connect(clientEndpoint);
  41.  
  42.                     sendBuffer = Encoding.ASCII.GetBytes("connected to client");
  43.                     clientSocket.Send(sendBuffer, 0, sendBuffer.Length, 0);
  44.                     sendBuffer = null;
  45.  
  46.                     receiveBuffer = new byte[255];
  47.                     int receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  48.                     Array.Resize(ref receiveBuffer, receive);
  49.                     lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
  50.  
  51.                     lblConnected.Text = "Connected to: " + clientSocket.RemoteEndPoint;
  52.                     lblConnected.ForeColor = Color.Green;
  53.                     btnJoin.Enabled = false;
  54.                     btnLeave.Enabled = true;
  55.                 }
  56.                 catch (Exception ex)
  57.                 {
  58.                     MessageBox.Show(ex.Message, "Client - Connection Error");
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 MessageBox.Show("You did not enter a valid port number or IP address, please try again.", "Error: connect to server not established", MessageBoxButtons.OK, MessageBoxIcon.Error);
  64.             }
  65.         }
  66.  
  67.         private void btnLeave_Click(object sender, EventArgs e)// WORKS
  68.         {
  69.             clientSocket.Close();
  70.             lblConnected.Text = "Disconnected";
  71.             lblConnected.ForeColor = Color.Red;
  72.             btnJoin.Enabled = true;
  73.             btnLeave.Enabled = false;
  74.         }
  75.  
  76.         private void btnGuess_Click(object sender, EventArgs e)// WORKS
  77.         {
  78.             int guess;
  79.             int receive;
  80.             bool tryParseGuess = Int32.TryParse(txtNumber.Text, out guess);
  81.  
  82.             if (tryParseGuess == true && guess <= 10 && guess > 0)
  83.             {
  84.                 try
  85.                 {
  86.                     sendBuffer = new byte[255];
  87.                     sendBuffer = Encoding.ASCII.GetBytes(txtNumber.Text);
  88.  
  89.                     clientSocket.Send(sendBuffer, 0, sendBuffer.Length, 0);
  90.  
  91.                     receiveBuffer = new byte[255];
  92.                     receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  93.                     Array.Resize(ref receiveBuffer, receive);
  94.                     lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
  95.  
  96.                     receiveBuffer = new byte[255];
  97.                     receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
  98.                     Array.Resize(ref receiveBuffer, receive);
  99.                     lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
  100.                 }
  101.                 catch (SocketException)
  102.                 {
  103.                     clientSocket.Close();
  104.                 }
  105.             }
  106.             else
  107.             {
  108.                 MessageBox.Show("Enter a valid number between 1 and 100", "Error - valid number not entered", MessageBoxButtons.OK, MessageBoxIcon.Error);
  109.             }
  110.         }
  111.  
  112.         private void frmClient_FormClosing(object sender, FormClosingEventArgs e)// WORKS
  113.         {
  114.                 //clientSocket.Close();
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment