Java 如何通过大量数据传输优化服务器-客户端应用程序?
我很好奇如何优化基于套接字或服务器客户端(或任何其他体系结构)的应用程序。我是java新手。我正在试验基于S-C模型的代码,运行此代码时,我感觉机器上有负载。几天前,我记得戴尔技术支持公司曾经使用一些Java应用程序通过互联网诊断我的电脑。为了试试我的技能,在谷歌的帮助下,我编辑了这段代码,并尝试制作类似的应用程序。但我不知道为什么要花很多时间。无论如何,这是我的代码。任何帮助或建议都会很好Java 如何通过大量数据传输优化服务器-客户端应用程序?,java,image,sockets,client-server,serversocket,Java,Image,Sockets,Client Server,Serversocket,我很好奇如何优化基于套接字或服务器客户端(或任何其他体系结构)的应用程序。我是java新手。我正在试验基于S-C模型的代码,运行此代码时,我感觉机器上有负载。几天前,我记得戴尔技术支持公司曾经使用一些Java应用程序通过互联网诊断我的电脑。为了试试我的技能,在谷歌的帮助下,我编辑了这段代码,并尝试制作类似的应用程序。但我不知道为什么要花很多时间。无论如何,这是我的代码。任何帮助或建议都会很好 Client.Java import java.net.*; import java.io.*; i
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();
}
}
我知道客户机每次都在创建新实例
client = new Socket(serverName, port);
因此,在Client.java中,在执行每个循环时,最后有两个问题:如何在不打开新端口的情况下从一个端口发送数据?如何检查我的代码是否完全优化?(如哪个元素或哪个循环占用多少内存)
提前谢谢!!!!!
(抱歉有时我对知识的好奇心和兴奋度太高,以至于很难提问,因为我脑子里有很多东西。-)哈哈哈)k,我明白了(仍然怀疑)。
imagePanel在执行时占用了太多内存。
通过@MadProgrammer使用ImagePaneExample解决了这个问题。另外,我不认为我需要客户端的特定端口,它只会在客户端运行太多应用程序时增加失败的机会
无论如何,谢谢 您已经在发送数据,但尚未说明要优化的内容。你的问题不清楚。请澄清。@EJP我不知道为什么它消耗了太多的ram和CPU,以及客户端每个循环的执行。客户端打开一个新端口以发送下一个映像,是否有方法在不打开新端口的情况下将数据发送到服务器。