aspose file tools*
The moose likes I/O and Streams and the fly likes Help(Client Fails to read from Server) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Help(Client Fails to read from Server)" Watch "Help(Client Fails to read from Server)" New topic
Author

Help(Client Fails to read from Server)

Thabo Matjuda
Greenhorn

Joined: Jul 06, 2008
Posts: 29
Hey Guys. I need Help. My client doesnt read when server echoes back in a multi-client server application. I get "ArrayIndexOutOfBoundsException". What happens is i send a request via client to server. The server sees the request, the processes it and prints the data back to client but client only prints "0". I used this variable "String fromServ = in.readLine()" to read from Server. Any ideas why my client fails to read from server?


T.Tuch (T.L.M)
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Hi Thabo,

You're not giving us enough information to even make a guess what might be wrong. How are you connecting client and server? Through a socket? RMI? Pipe? What array is going out of bounds? What is "in"?

You probably want to post some code ... as little as possible ... to demonstrate the problem. Use code tags.
Thabo Matjuda
Greenhorn

Joined: Jul 06, 2008
Posts: 29
Hey Charles. I am connecting via Sockets. "in" is my BufferedReader which covers my InputStreamReader. Everytime i use it to read a resultset from Server, it returns "0" and for example if i queried lets say Movie Genres. My "in" wil return "0" when i pres my action Button, it shows "Animation", when i press again it shows "AnimationFantasy" and so it keeps on Adding so and so, instead of showing them all at once.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Well, I can give you some theories based on my nearly blind guesses of what your app may be doing.

You somehow send a command to the server that causes it to write out a series of strings to the socket. You have an "action button" that causes your client to read a line of the input every time you press it. I don't know where the 0 is coming from, but the rest of the display is the movie genres. You only get one at a time because that's how you implemented your action: read one line from the socket and display it.

Hope that helps!

Greg

Thabo Matjuda
Greenhorn

Joined: Jul 06, 2008
Posts: 29
OK. I think i should give you highlights on what should happen in this project and upload the 2 classes dealing with the whole search engine. I believe you can help me. Thank you
Thabo Matjuda
Greenhorn

Joined: Jul 06, 2008
Posts: 29
Summarised Specifications.
* I must USE Access OR SQL 2000 to create a Movie Database
* Must I am using netbeans five point five TO:
- create a multi-client server application
- create a search gui(Mine named mUserGUI) which will be used to search movies from SQL Database
- Server must be the one querying every client request and echo it to client through use of threads
- When server doesn' find client's request then Message regarding that must display


I have a questions and I need help.
1.What does it mean when My Multi-Client Server recieves requests from client, processes the request, sends it to client but client only sees "0"?
I used String fromServ = in.readLine() to read from server, but still nothing.
2.How do I check and let the client know when server hasn't found any movie record
So far that's it.
3.I need Help In Using TreeSet in my client, to Get Movie ResultSet from server & Fill My JTable in the client

Classes I am using so far are:
- mUserGUI (My Client GUI used for searching)
- Server (Main Multi-Client Server class)
- mServer (My Server frame)

SERVER

/*
* FILENAME: Server.java
* AUTHOR: Thabo Matjuda
* DATE: 03 March 2009, 03:25 am
* DESCRIPTION: This file is the Movie Store Server
* This is the class that does all the client services
* This File waits on client request to process them
*/

package moviestoreserver;

/**
*
* @author Administrator
*/

import java.io.*;
import java.net.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread.*;
import java.util.*;

//Server Class creates server socket
//This class also waits on clients to connect
public class Server implements Runnable {

//Class Variables
int port = 6666;
ServerSocket ss;
boolean waiting;
Thread runz;


/** Creates a new instance of Server */
//Constructor
public Server() {

//Try/Catch Block to catch server socket exceptions
try {

//Create the server socket
ss = new ServerSocket(port);
waiting = true;
JOptionPane.showMessageDialog(null,
"Server Started & Running");
} catch(IOException ioe) {
JOptionPane.showMessageDialog(null,
ioe.toString(),
"Server IOError",
JOptionPane.WARNING_MESSAGE);
} catch(Exception err) {
JOptionPane.showMessageDialog(null,
err.toString(),
"Server Error",
JOptionPane.WARNING_MESSAGE);
}

//Start Thread below
if (runz == null) {
runz = new Thread(this);
runz.start();
}
}

//Run Method
public void run() {

//Accepts clients
//Waiting for clients
while(waiting) {
try {

//create client socket
//start a new client session
new Session(ss.accept());
} catch(IOException ioe) {
JOptionPane.showMessageDialog(null,
ioe.toString(),
"Waiting IOError",
JOptionPane.WARNING_MESSAGE);
} catch(Exception err) {
JOptionPane.showMessageDialog(null,
err.toString(),
"Waiting Error",
JOptionPane.WARNING_MESSAGE);
}
}
}
}

