You are on page 1of 5

BAI THC HANH LP TRINH MNG

BAI S 1
Xy dng chng trinh theo m thi client/server d thc hin cng vic sau:
Client gui biu thuc ln Server, Server thc hin tinh toan va tra v client.
Hong dn:
//Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
public class server extends Thread
public static void ConnectionDatabase(String data) throws SQLException,
ClassNotFoundException
}
public static void main(String|| args) throws Exception
ServerSocket serverSocket new ServerSocket(7777);
System.out.print("Server is start............");
Socket socket serverSocket.accept();
DataInputStream inputStream new DataInputStream(socket.getInputStream());
DataOutputStream outputStream new DataOutputStream(socket.getOutputStream());
while (true)
String snhan inputStream.readUTF();
String straxulychuoi(snhan);
outputStream.writeUTF(stra);

}
}
public static String xulychuoi(String s)

Iloat ketqua0;
int i0;
Iloat giatritam0;
while(is.length())

while ((s.charAt(i)!'')&&((s.charAt(i)!'-'))&&(s.charAt(i)!
'*')&&(s.charAt(i)!'/'))

ketqua ketqua*10 Integer.parseInt(String.valueOI(s.charAt(i)));


i;
}
iI(s.charAt(i)'')

i;
while ((is.length()&&(s.charAt(i)!'')&&((s.charAt(i)!
'-'))&&(s.charAt(i)!'*')&&(s.charAt(i)!'/')))

giatritam giatritam*10
Integer.parseInt(String.valueOI(s.charAt(i)));
i;
}
ketquagiatritam;
giatritam0;
}
iI((is.length())&&(s.charAt(i)'-'))

i;
while ((is.length()&&(s.charAt(i)!'')&&((s.charAt(i)!
'-'))&&(s.charAt(i)!'*')&&(s.charAt(i)!'/')))

giatritam giatritam*10
Integer.parseInt(String.valueOI(s.charAt(i)));
i;
}
ketqua-giatritam;
giatritam0;
}
iI((is.length())&&(s.charAt(i)'*'))

i;
while ((is.length()&&(s.charAt(i)!'')&&((s.charAt(i)!
'-'))&&(s.charAt(i)!'*')&&(s.charAt(i)!'/')))

giatritam giatritam*10
Integer.parseInt(String.valueOI(s.charAt(i)));
i;
}
ketqua*giatritam;
giatritam0;
}
iI((is.length())&&(s.charAt(i)'/'))

i;
while ((is.length()&&(s.charAt(i)!'')&&((s.charAt(i)!
'-'))&&(s.charAt(i)!'*')&&(s.charAt(i)!'/')))

giatritam giatritam*10
Integer.parseInt(String.valueOI(s.charAt(i)));
i;
}
ketqua/giatritam;
giatritam0;
}
}
System.out.print(ketqua);
return String.valueOI(ketqua);
}

}
//Client
import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class client implements ActionListener
public static Panel pn1, pn2,pn3, pn;
public static TextField tInhap new TextField(5);
public static TextField tIkq new TextField();
public static Label lbnhap new Label();
public static Label lbkq new Label();
public static Button bt1,bt2;
public static String s"";
Socket socket ;
DataOutputStream outputStream;
DataInputStream inputStream;

public client () throws UnknownHostException, IOException

GUI();
}
void GUI() throws UnknownHostException, IOException
JFrame Ir new JFrame("Client");
Ir.setSize(400, 200);
Ir.setLayout(new GridLayout());
lbnhap.setText("Nhap vao chuoi can tinh toan");
lbkq.setText("Ket qua");
pnnew Panel(new GridLayout(5,1));
pn1new Panel(new GridBagLayout());
bt1new Button("send");
bt2new Button("Exit");
pn.add(lbnhap);
pn.add(tInhap);
pn.add(lbkq);
pn.add(tIkq);
pn1.add(bt1);
pn1.add(bt2);
pn.add(pn1);
Ir.add(pn);
bt1.addActionListener(this);
bt2.addActionListener(this);
Ir.show();
socket new Socket("127.0.0.1", 7777);;
outputStream new DataOutputStream(socket.getOutputStream());
inputStream new DataInputStream(socket.getInputStream());
}
public static void main(String|| args) throws Exception
client t new client();

}
Override
public void actionPerIormed(ActionEvent e)
// TODO Auto-generated method stub
iI(e.getSource()bt1)

try
String s tInhap.getText();
outputStream.writeUTF(s);
String nhan inputStream.readUTF();
tIkq.setText(nhan);
} catch (UnknownHostException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
iI(e.getSource()bt2)
System.exit(0);
}
}