By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

XML Socket Server, Works but only after everyone disconnects!?

P: 1
I have programmed an XML Socket Server to communicate with Flash.
It doesn't print out the messages it has received from the flash until the server has been shut down. Any ideas?

The part with the problem:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             //Create streams
  3.             BufferedReader data_in = new BufferedReader(
  4.                     new InputStreamReader(incoming.getInputStream()));
  5.             PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
  6.  
  7.             //Print sample welcome message
  8.             data_out.println("<MESSAGE USER='John' TEXT='Hello, my name is John!' />" + EOF);
  9.             data_out.flush();        
  10.  
  11.             boolean loopMAN = false;
  12.             String line = null;
  13.             while(null!=(line=data_in.readLine()))
  14.             {
  15.                 //Print in server console
  16.                 System.out.println("Received message : " + line);
  17.                 //Write out to all clients connected
  18.                 data_out.write(line + EOF);
  19.                 data_out.flush();
  20.             }
  21.  
  22.             //Close the server
  23.             data_out.close();
  24.             incoming.close();
  25.             s.close();
  26.             loopMAN = true;
  27.  
  28.             //Close the server if all clients disconnect
  29.             if(loopMAN)
  30.             {
  31.                 System.out.println("All clients disconnected!\nServer shut down.");
  32.                 System.exit(-1);
  33.             }
  34.  
The whole code:
Expand|Select|Wrap|Line Numbers
  1. // Name: XML Socket Server
  2. // Description: Communication with Macromedia Flash
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class ServerMain
  9. {
  10. public static void main(String[] args)
  11. {
  12.         char EOF = (char)0x00;
  13.         try
  14.         {
  15.             //Start server
  16.             ServerSocket s = new ServerSocket(2000);
  17.             System.out.println("Server running! Now accepting connections...");
  18.             Socket incoming = s.accept();
  19.             incoming.setTcpNoDelay(true);
  20.  
  21.             //Create streams
  22.             BufferedReader data_in = new BufferedReader(
  23.                     new InputStreamReader(incoming.getInputStream()));
  24.             PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
  25.  
  26.             //Print sample welcome message
  27.             data_out.println("<MESSAGE USER='John' TEXT='Hello, my name is John!' />" + EOF);
  28.             data_out.flush();        
  29.  
  30.             boolean loopMAN = false;
  31.             String line = null;
  32.             while(null!=(line=data_in.readLine()))
  33.             {
  34.                 //Print in server console
  35.                 System.out.println("Received message : " + line);
  36.                 //Write out to all clients connected
  37.                 data_out.write(line + EOF);
  38.                 data_out.flush();
  39.             }
  40.  
  41.             //Close the server
  42.             data_out.close();
  43.             incoming.close();
  44.             s.close();
  45.             loopMAN = true;
  46.  
  47.             //Close the server if all clients disconnect
  48.             if(loopMAN)
  49.             {
  50.                 System.out.println("All clients disconnected!\nServer shut down.");
  51.                 System.exit(-1);
  52.             }
  53.         }
  54.         catch (IOException e)
  55.         {
  56.             //Catch the exception
  57.             System.out.println("Client Disconnected");
  58.         }
  59. }
  60. }
May 25 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.