CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Desperate Help Needed

  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Arrow Desperate Help Needed

    I have been trying to send multiple messages from client to server & do a comparison with a value read from a file. Until I added the comparison part, I had got everthing working. Ever since I did comparison, server doesn echo wat user writes in the client. M running out of time but I can't get the code to do what I want, pls help.

    SERVER:

    Code:
    /*Does encryption and decryption, check car existence(read file), 
    check timestamp differs from current timestamp no more than 1 min, 
    if more than 1 min request retransmission Ability of branch to manage car, nform C if can't manage*/
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class AS{
        public static void main(String[] args ){
            int i = 1;
    
            try{
                 ServerSocket s = new ServerSocket(9001);
    
                 for (;;){
                     Socket incoming = s.accept( );
                     System.out.println("Spawning " + i);
                     new RealEchoHandler(incoming, i).start();
                     i++;
                 }
            } catch (Exception e){ System.out.println(e); }
        }
    }
    
    class RealEchoHandler extends Thread{
    	DataInputStream in;
    	DataOutputStream out;
    	FileInputStream fin = null;
        BufferedInputStream buff = null;
        DataInputStream ds = null;
    
    
    	private Socket incoming;
        	private int counter;
    
        public RealEchoHandler(Socket i, int c){
            incoming = i;
            counter = c;
        }
    
        public void run(){
            try {
    
                in = new DataInputStream(incoming.getInputStream());
    	    out = new DataOutputStream(incoming.getOutputStream());
    
                boolean done = false;
                String str="";
                String store="";
                String [] temp;
    
                out.writeUTF("Connected!\n");
                out.flush();
    
    	    out.writeUTF(">");
    	    str = in.readUTF();
    	    out.writeUTF("Echo (" + counter + "): " + str+"\n");
    	    System.out.println(in+"Echo:"+str);
    	    out.flush();
    
    
                       
                while (!done){
    			
                    if (str == null)
                        done = true;
    
                    else{
                         out.flush();
                    }
                }
                    File fil = new File("CarBranchInfo.txt");
                    
                    fin = new FileInputStream(fil);
                    buff = new BufferedInputStream(fin);
                    ds = new DataInputStream(buff);
                    
                    /* delimiter */  
                    String delimiter = " ";
                    while (ds.available() != 0) {
    
                   // this statement reads the line from the file 
                   store = ds.readLine();
                   /* given string will be split by the argument delimiter provided. */
                   temp = store.split(delimiter);
                   
                   for(int idx=0; idx < temp.length; idx++)
                   {
                     if (str.equals(temp[idx]))
                      {
                        System.out.print("Car Exists!");
                      }
                   }
                   
          }
          
                // dispose all the resources after using them.
                fin.close();
                buff.close();
                ds.close();
                incoming.close();
             } catch (Exception e){
                 System.out.println(e);
             } 
    
        }
    CLIENT:

    Code:
     // C generates session key and subscripts it, does encryption & decryption
    //Check timestamp doesn't differ from curr time for more than 1 min
    //Timestamp value should be same everywhere
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.sql.Timestamp;
    
    class Client{
    	public static void main(String[] args) {
    		String str1="";
    		String str2="";
    		String str3="";
    		String str4="";
            String clientCar="";
    		String clientBranch="";
    		String clientDriver="";
    		String clientPasswd="";
    
    		DataOutputStream out;
    		DataInputStream in;
    
    
    		try {
    		    Socket t = new Socket("127.0.0.1", 9001);
    
                  	    in = new DataInputStream(t.getInputStream());
                        out = new DataOutputStream(t.getOutputStream());
               
    
    		 BufferedReader br = new BufferedReader
                         (new InputStreamReader(System.in));
                            
    			boolean more = true;
    			System.out.println(in.readUTF());
                
    
    		while (more) {
    
    				clientCar = in.readUTF();
    			    	System.out.print(clientCar);
    			    	
    			    clientBranch = in.readUTF();
    			    	System.out.print(clientBranch);
    			    	
    			    clientDriver = in.readUTF();
    			    	System.out.print(clientDriver);
    			    	
    			    clientPasswd = in.readUTF();
    			    	System.out.print(clientPasswd);
    		
    		str1 = br.readLine();
    		str2 = br.readLine();
    		str3 = br.readLine();
    		str4 = br.readLine();
    		
    		out.writeUTF(str1);
    		out.writeUTF(str2);
    		out.writeUTF(str3);
    		out.writeUTF(str4);
    		out.flush();
    		
    		clientCar = in.readUTF();	    
                    
                    if (clientCar == null)
                        more = false;
                    else
                        System.out.print(clientCar);
                        
            clientBranch = in.readUTF();
           
                   if (clientBranch == null)
                        more = false;
                    else
                        System.out.print(clientBranch);
                        
            clientDriver = in.readUTF();
           
                   if (clientDriver == null)
                        more = false;
                    else
                        System.out.print(clientDriver);
                        
            clientPasswd = in.readUTF();
           
                   if (clientBranch == null)
                        more = false;
                    else
                        System.out.print(clientPasswd);
                        
                        
                }
    
             } catch(IOException e){
    			    System.out.println("Error" + e);
    	     }
    	      //System.out.print(str);
    	     //java.util.Date date= new java.util.Date();
    	     //System.out.println(new Timestamp(date.getTime()));
    
         }
    }
    
    
    
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Desperate Help Needed

    Check your server 'while' loop - it just sits there flushing the output stream - it looks like there's a bunch of stuff missing. Even if the loop finished and the CarBranchInfo file was opened, I can't see where anything else gets output.

    I suggest you either reformat the code so that the indentations match the curly braces, then step through it by hand, noting what happens and compare it with the sequence of steps from your design on paper. Or go back to the version that worked (before the comparison), then work out on paper how to fit it in before you write any code.

    The sooner you start to code, the longer the program will take...
    R. Carlson
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)