c# networkstream dataAvailable is false

Discussion in 'Software' started by soulhaunter, Feb 16, 2011.

  1. soulhaunter

    soulhaunter Private E-2

    hey guys

    Im new to this networking programming so bare with me. Im really annoyed. For some reason, my server is returning false for dataAvailable and my client keeps crashing when I put arguements in. If it does work, nothing is returned from the server. I know that the socket connection is fine as that is returning true so im very confused.

    The part of the server you may be interested in

    Code:
     static public void RunServer()
            {
                TcpListener listener;
                Socket connection;
                NetworkStream socketStream;
    
                listener = new TcpListener(IPAddress.Any, 3001);
                listener.Start();
    
                while (true)
                {
                    try
                    {
                        connection = listener.AcceptSocket();
                        //Console.WriteLine();
                        socketStream = new NetworkStream(connection);
    
                        if (connection.Connected && socketStream.DataAvailable)
                        {
                            Console.WriteLine("woooop, connected");
                        }
                        else
                        {
                            Console.WriteLine("not connected");
                        }
    
                        
                        
                        doRequest(socketStream);
                        socketStream.Close();
                        connection.Close();
                    }
                    catch(Exception e)
                    {
                        throw e;
                    }
    
                    
                }
            }
    
            static public void doRequest(NetworkStream socketStr)
            {
                string username = "";
                string location = "";
                if (socketStr.DataAvailable)
                {
                    StreamWriter write = new StreamWriter(socketStr);
                    StreamReader read = new StreamReader(socketStr);
    
                    string into = read.ReadLine();
                    Console.WriteLine("SUCCESS: " + into);
                    write.WriteLine(into);
                    write.Flush();
    
                    
                    into.Trim();
                    if (into.Contains(" "))
                    {
                        int spaceIndex = into.IndexOf(' ');
                        username = into.Substring(0, spaceIndex);
                        location = into.Substring(spaceIndex, into.Length - spaceIndex);
    
                        username.Trim();
                        location.Trim();
                        findNames(username, location);
                        storeNames(username, location);
    
                    }
                    else
                    {
                        Console.WriteLine("nom");
                    }
    
                    write.Close();
                    read.Close();
    
                }
                else
                {
                    Console.WriteLine("FAILED");
                }
            }
    the client

    Code:
    public class Program
    {
        static void Main(string[] args)
        {
            #region Create Client
            TcpClient client = new TcpClient();
            //Console.WriteLine("Connecting.........");
    
            client.Connect("localhost", 3001);
            //Console.WriteLine("CONNECTED");
    
            #endregion
    
            #region Timeouts
            client.SendTimeout = 1000;
            client.ReceiveTimeout = 1000;
            #endregion
    
            #region Create Read and Write
            StreamWriter sw = new StreamWriter(client.GetStream());
            StreamReader sr = new StreamReader(client.GetStream());
            #endregion
            
    
            #region Arguements
            if (args.Length < 1) //error -- not allowed
            {
                Console.WriteLine("Not Enough Arguements");
            }
            else
            {
                if (args.Length == 1) //if exactly one arguement is implemented
                {
                    Console.WriteLine("You have entered 1 arguement");
                    sw.WriteLine(args[0]);
                    sw.Flush();
                    Console.WriteLine(args[0] + " is " + sr.ReadToEnd());
                }
                else if (args.Length == 2) //if exactly two arguements
                {
                    Console.WriteLine("You have entered 2 arguements");
                    sw.WriteLine(args[0] + " " + args[1]);//e.g. username locationChange
                    sw.Flush();
    
                    String reply = sr.ReadToEnd();
                    if (reply.Trim().Equals("OK"))
                    {
                        Console.WriteLine(args[0] + " location changed to be " + args[1]);
                    }
                    else
                    {
                        Console.WriteLine("Server says no...:  " + reply);
                    }
    
                }
                else
                {
                    Console.WriteLine("Too Many Arguements");
                }
            }
            #endregion
    
            Console.ReadKey();
    
            #region Closing
            sw.Close();
            sr.Close();
            client.Close();
            #endregion
        }
    }
    there are silly words in there, lol sorry about those haha

    would be grateful if you could help me, thanks
     
  2. soulhaunter

    soulhaunter Private E-2

    As added information

    When I run the client, it works first time round and says:
    you have entered 1 arguement
    371140 is 371140


    and the server says:
    wooop connected
    SUCCESS: 371140


    the second time I try that or if I try it with 2 arguements its says:
    Unhandled exception: system.IO.IOException: unable to read data from the transport connection: a connection attempt failed because the connected part did not properly respond after a period of time or established connection failed because connected host has failed to respond --> system.net.sockets.socketexception: A connection attempt failed because the connected party did not properly respond......etc.....

    and server says
    not connected
     
  3. soulhaunter

    soulhaunter Private E-2

    In regards to that unhandled exception..which many have asked about on the net, the answer to fix that in my type of code was that I have put the closing of the socket and networkstream in the try part.

    To fix it, i put
    Code:
    connection = listener.AcceptSocket();
                    socketStream = new NetworkStream(connection);
    above the try and closed the socket and networkstream in a finally
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds