Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Mar 31st, 2008, 7:08 PM   #1
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
Smile Returning data from a class

I'm working a a chat application for me and my girlfriend. Mu ultimate goal to is construct one class to utilize in both the client and server apps. The trouble I'm running into is being able to return recieved data back to the app from the class and post it in the text box. Below I will post the class constructor and a few linked methods it uses.

        public xsocket(int port, int maxCon, bool isServer)
        {
            i_Port = port;

            if (isServer)
            {
                i_maxConnections = maxCon;

                tcp_listener = new TcpListener(i_Port);
                tcp_listener.Start();
                s_serverIP = tcp_listener.LocalEndpoint.ToString();

                thd_tcp = new Thread(new ThreadStart(ListenLoop));
                hash_threads.Add(i_connectID, thd_tcp);
                thd_tcp.Start();
            }
            else
            {
                sock_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
        }

        public void ListenLoop()
        {
            while (b_waitForClients)
            {
                sock_newClient = tcp_listener.AcceptSocket();

                if (hash_sockets.Count <= i_maxConnections)
                {
                    while (hash_sockets.Contains(i_connectID))
                    {
                        Interlocked.Increment(ref i_connectID);
                    }

                    thd_dataReader = new Thread(new ThreadStart(DataReader));

                    hash_sockets.Add(i_connectID, sock_newClient);
                    hash_threads.Add(i_connectID, thd_dataReader);

                    thd_dataReader.Start();
                }
            }
        }

        public void DataReader()
        {
            if (b_isServer)
            {
                i_userID = i_connectID;
                sock_dataRet = (Socket)hash_sockets[i_userID];

                while (true)
                {
                    if (sock_dataRet.Connected)
                    {
                        Byte[] b_newData = new Byte[1024];
                        try
                        {
                            int retrievedData = sock_dataRet.Receive(b_newData, b_newData.Length, 0);

                            if (retrievedData > 0)
                            {
                                string s_temp;
                                s_temp = Encoding.ASCII.GetString(b_newData);
                            }
                        }
                        catch
                        {
                            if (b_isServer)
                                RemoveClient(i_userID);
                        }
                    }
                }
            }
            else
            {
                Byte[] b_dataRet = new Byte[1024];
                string tempData;

                try
                {
                    int ret = sock_client.Receive(b_dataRet, b_dataRet.Length, 0);

                    if (ret > 0)
                    {
                        // find a way to return the data
                        tempData = Encoding.ASCII.GetString(b_dataRet);
                    }
                }
                catch
                {
                    // connection lost??? return error
                }
            }
        }

If you need anymore code let me know... I didn't want to spam it. Thanks!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
template class brad sue C++ 1 Mar 25th, 2007 6:46 PM
Class problem Ithaqua Visual Basic .NET 4 Dec 5th, 2006 5:30 PM
URL class Eric the Red Java 5 Jun 24th, 2006 10:01 PM
Problem with class constructor paeck C++ 8 Feb 7th, 2006 1:53 PM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 1:21 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC