initialise 0 Newbie Poster

Hi All,

I am writing a client server program that has quite a few GUI's in it. Basically, everything is working except for the problem I'm encountering. Well, obviously :P I am not currently running my server program in order to simulate what would happen to the client if the server connection went down for whatever reason. The exception of not being connected is caught. However, within this exception block I call a GUI, in a class "SocketError", alerting the user to the fact that the program has encountered an error. Then the program exits. However, this GUI does not appear on screen even though the program does register the forced ending of the program in the "SocketError" class.


I call the client class to bind the socket in two separate places in my program. In the first instance, at the beginning of the program, if I close the socket in the "SocketError" class then, yet only sometimes, the GUI will flash momentarily before the next window will be called up. I know that once the socket has closed the program returns from the "SocketError" class to where it was called from, bypassing my


and causing the main menu window to be called up when I want the program to end. If I call the client class to bind the socket just before the end of my program, where again I make use of


, then the GUI doesn't appear at all.

My question is: How do I manage to get my GUI to appear on screen and my sleep and exit carried out with the socket closed? Or, if I do not need to close the socket - seeing as it wasn't accepted - how do I get the GUI to appear?

My Client code:

public class Client{
  Socket sock;

  Client() {
        BufferedOutputStream bos;
	BufferedInputStream bis;
	int in;
		//1. creating a socket to connect to the server
		sock = new Socket("", 1517);
		File myFile = new File("C:/Extras/Documents.txt");
		bis = new BufferedInputStream(new FileInputStream(myFile));
		bos = new BufferedOutputStream(sock.getOutputStream());
		byte[] byteArray = new byte[(int)myFile.length()];
		while((in =!= -1)
		File file = new File("C:/Extras/Documents.txt");
	catch (Exception e) {
	  SocketError GUI = new SocketError(sock);

and my SocketError class:

import java.awt.*;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Graphics.*;
import javax.swing.JComponent.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
import java.util.*;

public class SocketError extends JFrame
 public SocketError(Socket sock)
   Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
   setPreferredSize(new Dimension(dim.width,dim.height));

   JPanel mainPanel = new JPanel();
   mainPanel.setPreferredSize(new Dimension(dim.width,dim.height));
   mainPanel.setBackground(new Color(102, 102, 102));
   mainPanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   mainPanel.setLayout(new GridBagLayout());
   Border bevelBorder = BorderFactory.createBevelBorder(1, Color.GRAY, Color.WHITE);
   JButton jButton1 = new JButton();
   JLabel jLabel1 = new JLabel();
   JLabel jLabel2 = new JLabel();
   JLabel jLabel3 = new JLabel();

   jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel1.setForeground(new java.awt.Color(255, 255, 255));
   jLabel1.setText("The program has encountered a problem");

   jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel2.setForeground(new java.awt.Color(255, 255, 255));
   jLabel2.setText("The program will now end");
   jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24));
   jLabel3.setForeground(new java.awt.Color(255, 255, 255));
   jLabel3.setText("Please notify the person in charge");
   GridBagConstraints ecHeading = new GridBagConstraints();
   ecHeading.fill = GridBagConstraints.NONE;
   ecHeading.weightx = 0.5;
   ecHeading.weighty = 0.3;
   ecHeading.gridx = 0;
   ecHeading.gridy = 2;
   mainPanel.add(jLabel1, ecHeading);
   GridBagConstraints ecHeading2 = new GridBagConstraints();
   ecHeading2.fill = GridBagConstraints.NONE;
   ecHeading2.weightx = 0.5;
   ecHeading2.weighty = 0.3;
   ecHeading2.gridx = 0;
   ecHeading2.gridy = 4;
   mainPanel.add(jLabel2, ecHeading2);

   GridBagConstraints ecHeading3 = new GridBagConstraints();
   ecHeading3.fill = GridBagConstraints.NONE;
   ecHeading3.weightx = 0.5;
   ecHeading3.weighty = 0.3;
   ecHeading3.gridx = 0;
   ecHeading3.gridy = 6;
   mainPanel.add(jLabel3, ecHeading3);
   try {
      if (sock != null) sock.close();
   catch (Exception e) {}

Thanks in advance :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.