Question I have some exception in my code

bali971

New member
Joined
May 24, 2015
Messages
1
Programming Experience
Beginner
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace newClient
{
    class Connection
    {
        //Thread _threadObj;
        TcpClient _TcpObj;
        NetworkStream _NSObj;
        StreamReader _SRObj;
        StreamWriter _SWObj;
        string _MainIp;
        int _Socket;
        string _UserName;
        public string _USERNAME 
        {
            get 
            {
                return _UserName;
            }
            set 
            {
                _UserName = value;
            }
        }
        public int _SOCKET 
        {
            get 
            {
                return _Socket;
            }
            set 
            {
                _Socket = value;
            }
        }
        public string _MAINIP 
        {
            get
            {
                return _MainIp;
            }
            set
            {
                _MainIp = value;
            }
        }
        public string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }
        public void ConnectionEstablish()
        {
            _TcpObj = new TcpClient("192.168.1.12", 6060);
            _NSObj = _TcpObj.GetStream();
            _SRObj = new StreamReader(_NSObj);
            _SWObj = new StreamWriter(_NSObj); 
        }


        public void ConnectionRemove()
        {
            _TcpObj.Close();
            _NSObj.Dispose();
            _SRObj.Dispose();
            _SWObj.Dispose();
        }


        public bool Login(string UserName, string PassWord)
        {
            ConnectionEstablish();
            string _IDLogin = "&*@Loginn@*&" + UserName +"$"+ PassWord;
            _SWObj.WriteLine(_IDLogin);
            _SWObj.Flush();
            string _Response = _SRObj.ReadLine();
            if (_Response.StartsWith("&@*IP*@&"))
            {
                string _subS = _Response.Substring(8);
                string[] _IPSUSep = _subS.Split('&');
                _USERNAME = _IPSUSep[1];
                _IPSUSep = _IPSUSep[0].Split(':');
                _MAINIP = _IPSUSep[0];
                _SOCKET = int.Parse(_IPSUSep[1]);
                ConnectionRemove();
                return true;
            }
            else
            {
                ConnectionRemove();
                return false;
            }


        }
        public bool SignUp(string UserName, string PassWord)
        {
            ConnectionEstablish();
            string _IDSignUp = "&*@SignUp@*&" + UserName + "$" + PassWord;
            _SWObj.WriteLine(_IDSignUp);
            _SWObj.Flush();
            string _Response = _SRObj.ReadLine();
            if (_Response.StartsWith("SignUp Successsfully"))
            {
                ConnectionRemove();
                return true;
            }
            else
            {
                ConnectionRemove();
                return false;
            }


        }


    }
}
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,792
Location
Sydney, Australia
Programming Experience
10+
Firstly, please use formatting tags when posting code snippets. I have fixed that for you and, as you can see, it makes the code far more readable, especially because it maintains inventing.

Secondly, don't post JUST code. Provide a FULL and CLEAR explanation of the problem. Explain exactly what the code is supposed to do for a start and also specify exactly what exceptions are being thrown and exactly where. We can't read your mind and we shouldn't have to waste time trying to work things out that you already know and can easily tell us.
 
Top Bottom