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