//Class Session
//Creates stream to comuunicate with clients
//It Processes all the client requests
class Session implements Runnable {

//This class's Variables
Socket soc;
BufferedReader br;
PrintWriter pw;
Thread runner;
Connection dbConn;
ResultSet res;
Statement state;
String[] optionz = {"OK"};

/** Creates a new instance of Session */
//Constructor Session
public Session(Socket s) {
soc = s;

//TRY/CATCH Block For The Streams & Database Connection
try {

//Conection to database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbConn = DriverManager.getConnection("jdbcdbcBMovieStore");

//Streams for Client communication
br = new BufferedReader( new InputStreamReader(
soc.getInputStream()));
pw = new PrintWriter( new BufferedOutputStream(
soc.getOutputStream()), true);

//Server message to notify client wen connected
int servmsg = JOptionPane.showOptionDialog(null,
"Connected to Server!! ",
"Server Message",
0,
JOptionPane.INFORMATION_MESSAGE,
null,
optionz,
optionz[0]);

//Sending connection status to client
pw.println(servmsg);
} catch(IOException ioe) {
JOptionPane.showMessageDialog(null,
ioe.toString(),
"Session IO Error",
JOptionPane.WARNING_MESSAGE);
} catch(ClassNotFoundException cnfe) {
JOptionPane.showMessageDialog(null,
cnfe.toString(),
"Session Class Not Found Error",
JOptionPane.WARNING_MESSAGE);
} catch(SQLException sqle) {
JOptionPane.showMessageDialog(null,
sqle.toString(),
"Session SQL Error",
JOptionPane.ERROR_MESSAGE);
}

//Start thread "runner" below
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

//This method is used to return the Movie records queried to the client
public void getData() {
try {

//Loops through all the rows returned from the query and
//writes them to the client
while (res.next()) {
int movieID = res.getInt(1);
String movieName = res.getString(2);
String movieDesc = res.getString(3);
String genre = res.getString(4);
pw.println(movieID);
pw.println(movieName);
pw.println(movieDesc);
pw.println(genre);
}

//Tells Client that it has Returned
pw.println("Returned");
} catch(SQLException sqle) {
JOptionPane.showMessageDialog(null,
sqle.toString(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
}
}

//Called when thread starts
public void run() {

//While runner is the current thread
while (runner == Thread.currentThread()) {
try {
String queryType = br.readLine();
System.out.println(queryType);

//Checks Which Query To Work with
if (queryType != null) {

//When Server receives data about Movie
if (queryType.endsWith("$")) {
int a = queryType.length()-1;
String tempQuery = queryType.substring(0, a);
System.out.println(tempQuery);

Statement state = dbConn.createStatement();
res = state.executeQuery(
"SELECT M.movie_id, M.movie_name, M.m_description, G.genre_name " +
"FROM Muviez M INNER JOIN Genrez G " +
"ON M.genre_id = G.genre_id " +
"WHERE M.movie_name LIKE '%" + tempQuery + "%'");
getData();
}
}
} catch(IOException ioe) {
runner = null;
JOptionPane.showMessageDialog(null,
ioe.toString(),
"Session IO Error",
JOptionPane.WARNING_MESSAGE);
} catch(SQLException sqle) {
JOptionPane.showMessageDialog(null,
sqle.getNextException(),
"Session SQL Error",
JOptionPane.WARNING_MESSAGE);
} catch(Exception err) {
JOptionPane.showMessageDialog(null,
err.toString(),
"Session Error",
JOptionPane.WARNING_MESSAGE);
}
}
}
}


Thabo Matjuda
Greenhorn

Joined: Jul 06, 2008
Posts: 29
MY SEARCH GUI

/*
* FILENAME: mUserGUI.java
* AUTHOR: Thabo Matjuda
* DATE: 05 March 2009, 16:12 am
* DESCRIPTION: This file is the client application
* It is the GUI used to send requests to server
* It is used as a Search Application which must be connected to server
* It also shows data resulting to your search
*/

package moviestoreclient;

/**
*
* @author Lebogang
*/

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.regex.*;
import java.util.*;

//This class is a client whisch searches from the server
public class mUserGUI extends javax.swing.JFrame {

/** Class Varials*/
Socket soc;

//Host OR Server IP address
public String servIP;

//Port used to connect to server
int port = 6666;

//These initialises my Communication streams
PrintWriter out;
BufferedReader in;

//Arrays to deal with my Movie Table
Object[] [] rowz;
String[] columnNames = {"MovieID", "Title", "Description", "Genre"};

//This is going to hold the values retrieved from the database
TreeSet<String> movSet = new TreeSet<String>();

/** Creates new form mUserGUI */
//Constructor
public mUserGUI() {
super("M.Store Search");
initComponents();

//Setting the GUI's Look & Feel
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows." +
"WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception t) {}

//Setting InputDialog Active
//This will get the address of the server to connect
servIP = JOptionPane.showInputDialog(null,"Please insert the" +
" IP of the server","Server address",JOptionPane.PLAIN_MESSAGE);

//Tests to see if the servIP is a valid IP
if (!Pattern.matches("^\\d*\\.?\\d*\\.?\\d*\\.?\\d*$",servIP)) {
JOptionPane.showMessageDialog(null, "Seeking Server..." );
System.exit(1);
}

//This will try connection to server
//Creates streams to be used for connection to server
try {
soc = new Socket(servIP, port);

out = new PrintWriter(new BufferedOutputStream(
soc.getOutputStream()), true);
in = new BufferedReader(new InputStreamReader(
soc.getInputStream()));
} catch(UnknownHostException uherr) {
JOptionPane.showMessageDialog(null,
uherr.toString(),
"Unknown Host Error",
JOptionPane.WARNING_MESSAGE);
} catch(IOException ioe) {
JOptionPane.showMessageDialog(null,
ioe.toString(),
"IO Error",
JOptionPane.WARNING_MESSAGE);
} catch(Exception eer) {
JOptionPane.showMessageDialog(null,
eer.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}

//Setting the table properties
rowz = new Object[0][4];
tblMovie = new JTable(rowz, columnNames);
scrPane.getViewport().add(tblMovie);

//Disabled Edit Data button
btnEdit.setEnabled(false);
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
puser = new javax.swing.JPanel();
lblTitle = new javax.swing.JLabel();
puser2 = new javax.swing.JPanel();
lblSearchMov = new javax.swing.JLabel();
tfSearchMov = new javax.swing.JTextField();
lblOR = new javax.swing.JLabel();
btnSearch = new javax.swing.JButton();
lblSearchGen = new javax.swing.JLabel();
tfGenre = new javax.swing.JTextField();
scrPane = new javax.swing.JScrollPane();
tblMovie = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
lblUserN = new javax.swing.JLabel();
lblUserword = new javax.swing.JLabel();
tfUserN = new javax.swing.JTextField();
btnLog = new javax.swing.JButton();
pfUser = new javax.swing.JPasswordField();
btnEdit = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
userMenuBar = new javax.swing.JMenuBar();
userMenu = new javax.swing.JMenu();
itemSearch = new javax.swing.JMenuItem();
itemEdit = new javax.swing.JMenuItem();
separator1 = new javax.swing.JSeparator();
itemExit = new javax.swing.JMenuItem();
userHelp = new javax.swing.JMenu();
itemHostHelp = new javax.swing.JMenuItem();
itemHelp = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
puser.setBackground(new java.awt.Color(0, 0, 0));
puser.setForeground(new java.awt.Color(255, 255, 255));
lblTitle.setBackground(new java.awt.Color(0, 0, 0));
lblTitle.setFont(new java.awt.Font("Tahoma", 1, 14));
lblTitle.setForeground(new java.awt.Color(255, 255, 255));
lblTitle.setText("MOVIE STORE SEARCH");

puser2.setBackground(new java.awt.Color(51, 51, 51));
puser2.setForeground(new java.awt.Color(255, 255, 255));
lblSearchMov.setBackground(new java.awt.Color(51, 51, 51));
lblSearchMov.setFont(new java.awt.Font("Tahoma", 1, 11));
lblSearchMov.setForeground(new java.awt.Color(255, 255, 255));
lblSearchMov.setText("Movie Title:");

lblOR.setBackground(new java.awt.Color(51, 51, 51));
lblOR.setFont(new java.awt.Font("Tahoma", 1, 12));
lblOR.setForeground(new java.awt.Color(255, 255, 255));
lblOR.setText("OR");

btnSearch.setBackground(new java.awt.Color(51, 51, 51));
btnSearch.setText("SEARCH");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});

lblSearchGen.setBackground(new java.awt.Color(51, 51, 51));
lblSearchGen.setFont(new java.awt.Font("Tahoma", 1, 11));
lblSearchGen.setForeground(new java.awt.Color(255, 255, 255));
lblSearchGen.setText("By Genre:");

org.jdesktop.layout.GroupLayout puser2Layout = new org.jdesktop.layout.GroupLayout(puser2);
puser2.setLayout(puser2Layout);
puser2Layout.setHorizontalGroup(
puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, puser2Layout.createSequentialGroup()
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puser2Layout.createSequentialGroup()
.add(23, 23, 23)
.add(lblSearchMov))
.add(puser2Layout.createSequentialGroup()
.add(26, 26, 26)
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(lblSearchGen)
.add(lblOR))))
.add(26, 26, 26)
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(tfGenre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
.add(tfSearchMov, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
.add(btnSearch))
.add(56, 56, 56))
);
puser2Layout.setVerticalGroup(
puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puser2Layout.createSequentialGroup()
.add(24, 24, 24)
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lblSearchMov)
.add(tfSearchMov, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puser2Layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(lblOR))
.add(puser2Layout.createSequentialGroup()
.add(25, 25, 25)
.add(puser2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lblSearchGen)
.add(tfGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
.add(31, 31, 31)
.add(btnSearch)
.addContainerGap(43, Short.MAX_VALUE))
);

tblMovie.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
scrPane.setViewportView(tblMovie);

jPanel1.setBackground(new java.awt.Color(51, 51, 51));
jPanel1.setForeground(new java.awt.Color(255, 255, 255));
lblUserN.setBackground(new java.awt.Color(51, 51, 51));
lblUserN.setFont(new java.awt.Font("Tahoma", 1, 11));
lblUserN.setForeground(new java.awt.Color(255, 255, 255));
lblUserN.setText("UserName:");

lblUserword.setBackground(new java.awt.Color(51, 51, 51));
lblUserword.setFont(new java.awt.Font("Tahoma", 1, 11));
lblUserword.setForeground(new java.awt.Color(255, 255, 255));
lblUserword.setText("Password:");

btnLog.setBackground(new java.awt.Color(51, 51, 51));
btnLog.setText("LOGIN");
btnLog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLogActionPerformed(evt);
}
});

