Skip to main content

server not receiving the image sent by client side

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
brother
Offline
Joined: 2006-05-05

Hello Java lovers !
Firstly forgive my ignorance. I am new to java network programming . I have made a client and server in java. Client simply send and "jpg" image file to server but server side never receives it although client gets successfully connected to server.. Below is the code for my client and server program.

Client code :
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.imageio.ImageIO;

public class clientImageSender {
public static void main(String[] args) {
Socket sock=null;
BufferedImage image=null;
String serverMessage;
InputStreamReader isReader;
BufferedReader bReader;
try{
sock=new Socket("127.0.0.1",4444);

image=ImageIO.read(new File("clientImage.jpg"));
ImageIO.write(image, "jpg", sock.getOutputStream());
System.out.println("Image sent");

isReader=new InputStreamReader(sock.getInputStream());// gets acknoledgement by server when image is received on server's side
bReader = new BufferedReader(isReader);
serverMessage=bReader.readLine();
System.out.println(serverMessage);

System.out.println("This code is running");

}//try block
catch(Exception e){
e.printStackTrace();
}

}
}

Server code :

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;

public class ServerImageReceiver {
public static void main(String[] args) {

try
{
ServerSocket serverSocket=new ServerSocket(4444);
System.out.println("server is running");
BufferedImage image;

System.out.println("Server is waiting for connection");
while(true)
{
Socket sock=serverSocket.accept();
System.out.println("Connected to client mechine");
image=ImageIO.read(sock.getInputStream());
ImageIO.write(image, "jpg",new File("newImage.jpg"));
System.out.println("Image saved");
PrintWriter sendMe=new PrintWriter(sock.getOutputStream());
sendMe.println("File Received:Message by server");
sendMe.flush();
System.out.println("Server side message sent");

}
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

OUTPUTS :
CLIENT SIDE OUTPUT: Image sent

SERVER SIDE OUTPUT :
server is running
Server is waiting for connection
Connected to client mechine

I don't know where the problem lies. Please help