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; using System.Threading; using System.IO; namespace WinNetworkIOCS { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void ConnectButton_Click(object sender, EventArgs e) { DisableFields(); DoNetworkingConnection(); } private void DisableFields() { PortBox.Enabled = false; IPAddressBox.Enabled = false; SendMessageBox.Enabled = false; ConnectButton.Enabled = false; } private void EnableFields() { PortBox.Enabled = true; IPAddressBox.Enabled = true; SendMessageBox.Enabled = true; ConnectButton.Enabled = true; } private void WriteToStatusBar(string Message) { //EnableFields(); ThreadHelperClass.SetText(this, lblStatus, Message); } private void DoNetworkingConnection() { Thread MyThread = null; try { ThreadStart ThreadMethod = new ThreadStart(ConnectTo); MyThread = new Thread(ThreadMethod); } catch (Exception e) { WriteToStatusBar("Failed to create thread with error: " + e.Message); return; } try { MyThread.Start(); } catch (Exception e) { WriteToStatusBar("The thread failed to start with error: " + e.Message); } } private void ConnectTo() { string ServerName = this.IPAddressBox.Text; int Port = System.Convert.ToInt32(this.PortBox.Text); WriteToStatusBar("IP Address: " + ServerName + "Port: " + Port); Socket ClientSocket = null; try { // Let's connect to a listening server try { ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); WriteToStatusBar("Socket is OK..."); } catch (Exception e) { throw new Exception("Failed to create client Socket: " + e.Message); } IPEndPoint ServerEndPoint = new IPEndPoint(IPAddress.Parse(ServerName), Convert.ToInt16(Port)); try { ClientSocket.Connect(ServerEndPoint); WriteToStatusBar("Connect() is OK..."); } catch (Exception e) { throw new Exception("Failed to connect client Socket: " + e.Message); } } catch (Exception e) { WriteToStatusBar(e.Message); ClientSocket.Close(); return; } // Let's create a network stream to communicate over the connected Socket. NetworkStream ClientNetworkStream = null; try { try { // Setup a network stream on the client Socket ClientNetworkStream = new NetworkStream(ClientSocket, true); WriteToStatusBar("Instantiating NetworkStream..."); } catch (Exception e) { // We have to close the client socket here because the network // stream did not take ownership of the socket. ClientSocket.Close(); throw new Exception("Failed to create a NetworkStream with error: " + e.Message); } StreamWriter ClientNetworkStreamWriter = null; try { // Setup a Stream Writer ClientNetworkStreamWriter = new StreamWriter(ClientNetworkStream); WriteToStatusBar("Setting up StreamWriter..."); } catch (Exception e) { ClientNetworkStream.Close(); throw new Exception("Failed to create a StreamWriter with error: " + e.Message); } try { ClientNetworkStreamWriter.Write(this.SendMessageBox.Text.ToString()); ClientNetworkStreamWriter.Flush(); WriteToStatusBar("We wrote " + this.SendMessageBox.Text.Length.ToString() + " character(s) to the server."); } catch (Exception e) { throw new Exception("Failed to write to client NetworkStream with error: " + e.Message); } } catch (Exception e) { WriteToStatusBar(e.Message); } finally { // Close the network stream once everything is done ClientNetworkStream.Close(); } } delegate void SetTextCallback(string text); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)