Problems with client/server application
Hi, I'm new here! I've studied Java for 1 year now but it's been only a couple of month since I've started with the client server applications. I've to do an application abt a library and I'm having some trouble. I have to search for some books from the database using advanced search with some text boxes and then the result will show up in a panel. The code seems to run correctly but when I run the client, I expect the GUI of the application to show up but it doesn't happen a thing. Not an error or anything. I wonder if you can please give me some help and watch if the code is correct esp the client server part.Thank you in advance !
p.s The code is not complete.
THE CLIENT
Code :
package javaapplication1;
/**
*
* @author Lori
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Biblioteke{
public static void main(String[] args) {
MyFrame fr=new MyFrame();
fr.setTitle("Kerkimi i librave");
fr.connect();
fr.communicate();
fr.show();
}
}
class MyFrame extends JFrame implements ActionListener{
String emerserver="localhost";
int portaserver=32007;
Socket s;
DataOutputStream outdrejtserver;
BufferedReader inngaserver;
public MyFrame(){
final int w=1200;
final int h=750;
setSize(w,h);
Container cp=getContentPane();
JLabel l1=new JLabel();
l1.setPreferredSize(new Dimension(50, 0));
JLabel l2=new JLabel();
l2.setPreferredSize(new Dimension(50, 0));
JLabel l3=new JLabel();
l3.setPreferredSize(new Dimension(50, 0));
JLabel l4=new JLabel();
l4.setPreferredSize(new Dimension(20, 0));
panel=new JPanel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
searchpanel=new JPanel();
searchpanel.setLayout(new GridLayout(5,1));
p1.add(new JLabel("Titulli:"));
tf1=new JTextField(15);
p1.add(tf1);
p1.add(l1);
p1.add(new JLabel("Autori:"));
tf2=new JTextField(15);
p1.add(tf2);
p2.add(new JLabel("Viti:"));
tf3=new JTextField(15);
p2.add(tf3);
p2.add(l2);
p2.add(new JLabel("Gjuha:"));
tf4=new JTextField(15);
p2.add(tf4);
p3.add(new JLabel("Zhanri:"));
tf5=new JTextField(15);
p3.add(tf5);
p3.add(l3);
p3.add(new JLabel("Tipi:"));
tf6=new JTextField(15);
p3.add(tf6);
p4.add(new JLabel("Sh.botuese:"));
tf7=new JTextField(15);
p4.add(tf7);
p4.add(l4);
p4.add(new JLabel("Perkthyesi:"));
tf8=new JTextField(15);
p4.add(tf8);
searchpanel.add(p1);
searchpanel.add(p2);
searchpanel.add(p3);
searchpanel.add(p4);
panel.setLayout(new GridLayout(3,1));
panel.add(new JLabel(" "));
panel.add(new JLabel("----------------------------------------------------------------------------------------------------------------------------------------Kerkoni sipas kategorive:--------------------------------------------------------------------------------------------------------------------------------------------------"));
panel.add(new JLabel("\n \n "));
buton=new JButton("Kerko");
buton.addActionListener(this);
buttonpanel=new JPanel();
buttonpanel.add(new JLabel(" "));
buttonpanel.add(new JLabel(" "));
buttonpanel.add(buton);
buttonpanel.add(new JLabel(" "));
searchpanel.add(buttonpanel);
JLabel label2 = new JLabel("Label");
label2.setPreferredSize(new Dimension(100, 100));
JScrollPane jScrollPane = new JScrollPane(label2);
resultpanel=new MyPanel();
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.getViewport().add(resultpanel, null);
cp.add(panel, "North");
cp.add(searchpanel, "West");
cp.add(jScrollPane, "Center");
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if (source==buton){
s1=tf1.getText();
s2=tf2.getText();
s3=tf3.getText();
s4=tf4.getText();
s5=tf5.getText();
s6=tf6.getText();
s7=tf7.getText();
s8=tf8.getText();
int i=0;
while (i<=n){
resultpanel.writestr(string[i]);}
}
}
public void communicate(){
for(;;)
try{
outdrejtserver.writeBytes(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5+"\n"+s6+"\n"+s7+"\n"+s8);
n=0;
while(inngaserver.readLine()!=null){
n++;
string[n]=inngaserver.readLine();
s.close();
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
System.out.println("Gabim gjate komunikimit me server!");
System.exit(1);
}
}
public Socket connect(){
try {
s= new Socket( emerserver , portaserver) ;
outdrejtserver = new DataOutputStream ( s.getOutputStream());
inngaserver = new BufferedReader (new InputStreamReader ( s.getInputStream()));
}
catch (UnknownHostException ex ) {
System.err.println(" Hosti i panjohur ") ;
}
catch ( Exception ex )
{
System.out.println(ex.getMessage());
System.out.println("Gabim gjate lidhjes!");
System.exit(1) ;
}
return s;
}
private JPanel panel;
private MyPanel resultpanel;
private JPanel searchpanel;
private JPanel buttonpanel;
private JLabel label;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JTextField tf4;
private JTextField tf5;
private JTextField tf6;
private JTextField tf7;
private JTextField tf8;
private JPanel p1;
private JPanel p2;
private JPanel p3;
private JPanel p4;
private JButton buton;
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
private String s7;
private String s8;
private String [] string;
private int n;
private class WindowCloser extends WindowAdapter{
@Override
public void windowClosing(WindowEvent event){
System.exit(0);
}
}
}
class MyPanel extends JPanel{
public MyPanel(){
s="Rezultatet e kerkimit";
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.drawString(s, 10,20);
}
public void writestr(String str){
s = str;
repaint();
}
private String s;
}
THE SERVER
Code :
package javaapplication1;
/**
*
* @author Lori
*/
import java.net.*;
import java.io.*;
import java.sql.*;
class ServerThread extends Thread{
ServerSocket server=null;
Socket client=null;
BufferedReader inngaklient;
DataOutputStream outdrejtklient;
public ServerThread(Socket socket) {
this.client=socket;}
@Override
public void run(){
try{
communication();
System.out.println("Serveri filloi!");
}
catch(Exception e){
e.printStackTrace(System.out);
}
}
public void communication()throws Exception{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
System.out.println("Error!");
return;
}
inngaklient=new BufferedReader(new InputStreamReader(client.getInputStream()));
outdrejtklient=new DataOutputStream(client.getOutputStream());
for(;;){
String s1=inngaklient.readLine();
String s2=inngaklient.readLine();
String s3=inngaklient.readLine();
String s4=inngaklient.readLine();
String s5=inngaklient.readLine();
String s6=inngaklient.readLine();
String s7=inngaklient.readLine();
String s8=inngaklient.readLine();
try{
Connection c=DriverManager.getConnection("jdbc:odbc:lori", null, null);
Statement s=c.createStatement();
if(!s1.equals("")){
ResultSet r = s.executeQuery("Select * from dbo.libra where titulli=s1");
while(r.next()){
outdrejtklient.writeBytes(r.getString("titulli")+"\n ");
System.out.println("Serveri filloi"); }
r.close();
s.close();
c.close();
}
}
catch(SQLException se){
System.out.println("Sql exception:" +se.getMessage());
se.printStackTrace(System.out);
}
outdrejtklient.close();
inngaklient.close();
client.close(); }
}
}
public class Multiserver {
public void start(){
try{
ServerSocket servSocket=new ServerSocket(32007);
for(;;){
Socket socket=servSocket.accept();
ServerThread servThread=new ServerThread(socket);
servThread.start();
}
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println("Gabim gjate krijimit te instances se serverit!");
System.exit(1);
}
}
public static void main(String[]args){
Multiserver server=new Multiserver();
server.start();
}
}
Re: Problems with client/server application
The function JFrame.show is deprecated as of Java 1.5. Try changing it to setVisible(true) and see if this fixes the problem
Re: Problems with client/server application
Thanks for the advice, but the problem still persists. I think it might be sth with the client server part of the program
Re: Problems with client/server application
Its tough to determine the exact problem for some cases like this. My guess is that it does have something to do with the connection and/or communication, which might be timing out or a method like read() blocking while its waiting for input. I'd suggest you do some debugging either by using a debugger and stepping through the code to see if it blocking, or commenting out some code until it works, then incrementally uncomment to try and determine the underlying problem.