Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Determining if an input has value.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
S_Grimm




PostPosted: Mon Nov 03, 2008 12:08 pm   Post subject: Determining if an input has value.

First, i apoligise for the title. didn't know how else to word this. What i want is to be able to check if a network input has a value so what i have is :
Java:

import java.net.*;
import java.io.*;
public class chatServerv2p8
{
    public static InputStreamReader reader = new InputStreamReader (System.in);
    public static BufferedReader input = new BufferedReader (reader);
    public static String NumConnecting;
    public static Object Convert = new Object ();
    public static int count;
    public static Socket [] mySocket = new Socket [50];
    public static ServerSocket [] ss = new ServerSocket [50];
    public static String datainput;
    public static InputStreamReader [] isr = new InputStreamReader [50];
    public static BufferedReader [] br = new BufferedReader [50];
    public static PrintWriter [] pw = new PrintWriter [50];
    public static String datavar;
    public static String name;
    public static boolean message = false;

    public static class Write extends Thread
    {
        public void run ()
        {

            try
            {
            if ((datavar = br [0].readLine ()) == null)
                {
                new Read().run();
                }
                if ((datavar = br [0].readLine ()) != null)
                {
                   // datavar = br [0].readLine ();
                    message = true;
                }
                else
                {
                    message = false;
                    System.out.println (datavar);
                    new Read ().resume ();
                }
                if (message == true)
                {
                    new Read ().suspend ();
                    System.out.println (datavar);
                    message = false;
                    new Read ().resume ();
                }
                else
                {
                }
               
            }
            catch (Exception e)
            {
                //System.out.println ("No Data To Read");
            }
        }
    }



    public static class Read extends Thread
    {
        public void run ()
        {
            try
            {
                pw [0].println (name + input.readLine ());
            }
            catch (Exception e)
            {
            System.out.println ("Error sending message");
            }
        }
    }


    public static void main (String [] args) throws Exception
    {
        InetAddress lclAdr = InetAddress.getLocalHost ();
        System.out.println ("Your IP address is " + lclAdr.getHostAddress ());
        System.out.print ("How many clients are connecting : ");
        count = Integer.parseInt (input.readLine ());
        synchronized (ss)
        {
            for (int i = 0 ; i < count ; i++)
            {
                try
                {
                    ss [i] = new ServerSocket (5050 + i);
                }
                catch (Exception e)
                {
                }
            }
        }


        synchronized (mySocket)
        {
            for (int h = 0 ; h < count ; h++)
            {
                try
                {
                    mySocket [h] = ss [h].accept ();
                }
                catch (Exception e)
                {
                }

            }
        }


        for (int i = 0 ; i < count ; i++)
        {
            isr [i] = new InputStreamReader (mySocket [i].getInputStream ());

        }
        for (int i = 0 ; i < count ; i++)
        {
            pw [i] = new PrintWriter (mySocket [i].getOutputStream (), true);
        }

        for (int i = 0 ; i < count ; i++)
        {
            br [i] = new BufferedReader (isr [i]);
        }

        new Read ().setPriority (1);
        new Write ().setPriority (2);
        for (;;)
        {
            new Read ().run ();
            new Write ().run ();


        }
    }
}




I need a way to replace the
Quote:

try
{
if ((datavar = br [0].readLine ()) == null)
{
new Read().run();
}
if ((datavar = br [0].readLine ()) != null)
{
// datavar = br [0].readLine ();
message = true;
}
else
{
message = false;
System.out.println (datavar);
new Read ().resume ();
}
if (message == true)
{
new Read ().suspend ();
System.out.println (datavar);
message = false;
new Read ().resume ();
}
else
{
}

}
catch (Exception e)
{
//System.out.println ("No Data To Read");
}


with a simpiler statement that:
a) checks for text over the network
b) if text is present, interrupts user input and prints text, then allows user to continue
c) if text is NOT present, allows user to continue inputting

Thanks in advance.
A\V
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: Mon Nov 03, 2008 8:45 pm   Post subject: RE:Determining if an input has value.

A\V; Looks like you are digging in quite nicely.

Umm, what if you separate the user input, text output and network I/O into Threads? Would the problem not disappear?

Given this is Java, I don't think a modular approach like that will make the code overly complex. It could simplify a few things.

Cheers
S_Grimm




PostPosted: Tue Nov 04, 2008 11:22 am   Post subject: Re: Determining if an input has value.

Quote:
Umm, what if you separate the user input, text output and network I/O into Threads?

I thought they were.... That is how i was told to delcare threads. Do you mean make more threads? One to read the user input, one to read the network input. one to write the user input to the network, and one to print the network input?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: