Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Sending zip from server socket

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sending zip from server socket

    I have been programing a multiplayer game and have been running in to a problem

    This code closes the socket
    out = p.getSocket().getOutputStream();
    ZipOutputStream mZip = new ZipOutputStream(out);
     
    //Write code to mZip
     
    mZip.flush();
    mZip.close();
    out.close();

    but how do i do so i can send more zip files?

    also the client have a almost the same problem, then i read the data i can only read it once of every time i close the clients mZip to read the next zip file that comes in i closes the socket :/

    what should i do?

    Server send code
    public static void sendData(Player[] players) 
    	{
    		for(Player p : players)
    		{
    			OutputStream out = null;
     
    			try
    			{
    				out = p.getSocket().getOutputStream();
    				ZipOutputStream mZip = new ZipOutputStream(out);
    				mZip.setLevel(1);
     
    				//send players
    				mZip.putNextEntry(aZip[0]);
    				for(Player pp : players)
    				{
    					mZip.write(pp.getData());
    				}
     
    				mZip.flush();
    				mZip.close();
    				out.close();
     
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}

    Cilent Receive code
    Socket s = me.getSocket();
    		while(!s.isClosed())
    		{
    			try
    			{
    				ZipInputStream mZip = new ZipInputStream(s.getInputStream());
    				if(mZip != null && mZip.available() > 0)
    				{
    					if(mZip.getNextEntry() != null)
    					{
    						String line;
    						ArrayList<Player> temp = new ArrayList<Player>();
    						BufferedReader reader = new BufferedReader(new InputStreamReader(mZip));
    						while((line = reader.readLine()) != null)
    						{
    							Player p = new Player(null);
    							p.setName(line);
    							temp.add(p);
    						}
     
    						if(temp.size() != 0)
    						{
    							players = new Player[temp.size()];
    							temp.toArray(players);
     
    							Menu m = Main.menu;
    							m.getFrame().getContentPane().removeAll();
    							m.add(m.getPlayerPanel());
    							m.validate();
    							m.repaint();
    						}
    					}
     
    					mZip.close();
    				}
     
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    Last edited by moon_werewolf; May 30th, 2011 at 07:55 AM.


Similar Threads

  1. [SOLVED] Sending xml file over a socket
    By Kakashi in forum Java Networking
    Replies: 2
    Last Post: March 9th, 2011, 09:41 AM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  4. Sending object through socket
    By Alexandrinne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 16th, 2010, 02:18 AM
  5. sending objects from client to server
    By 11moshiko11 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 8th, 2010, 04:47 AM