I'm curious about how to optimize applications based on sockets or server-client(or any other architecture). I'm new to java. I'm experimenting with code based On S-C model, i felt load on my machine while running this code. A few days ago i remembered Dell technical support once diagnosed my PC through Internet using some Java app. Just to try my skills and with the help of google i edited this code and tried to make similar app. But i don't know why it is taking to much load. Anyway here is my code. Any help or suggestions will be great.
Client.Java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import javax.swing.ImageIcon;
public class Client
{
static BufferedImage bimg;
static String serverName = "localhost";
static int port = 6066;
static Socket client;
static DataInputStream in;
static DataOutputStream out ;
public static void main(String [] args) throws AWTException, UnknownHostException, IOException
{
while(true){
try
{
client = new Socket(serverName, port);
System.out.println("Connecting to " + serverName
+ " on port " + port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
in=new DataInputStream(client.getInputStream());
System.out.println(in.readUTF());
System.out.println(in.readUTF());
out =
new DataOutputStream(client.getOutputStream());
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
out.writeUTF("client: hello to server");
bimg = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(bimg,"JPG",client.getOutputStream());
System.out.println("Image sent!!!!");
client.close();
out.flush();
in.close();
out.close();
}catch(IOException e)
{
e.printStackTrace();
}}
}
}
Server.java
public class Server extends Thread {
private ServerSocket serverSocket;
Socket server;
JFrame VF;
static ImagePanel im;
static BufferedImage bp;
public Server(int port) throws IOException, SQLException, ClassNotFoundException, Exception {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(180000);
VF = new JFrame("Video output");
im = new ImagePanel();
im.setImage("screenshot.png");
im.setroundcorners(true, 10);
im.setImagePanelBounds(20, 20, 500, 500);
VF.pack();
VF.setLayout(new BorderLayout());
VF.add(im, BorderLayout.CENTER);
VF.setSize(800, 800);
VF.setLocation(10, 10);
VF.setVisible(true);
//startVideo();
VF.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent ce) {
im.setImagePanelBounds(0, 0, VF.getWidth(), VF.getHeight());
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentMoved(ComponentEvent ce) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentShown(ComponentEvent ce) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentHidden(ComponentEvent ce) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
//
}
public void run() {
while (true) {
try {
server = serverSocket.accept();
DataInputStream din = new DataInputStream(server.getInputStream());
DataOutputStream dout = new DataOutputStream(server.getOutputStream());
dout.writeUTF("server: -i am greeting server");
dout.writeUTF("server:- hi! hello client");
System.out.println(din.readUTF());
System.out.println(din.readUTF());
bp = ImageIO.read(ImageIO.createImageInputStream(server.getInputStream()));
System.out.println("Image received!!!!");
//lblimg.setIcon(img);
} catch (SocketTimeoutException st) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
} catch (Exception ex) {
System.out.println(ex);
}
//here in new
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
int i = 0;
{
im.setImage(bp);
im.repaint();
VF.repaint();
VF.revalidate();
}
}
//here is end
}
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, Exception {
//int port = Integer.parseInt(args[0]);
Thread t = new Server(6066);
t.start();
}
}
ImagePanel.java
class ImagePanel extends JPanel {
int g_h = 10, g_w = 10;
int width = 100;
int height = 100;
int cornerradius;
Image castle;
Dimension size;
protected int x1, y1;
Color c1 = new Color(255, 0, 0);
Rectangle rec;
boolean b = false;
boolean imboo = false;
boolean roundb = false;
boolean ovalb = false;
protected int strokeSize = 1;
protected Color shadowColor = Color.BLACK;
boolean shadowed = false;
public ImagePanel() {
//super();
setOpaque(false);
setLayout(null);
//System.out.println("it executed");
//this.paint(g);
}
public ImagePanel(int x, int y) {
this.height = this.getParent().getHeight();
setSize(x, y);
}
public void setSize(int x, int y) {
this.width = x;
this.height = y;
}
public int getheight() {
return height;
}
public int getwidth() {
return width;
}
public void setImagePanelBounds(int x, int y, int width, int height) {
x1 = x;
y1 = y;
this.width = width;
this.height = height;
//System.out.println("6it executed");
}
public void setroundcorners(boolean b, int i) {
roundb = b;
cornerradius = i;
////System.out.println("5it executed");
}
public void setoval(boolean b, int i) {
roundb = b;
cornerradius = i;
////System.out.println("5it executed");
}
public void setImage(BufferedImage bi) {
imboo = true;
size = new Dimension();
castle = (Image) bi;
size.width = castle.getWidth(null);
size.height = castle.getHeight(null);
setPreferredSize(size);
//System.out.println("4it executed");
}
public void setImage(String s) {
imboo = true;
size = new Dimension();
castle = new ImageIcon(s).getImage();
size.width = castle.getWidth(null);
size.height = castle.getHeight(null);
setPreferredSize(size);
//System.out.println("4it executed");
}
//public void addc(Component c){
// this.add(c);
public void paint(Graphics gh) { ////System.out.println("it executed p");
{
int x = this.getWidth();
int j = 20, a = 20;
//BufferedImage output = new BufferedImage(x, this.getheight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) gh.create();
// Graphics2D g2= output.createGraphics();
// super.paint(g);// why it was here ???
{
g2.setColor(Color.WHITE);
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setComposite(AlphaComposite.SrcAtop);
rec = new Rectangle(x1, y1, width, height);
//Start of If-else
if (roundb) {
g2.setClip(new RoundRectangle2D.Float((int) rec.getX(), (int) rec.getY(), (int) rec.getWidth(), (int) rec.getHeight(), cornerradius, cornerradius));
//System.out.println("it executed");
}
if (ovalb) {
g2.setClip(new Ellipse2D.Float((int) rec.getX(), (int) rec.getY(), cornerradius, cornerradius));
//System.out.println("it executed");
}
// End of If-Else
// Image condition Starts
if (imboo) {
g2.drawImage(castle, (int) rec.getX(), (int) rec.getY(), (int) rec.getWidth(), (int) rec.getHeight(), null);
//g.drawImage(castle, (int)rec.getX(),(int)rec.getY(), null);
}
// Image condition Ends
g2.setColor(Color.BLUE);
}
g2.dispose();
}
}
public int getImageHeight() {
return (int) rec.getHeight();
}
public int getImageWidth() {
return (int) rec.getWidth();
}
}
I know a little bit that client is creating new instance each time in line.
client = new Socket(serverName, port);
So in the end two questions, In Client.java with the execution of each loop how to send data from one port without opening new port? How to check that my code is fully optimized? (Like which element or which loop costs how much memory) Thanks in Advance !!!!! (Apologies sometimes my curiosity & excitement for knowledge is too high that it becomes difficult to ask questions bcz a lot of things are going up in my mind. :-) LOLz)