Java端口套接字编程错误

时间:2010-04-28 20:53:41

标签: java networking sockets

我一直在使用端口套接字处理java客户端服务器程序。该程序的目标是让客户端截取其运行的机器的屏幕截图,将此图像的RGB信息分解为整数和数组,然后将此信息发送到服务器,在那里将其重建为新的图像文件。我用另外两个小类运行这些文件,它们实例化这两个文件的对象并执行它们的run()方法。但是,当我运行该程序时,我遇到以下两个错误:

  1. 服务器收到的第一个号码,无论客户端的值是多少,都是49.
  2. 客户端只发送(或服务器只接收?)第一个值,然后程序永远挂起。
  3. 关于为什么会发生这种情况的任何想法,以及我可以采取哪些措施来解决这个问题?客户端和服务器的代码如下所示。

    谢谢!

    客户端:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;
    
    
    public class ViewerClient implements ActionListener{
    
     private Socket vSocket;
     private BufferedReader in;
     private PrintWriter out;
     private Robot robot; 
    // static BufferedReader orders = null;
    
     public ViewerClient() throws Exception{
      vSocket = null;
      in = null;
      out = null;
      robot = null;  
     }
    
     public void setVSocket(Socket vs) {
      vSocket = vs;
     }
    
     public void setInput(BufferedReader i) {
      in = i;
     }
    
     public void setOutput(PrintWriter o) {
      out = o;
     }
    
     public void setRobot(Robot r) {
      robot = r;
     }
    
     /*************************************************/
    
     public Socket getVSocket() {
      return vSocket;
     }
    
     public BufferedReader getInput() {
      return in;
     }
    
     public PrintWriter getOutput() {
      return out;
     }
    
     public Robot getRobot() {
      return robot;
     }
    
    
    
     public void run() throws Exception{
      int speed = 2500;
      int pause = 5000;
      Timer timer = new Timer(speed, this);
      timer.setInitialDelay(pause);
    //  System.out.println("CLIENT: Set up timer.");
      try {
       setVSocket(new Socket("Alex-PC", 4444));
       setInput(new BufferedReader(new InputStreamReader(getVSocket().getInputStream())));
       setOutput(new PrintWriter(getVSocket().getOutputStream(), true));
       setRobot(new Robot());
    //   System.out.println("CLIENT: Established connection and IO ports.");
    //   timer.start();
       captureScreen(nameImage());
      }catch(Exception e) {
       System.err.println(e);   
      }
    
     }
    
     public void captureScreen(String fileName) throws Exception{
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle screenRectangle = new Rectangle(screenSize);  
      BufferedImage image = getRobot().createScreenCapture(screenRectangle);
      int width = image.getWidth();
      int height = image.getHeight();
      int[] pixelData = new int[(width * height)];
      image.getRGB(0,0, width, height, pixelData, width, height);
      byte[] imageData = new byte[(width * height)];
      String fromServer = null;
    
      if((fromServer = getInput().readLine()).equals("READY")) {
       sendWidth(width);
       sendHeight(height);
       sendArrayLength((width * height));
       sendImageInfo(fileName);
       sendImageData(imageData);  
    
      }      
    
    
     }
    
     public String nameImage() throws Exception {
      String dateFormat = "yyyy-MM-dd HH-mm-ss";
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
      String fileName = sdf.format(cal.getTime());
      return fileName;
     }
    
     public void sendArrayLength(int length) throws Exception {
      getOutput().println("SENDING ARRAY LENGTH...");
      getOutput().println(length);
     }
    
     public void sendWidth(int width) throws Exception {
      getOutput().println("SENDING IMAGE WIDTH...");
      getOutput().println(width);
     }
    
     public void sendHeight(int height) throws Exception {
      getOutput().println("SENDING IMAGE HEIGHT...");
      getOutput().println(height);
     }
    
     public void sendImageData(byte[] imageData) throws Exception {
      getOutput().println("SENDING IMAGE...");  
      for(int i = 0; i < imageData.length; i++) {
       getOutput().println(imageData[i]);
      }
     }
    
     public void sendImageInfo(String info) throws Exception {
      getOutput().println("SENDING IMAGE INFO...");  
      getOutput().println(info);
     }
    
    
     public void actionPerformed(ActionEvent a){
      String message = null;
      try {
       if((message = getInput().readLine()).equals("PROCESSING...")) {
        if((message = getInput().readLine()).equals("IMAGE RECIEVED SUCCESSFULLY.")) {
         captureScreen(nameImage());
        }
       }
      }catch(Exception e) {
       JOptionPane.showMessageDialog(null, "Problem: " + e);
      }
    
     }
    
    }
    

    SERVER:

    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;  
    
    
    public class ViewerServer {
    
     private ServerSocket vServer;
     private Socket vClient;
     private PrintWriter out;
     private BufferedReader in;
     private byte[] imageData;
     private int width;
     private int height;
     private String imageInfo; 
     private int[] rgbData;
     private boolean active;
    
     public ViewerServer() throws Exception{
      vServer = null;
      vClient = null;
      out = null;
      in = null;
      imageData = null;
      width = 0;
      height = 0;
      imageInfo = null;
      rgbData = null;
      active = true;
     }
    
     public void setVServer(ServerSocket vs) {
      vServer = vs;
     }
    
     public void setVClient(Socket vc) {
      vClient = vc;
     }
    
     public void setOutput(PrintWriter o) {
      out = o;
     }
    
     public void setInput(BufferedReader i) {
      in = i;
     }
    
     public void setImageData(byte[] imDat) {
      imageData = imDat;
     }
    
     public void setWidth(int w) {
      width = w;
     }
    
     public void setHeight(int h) {
      height = h;
     }
    
     public void setImageInfo(String im) {
      imageInfo = im;
     }
    
     public void setRGBData(int[] rd) {
      rgbData = rd;
     }
    
     public void setActive(boolean a) {
      active = a;
     }
    
     /***********************************************/
    
    
     public ServerSocket getVServer() {
      return vServer;
     }
    
     public Socket getVClient() {
      return vClient;
     }
    
     public PrintWriter getOutput() {
      return out;
     }
    
     public BufferedReader getInput() {
      return in;
     }
    
     public byte[] getImageData() {
      return imageData;
     }
    
     public int getWidth() {
      return width;
     }
    
     public int getHeight() {
      return height;
     }
    
     public String getImageInfo() {
      return imageInfo;
     }
    
     public int[] getRGBData() {
      return rgbData;
     }
    
     public boolean getActive() {
      return active;
     }
    
    
     public void run() throws Exception{
      connect();
      setActive(true);  
      while(getActive()) {
       recieve();
      }  
      close();
     }
    
    
     public void recieve() throws Exception{  
      String clientStatus = null;
      int clientData = 0;
    
    //  System.out.println("SERVER: Sending ready message...");
      getOutput().println("READY");
    //  System.out.println("SERVER: Ready message sent.");
      if((clientStatus = getInput().readLine()).equals("SENDING IMAGE WIDTH...")) {
       setWidth(getInput().read());
       System.out.println("Width: " + getWidth());
      }
      if((clientStatus = getInput().readLine()).equals("SENDING IMAGE HEIGHT...")) {
       setHeight(getInput().read());
       System.out.println("Height: " + getHeight());
      }
      if((clientStatus = getInput().readLine()).equals("SENDING ARRAY LENGTH...")) {
       clientData = getInput().read();   
       setImageData(new byte[clientData]);   
       System.out.println("Array length: " + clientData);
      }
      if((clientStatus = getInput().readLine()).equals("SENDING IMAGE INFO...")) {
       setImageInfo(getInput().readLine());
       System.out.println("Image Info: " + getImageInfo());
      }
      if((clientStatus = getInput().readLine()).equals("SENDING IMAGE...")) {
       for(int i = 0; i < getImageData().length; i++) {
        getImageData()[i] = ((byte)getInput().read());
       }
      }
    
      if((clientStatus = getInput().readLine()).equals("FINISHED.")) {
       getOutput().println("PROCESSING...");
       setRGBData(new int[getImageData().length]);
       for(int i = 0; i < getRGBData().length; i++) {
        getRGBData()[i] = getImageData()[i];
       }
       BufferedImage image = null;
       image.setRGB(0, 0, getWidth(), getHeight(), getRGBData(), getWidth(), getHeight());
       ImageIO.write(image, "png", new File(imageInfo + ".png")); //create an image file out of the screenshot
    
       getOutput().println("IMAGE RECIEVED SUCCESSFULLY.");
      }
     }
    
     public void connect() throws Exception  {
      setVServer(new ServerSocket(4444)); //establish server connection
    //  System.out.println("SERVER: Connection established.");
      setVClient(getVServer().accept());  //accept client connection request
    //  System.out.println("SERVER: Accepted connection request.");
      setOutput(new PrintWriter(vClient.getOutputStream(), true));  //set up an output channel
      setInput(new BufferedReader(new InputStreamReader(vClient.getInputStream())));  //set up an input channel
    //  System.out.println("SERVER: Created IO ports.");
     }
    
     public void close() throws Exception {
      getOutput().close();
      getInput().close();
      getVClient().close();
      getVServer().close();
     }
    }
    

1 个答案:

答案 0 :(得分:0)

您没有向我们展示您如何运行客户端或服务器。看来你没有使用socket io

所需的线程

你有很多错误,我甚至不知道从哪里开始。

你在read()中调用BufferedReader它返回一个整数,但它的整数范围仅为0-255。是的我知道它是一个糟糕的界面,但你只读了一个字节,而不是整个宽度和高度。

我的建议是分解您的程序并确保您了解如何首先发送单个字节。编写一个使用套接字发送Hello World的程序。鉴于你所展示的代码,我认为你甚至不能这样做。