Java Port Socket Programming Error
- by atrus-darkstone
Hi-
I have been working on a java client-server program using port sockets. The goal of this program is for the client to take a screenshot of the machine it is running on, break the RGB info of this image down into integers and arrays, then send this info over to the server, where it is reconstructed into a new image file. However, when I run the program I am experiencing the following two bugs:
The first number recieved by the server, no matter what its value is according to the client, is always 49.
The client only sends(or the server only receives?) the first value, then the program hangs forever.
Any ideas as to why this is happening, and what I can do to fix it? The code for both client and server is below.
Thanks!
CLIENT:
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);
}
/* System.out.println(imageData.length);
String fromServer = null;
for(int i = 0; i < pixelData.length; i++) {
imageData[i] = ((byte)pixelData[i]);
}
System.out.println("CLIENT: Pixel data successfully converted to byte data.");
System.out.println("CLIENT: Waiting for ready message...");
if((fromServer = getInput().readLine()).equals("READY")) {
System.out.println("CLIENT: Ready message recieved.");
getOutput().println("SENDING ARRAY LENGTH...");
System.out.println("CLIENT: Sending array length...");
System.out.println("CLIENT: " + imageData.length);
getOutput().println(imageData.length);
System.out.println("CLIENT: Array length sent.");
getOutput().println("SENDING IMAGE...");
System.out.println("CLIENT: Sending image data...");
for(int i = 0; i < imageData.length; i++) {
getOutput().println(imageData[i]);
}
System.out.println("CLIENT: Image data sent.");
getOutput().println("SENDING IMAGE WIDTH...");
System.out.println("CLIENT: Sending image width...");
getOutput().println(width);
System.out.println("CLIENT: Image width sent.");
getOutput().println("SENDING IMAGE HEIGHT...");
System.out.println("CLIENT: Sending image height...");
getOutput().println(height);
System.out.println("CLIENT: Image height sent...");
getOutput().println("SENDING IMAGE INFO...");
System.out.println("CLIENT: Sending image info...");
getOutput().println(fileName);
System.out.println("CLIENT: Image info sent.");
getOutput().println("FINISHED.");
System.out.println("Image data sent successfully.");
}
if((fromServer = getInput().readLine()).equals("CLOSE DOWN")) {
getOutput().close();
getInput().close();
getVSocket().close();
} */
}
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;
/*IMPORTANT TODO:
* 1. CLOSE ALL STREAMS AND SOCKETS WITHIN CLIENT AND SERVER!
* 2. PLACE MAIN EXEC CODE IN A TIMED WHILE LOOP TO SEND FILE EVERY X SECONDS
* */
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();
}
}