Results 1 to 1 of 1
  1. #1
    shmatty52 is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Receive multiple vectors (communication client-server)

    Hi, I am newbie in java, I have a problem in communication client - server, I don't know how to read more than one vector, which I send. I'll appreciate your help :). My code is:
    Server:
    Java Code:
    import java.net.*;
    import java.util.Vector;
    import java.io.*;
    
    public class FileServer {
      private static ObjectOutputStream outStream;
    
    public static void main (String [] args ) throws IOException {
        // create socket
        ServerSocket servsock = new ServerSocket(2926);
        while (true) {
          System.out.println("Waiting...");
     
          
          Socket sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);
          outStream = new ObjectOutputStream(sock.getOutputStream());
          // sendfile
          File myFile = new File ("D://music.mp3");
         
          
          Vector<Integer> send = new Vector<Integer>();  
          int CRC=0;
          int part=0;
        	  for(int num=1;num<=(int) myFile.length()/60;num++){
        		  CRC= (part+num)%3;
        		  part = num*60;
        		  send.addElement(num);    		  
        		  send.addElement(part);
        		  send.addElement(CRC);
        		  outStream.writeObject(send);
        		  System.out.println(send);
        		  send.removeElement(num);
        		  send.removeElement(part);
        		  send.removeElement(CRC);
        		  
        		  if((myFile.length()<=(num+1)*60)&&(myFile.length()%60!=0)){
        			  int left = (int) (myFile.length()%60+part);
        			  int Num=num+1; 
        			  CRC = (left + num)%2;
        			  
        			  send.addElement(Num);
        			  send.addElement(left);    			  
            		  send.addElement(CRC);
            		  outStream.writeObject(send);
            		  System.out.println(send);
            		  send.removeElement(Num);
            		  send.removeElement(part);
            		  send.removeElement(CRC);
        	   
          
    	  
          System.out.println("Sending...");
          
        		  }
      	    }
          outStream.close();
       
          sock.close();
         
       }
      }
     }
    Error on server side:
    Java Code:
    [1 60 1]
    ....
    [64367, 3862020, 2]
    [64368, 3862047, 0]
    Sending...
    Exception in thread "main" java.net.SocketException: Socket closed
    	at java.net.SocketOutputStream.socketWrite(Unknown Source)
    	at java.net.SocketOutputStream.write(Unknown Source)
    	at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    	at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    	at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
    	at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
    	at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    	at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    	at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    	at java.io.ObjectOutputStream.writeObject(Unknown Source)
    	at FileServer.main(FileServer.java:61)
    My client side:
    Java Code:
    import java.net.*;
    import java.util.Vector;
    import java.io.*;
    
    public class FileClient{
    	
      private static InetAddress host;	
      public static void main (String [] args ) throws ClassNotFoundException, UnknownHostException, IOException {
        
    	host = InetAddress.getLocalHost();
    	Socket sock = new Socket(host,2926);
        System.out.println("Connecting...");
        ObjectInputStream inStream = new ObjectInputStream(sock.getInputStream());
            
        long start = System.currentTimeMillis();
            
        for(int a=1;a<=100;a++){
        System.out.println("seq number of received vector: "+a);
        	
        @SuppressWarnings("unchecked")    
    	Vector <send> response = (Vector<send>)inStream.readObject(); 
    	long end = System.currentTimeMillis();
      	System.out.println(response);
       	System.out.println("time: "+(end-start));
       	
       	
        }
    	
    	
    	sock.close();
    
           
      }
    
    
    }
    and the error from the client side: (there is no error, but I want to see at least 100 vectors, not 100 times the first one)
    Java Code:
    seq number of received vector: 1
    [1, 60, 1]
    time: 71
    ...
    seq number of received vector: 100
    [1, 60, 1]
    time: 79
    Last edited by shmatty52; 07-16-2012 at 09:08 PM.

Similar Threads

  1. Replies: 0
    Last Post: 07-15-2012, 08:57 AM
  2. How to Send Multiple File from Server to Client
    By reza1990 in forum Networking
    Replies: 9
    Last Post: 08-01-2011, 06:55 PM
  3. Replies: 8
    Last Post: 09-24-2010, 04:34 AM
  4. Client cannot receive inputstream from server
    By chyrl in forum Networking
    Replies: 2
    Last Post: 08-29-2010, 06:13 PM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 03:21 PM

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
  •