pfUser.setEchoChar('+');

btnEdit.setBackground(new java.awt.Color(51, 51, 51));
btnEdit.setText("EDIT DATA");
btnEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditActionPerformed(evt);
}
});

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(21, 21, 21)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(lblUserN)
.add(lblUserword))
.add(23, 23, 23)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(btnEdit)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(btnLog)
.add(tfUserN, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
.add(pfUser)))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(30, 30, 30)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lblUserN)
.add(tfUserN, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(26, 26, 26)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(lblUserword)
.add(pfUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(15, 15, 15)
.add(btnLog)
.add(14, 14, 14)
.add(btnEdit)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("USER LOGIN");

org.jdesktop.layout.GroupLayout puserLayout = new org.jdesktop.layout.GroupLayout(puser);
puser.setLayout(puserLayout);
puserLayout.setHorizontalGroup(
puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, puserLayout.createSequentialGroup()
.add(51, 51, 51)
.add(puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, scrPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
.add(puserLayout.createSequentialGroup()
.add(puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(lblTitle)
.add(puser2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(29, 29, 29)
.add(puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puserLayout.createSequentialGroup()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 201, Short.MAX_VALUE))
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.add(60, 60, 60))
);
puserLayout.setVerticalGroup(
puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puserLayout.createSequentialGroup()
.add(34, 34, 34)
.add(puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lblTitle)
.add(jLabel1))
.add(19, 19, 19)
.add(puserLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(puser2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(22, 22, 22)
.add(scrPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(46, Short.MAX_VALUE))
);

userMenuBar.setBackground(new java.awt.Color(0, 0, 0));
userMenuBar.setForeground(new java.awt.Color(255, 255, 255));
userMenu.setBackground(new java.awt.Color(0, 0, 0));
userMenu.setForeground(new java.awt.Color(255, 255, 255));
userMenu.setText("Menu");
userMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
userMenuActionPerformed(evt);
}
});

itemSearch.setText("Search");
userMenu.add(itemSearch);

itemEdit.setText("Edit Data");
userMenu.add(itemEdit);

userMenu.add(separator1);

itemExit.setText("Exit");
itemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemExitActionPerformed(evt);
}
});

