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.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- namespace Client
- {
- public partial class frmClient : Form
- {
- byte[] sendBuffer = new byte[255];
- byte[] receiveBuffer = new byte[255];
- Socket clientSocket;
- public frmClient()
- {
- InitializeComponent();
- }
- private void btnJoin_Click(object sender, EventArgs e)// WORKS
- {
- clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress ipAddress;
- int port;
- bool tryParseIP = IPAddress.TryParse(txtAddress.Text, out ipAddress);
- bool tryParsePort = Int32.TryParse(txtPort.Text, out port);
- if (tryParseIP == true && tryParsePort == true)
- {
- try
- {
- IPEndPoint clientEndpoint = new IPEndPoint(ipAddress, port);
- clientSocket.Connect(clientEndpoint);
- sendBuffer = Encoding.ASCII.GetBytes("connected to client");
- clientSocket.Send(sendBuffer, 0, sendBuffer.Length, 0);
- sendBuffer = null;
- receiveBuffer = new byte[255];
- int receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
- lblConnected.Text = "Connected to: " + clientSocket.RemoteEndPoint;
- lblConnected.ForeColor = Color.Green;
- btnJoin.Enabled = false;
- btnLeave.Enabled = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Client - Connection Error");
- }
- }
- else
- {
- 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);
- }
- }
- private void btnLeave_Click(object sender, EventArgs e)// WORKS
- {
- clientSocket.Close();
- lblConnected.Text = "Disconnected";
- lblConnected.ForeColor = Color.Red;
- btnJoin.Enabled = true;
- btnLeave.Enabled = false;
- }
- private void btnGuess_Click(object sender, EventArgs e)// WORKS
- {
- int guess;
- int receive;
- bool tryParseGuess = Int32.TryParse(txtNumber.Text, out guess);
- if (tryParseGuess == true && guess <= 10 && guess > 0)
- {
- try
- {
- sendBuffer = new byte[255];
- sendBuffer = Encoding.ASCII.GetBytes(txtNumber.Text);
- clientSocket.Send(sendBuffer, 0, sendBuffer.Length, 0);
- receiveBuffer = new byte[255];
- receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
- receiveBuffer = new byte[255];
- receive = clientSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, 0);
- Array.Resize(ref receiveBuffer, receive);
- lbGameState.Items.Add(Encoding.ASCII.GetString(receiveBuffer));
- }
- catch (SocketException)
- {
- clientSocket.Close();
- }
- }
- else
- {
- MessageBox.Show("Enter a valid number between 1 and 100", "Error - valid number not entered", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void frmClient_FormClosing(object sender, FormClosingEventArgs e)// WORKS
- {
- //clientSocket.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment