Looking for some Criticism on my Networking Classes

Nov 28 2006 7:57 PM

Hi all,
I wrote these two classes to simplify network communication a bit. It seems to work (only transfers strings atm) except one day it stopped working all together and im not sure why. 

Any constructive Criticism would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Text;

namespace MyNameSpace
{
    using System;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    using System.Windows.Forms;

    public class ServerSocket
    {
        private TcpListener myListener;
        private int port = 4000;
        private Thread listenerThread;
        private Socket Client;
        private bool keepListening=false;

        public ServerSocket(int port)
        {
            this.port = port;
        }
        public void setPort(int port)
        {
            this.port = port;
        }
        public void start()
        {
            try
            {
                this.keepListening = true;
                myListener = new TcpListener(port);
                myListener.Start();
                listenerThread = new Thread(new ThreadStart(listen));
                listenerThread.Start();
            }
            catch (Exception e) { }
        }
        public void stop()
        {
            this.keepListening = false;
            myListener.Stop();
            listenerThread.Abort();
        }
        public void listen()
        {
            while (this.keepListening)
            {
                try
                {
                    Client = myListener.AcceptSocket();//Catch Socket Exception
                }
                catch (SocketException) { }
                if (Client.Connected)
                {
                    //MessageBox.Show("Client Connected!!");
                    try
                    {
                        while (Client.Connected && this.keepListening)
                        {
                            Byte[] receive = new Byte[64];
                            int i = Client.Receive(receive, receive.Length, 0);
                            char[] unwanted = { ' ', ' ', '\0' };
                            recieveText(System.Text.Encoding.ASCII.GetString(receive).TrimEnd(unwanted));
                        }
                    }
                    catch (SocketException e)
                    {
                        Client.Disconnect(true);
                    }
                }
                Thread.Sleep(500);
            }
        }
        public void sendText(string text)
        {

            Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(text.ToCharArray());
            Client.Send(byteDateLine, byteDateLine.Length, 0);
        }
        public void recieveText(string text)
        {
            string sendText = "ERROR - Unrecognised Command";
                if (text.Equals("SayHello:"))
                    sendText = "Hello Dude!";
            this.sendText(sendText);
        }
    }
}

    public class ClientSocket
    {
        private TcpClient Server;
        private int port = 4554;
        private string ip;
        private bool readData = false;

        public ClientSocket(string ip, int port)
        {
            this.ip = ip;
            this.port = port;
        }
        public bool connect()
        {
            try
            {
                Server = new TcpClient(this.ip, this.port);
                return true;
            }
            catch (Exception e) { return false; }
        }
        public void disconnect()
        {
            Server.Close();
        }
        public string sendRequest(string request)
        {
            if (Server!=null&&Server.Connected == true)
            {
                string returnString = "";
                NetworkStream nts = Server.GetStream();
                if (nts.CanWrite)
                {
                    Byte[] sends = System.Text.Encoding.ASCII.GetBytes(request.ToCharArray());
                    nts.Write(sends, 0, sends.Length);
                    nts.Flush();
                }
                readData = false;
                while (!readData && nts.CanRead)
                {
                    //if data available then read from the stream
                    if (nts.DataAvailable)
                    {
                        byte[] rcd = new byte[128];
                        int i = nts.Read(rcd, 0, 128);

                        char[] unwanted = { ' ', ' ', ' ' };
                        returnString = System.Text.Encoding.ASCII.GetString(rcd).TrimEnd(unwanted);
                        readData = true;
                    }
                }
                return returnString;
            }
            return "Error - Not connected to server";
        }
    }


Answers (1)