userMenu.add(itemExit);

userMenuBar.add(userMenu);

userHelp.setBackground(new java.awt.Color(0, 0, 0));
userHelp.setForeground(new java.awt.Color(255, 255, 255));
userHelp.setText("Help");
itemHostHelp.setText("Host Help");
itemHostHelp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemHostHelpActionPerformed(evt);
}
});

userHelp.add(itemHostHelp);

itemHelp.setText("Search Help");
userHelp.add(itemHelp);

userMenuBar.add(userHelp);

setJMenuBar(userMenuBar);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(puser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, puser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents

//Opens the data editing Window
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
Object src = evt.getSource();
if (src.equals(btnEdit)) {
DataGUI edit = new DataGUI();
}
}//GEN-LAST:event_btnEditActionPerformed

private void btnLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogActionPerformed
// Object src = evt.getSource();
// if (src.equals(btnLog)) {
// String fromServer;
// try {
//
// //Shorten my Jfields
// String uzer = tfUserN.getText();
// String uword = new String(pfUser.getPassword());
//
// //Writes to the query to the server
// out.println(
// "SELECT * FROM Uzerz " +
// "WHERE uzer_name = '" + uzer + "'" +
// "AND pazzword = '" + uword + "'i*");
//
// //Reading feedback from server
// fromServer = in.readLine();
// System.out.println(fromServer);
//
// //Checks to see if the userName and password is correct
// if (fromServer.equals("badlogin")) {
// JOptionPane.showMessageDialog(null,"The username or " +
// "password is incorrect",
// "Login failed",
// JOptionPane.WARNING_MESSAGE);
// } else if (fromServer.equals("goodlogin")){
// JOptionPane.showMessageDialog(null,
// "Login Successfull",
// "Valid Data",
// JOptionPane.WARNING_MESSAGE);
// btnEdit.setEnabled(true);
// }
// } catch(IOException ioe) {
// JOptionPane.showMessageDialog(null,
// ioe.toString(),
// "IO Error",
// JOptionPane.ERROR_MESSAGE);
// } catch(Exception eer) {
// JOptionPane.showMessageDialog(null,
// eer.toString(),
// "Error Message",
// JOptionPane.ERROR_MESSAGE);
// }
// }
}//GEN-LAST:event_btnLogActionPerformed

