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
Code :
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
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
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();
}
}