![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Re: Returning data from a class
Why not just have an attribute and set that to the data returned.And have a getter method for that? Then in your main app you can just assign the texboes Text attribute to this value.
class Sock
{
private String data = "";;
public getData()
{
return data;
}
...
}and in your main app do: Sock s = new Sock(); TextBox.Text = s.getData()
__________________
Quote:
|
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
Re: Returning data from a class
Yea I thought that would have been a good quick fix too... but the main issue I'm running into is that this is a chat app and I'm not sure when the user will be sending data.... So i need to be able to detect from the application that the socket class has recieved new data. I believe this is a good time to implement some sort of event... but I have never made one..only utilized them.
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 |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Re: Returning data from a class
Have an attribute in your class which is a delegate, (I think delegates work like function pointers, look it up) and when you receive data invoke the delegate. You need to set the delegate before the data receiver method is invoked, probably in the constructor.
__________________
Quote:
|
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: Returning data from a class
Thanks for your help so far man... much appreciated.
I ended up passing a ref to the textbox through the contructor. When data is recieved a function is called to update the ref to the textbox. I have now encountered a new problem. I can only send 1 msg from the client to the server. None from the client and no more than 1 from the server. I think The DataReader function is only being called the first time date arrives... what about the second time? Believe I'm posting the entire class. Is there something I need to do to link the DataReader function to an event handler for the socket. EDIT: I noticed my code for the client sending is commented out. So i guess the only issue is the DataReader problem. C# Syntax (Toggle Plain Text)
__________________
"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 |
|
|
|
|
|
#6 |
|
Not a user?
Join Date: Sep 2007
Posts: 272
Rep Power: 2
![]() |
Re: Returning data from a class
This may help, I made a prog that pings various servers and emails results to me. I have to destroy each instance of ping and I think smtp message too before I can send another.
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
Re: Returning data from a class
Hehe... I realized that it was looping the data reader... I accomplished that by throwing the code in a while loop and throwing it on it's own thread. The problem where a few simple corrections within the send and recieve functions.
Thanks for the help!
__________________
"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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| template class | brad sue | C++ | 1 | Mar 25th, 2007 5:46 PM |
| Class problem | Ithaqua | Visual Basic .NET | 4 | Dec 5th, 2006 4:30 PM |
| URL class | Eric the Red | Java | 5 | Jun 24th, 2006 9:01 PM |
| Problem with class constructor | paeck | C++ | 8 | Feb 7th, 2006 12:53 PM |
| Recommended Practice for returning data from function | Arla | C# | 1 | Aug 16th, 2005 12:21 PM |