private void userMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userMenuActionPerformed

}//GEN-LAST:event_userMenuActionPerformed
//Reconnects to Server If not connected //For Exiting client programme
private void itemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemExitActionPerformed
// Object src = evt.getSource();
// if (src.equals(itemExit)) {
// int choice = JOptionPane.showConfirmDialog(null,
// "Are you sure that you want to exit? ",
// "Exit Conirmation",
// JOptionPane.YES_NO_OPTION,
// JOptionPane.QUESTION_MESSAGE);
//
// //If User chooses yes
// //Client closes all its conections & exits
// if (choice == JOptionPane.YES_OPTION) {
// try {
// soc.close();
// in.close();
// out.close();
// System.exit(1);
// } catch(IOException ioe) {
// JOptionPane.showMessageDialog(null,
// ioe.toString(),
// "IO Error",
// JOptionPane.ERROR_MESSAGE);
// } catch(Exception err) {
// JOptionPane.showMessageDialog(null,
// err.toString(),
// "Error",
// JOptionPane.ERROR_MESSAGE);
// }
// }
// }
}//GEN-LAST:event_itemExitActionPerformed

//Button action is used to search
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
Object src = evt.getSource();
if (src.equals(btnSearch)) {

//Setting tblMovie
rowz = new Object[0][4];
tblMovie = new JTable(rowz,columnNames);
scrPane.getViewport().add(tblMovie);

String fromServ;
String query;

//Shortening My textFiels' names
String title = tfSearchMov.getText();
String genre = tfGenre.getText();

//The Movie Fields Must match the following
Pattern pat = Pattern.compile("^[A-Z1-9]+[\\s\\w\\d]*");
Matcher m = pat.matcher(title);
boolean a = m.matches();

//When both fields are empty
//Then Display Message
if(title.equals("") && genre.equals("")) {
JOptionPane.showMessageDialog(null,
"Please Enter Movie OR Genre to search " +
"to search movie Record",
"Invalid Data",
JOptionPane.WARNING_MESSAGE);
}

//When Genre Field Is Empty
//Then This will Send Movie data
if (genre.equals("")) {

//Checks to see if movie title is typed correctly
//Will display message if not typed in capital letters
if(!a) {
JOptionPane.showMessageDialog(null,
"Make sure that Movir title is typed in capital letters",
"Invalid Data",
JOptionPane.WARNING_MESSAGE);
} else {

//Else the following code will start sending Movie Data
try {
out.println(title + "$");
String fromServer = in.readLine();
System.out.println(fromServer);
} catch(IOException ioe) {
JOptionPane.showMessageDialog(null,
ioe.toString(),
"IO Error",
JOptionPane.WARNING_MESSAGE);
} catch(Exception eer) {
JOptionPane.showMessageDialog(null,
eer.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
}//GEN-LAST:event_btnSearchActionPerformed

//This method is going to populate the table
/**
* Populates the tables
* @param ret Holds the records returned from server
*/
public void fillTable(TreeSet<String> movSet) {

int rN = 0;
int cN = 0;
Scanner scan;

//This is going to add all the rows in the collection to the table
for (String row : movSet) {
scan = new Scanner(row);
scan.useDelimiter(",");

//While there is a next token
while (scan.hasNext()) {
rowz[rN][cN] = scan.next();
cN++;
}

cN = 0;
rN++;
}

tblMovie = new JTable(rowz,columnNames);
scrPane.getViewport().add(tblMovie);
}

//Used to invoke search
private void itemHostHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemHostHelpActionPerformed
Object source = evt.getSource();
if (source.equals(itemHostHelp)) {
JOptionPane.showMessageDialog(null,
"If You are running this application on one pc then \n " +
"use This: 127.0.0.1 IP address \n " +
"Make sure you type in exactly the above IP address. \n " +
"Otherwise use another PC's IP address If you are running \n " +
"the client on a separate PC.");
}
}//GEN-LAST:event_itemHostHelpActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mUserGUI().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JButton btnEdit;
public javax.swing.JButton btnLog;
public javax.swing.JButton btnSearch;
public javax.swing.JMenuItem itemEdit;
public javax.swing.JMenuItem itemExit;
public javax.swing.JMenuItem itemHelp;
public javax.swing.JMenuItem itemHostHelp;
public javax.swing.JMenuItem itemSearch;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
public javax.swing.JLabel lblOR;
public javax.swing.JLabel lblSearchGen;
public javax.swing.JLabel lblSearchMov;
public javax.swing.JLabel lblTitle;
public javax.swing.JLabel lblUserN;
public javax.swing.JLabel lblUserword;
private javax.swing.JPasswordField pfUser;
public javax.swing.JPanel puser;
public javax.swing.JPanel puser2;
public javax.swing.JScrollPane scrPane;
public javax.swing.JSeparator separator1;
public javax.swing.JTable tblMovie;
public javax.swing.JTextField tfGenre;
public javax.swing.JTextField tfSearchMov;
public javax.swing.JTextField tfUserN;
public javax.swing.JMenu userHelp;
public javax.swing.JMenu userMenu;
public javax.swing.JMenuBar userMenuBar;
// End of variables declaration//GEN-END:variables

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41865
    
  63
Thabo,
please refrain from posting lengthy passages in colored text; those should be reserved to highlight short excerpts.

Also, for code, you should UseCodeTags. Otherwise It's unnecessarily hard to read, making it less likely that people will bother to do so.


Ping & DNS - my free Android networking tools app
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Wow Thabo! You go from too little information to too much. I took a quick look through your code. The reason you are seeing 0 appear on the client is that you are creating a JOptionPane saying "Connected to Server", but you are creating it on the server. You then take the status result from that pane, put it into svrmsg (an int), and write that to the socket. It's like you thought that svrmsg would be the string "Connected to Server", but it's not.

Anyway, your event handling is a bit confusing since so much of it is commented out. I think you are reading one line each time the button is pressed, and that's your problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help(Client Fails to read from Server)