How to receive a file from a socket and not its content? - java

I have a server that returns a file photo.png for a client, the server is running correctly considering the client as a browser when i introduce this link localhost:5555/photo.png i dont get the picture i get this:
\89PNG
\00\00\00
IHDR\00\00\00\00\00\00\00\91\F9\00\00\00gAMA\00\00\B1\8F\FCa\00\00\00sRGB\00\AE\CE\E9\00\00\00PLTELiq\EA\F2\F7\F2\F4\F8\FB\EF\EF\E9\F1\F7\EA\F2\F8\EC\F3\F8\FC\EC\EB\FB\ED\EE\E4\EF\F7\FB\EC\EC\FB\ED\ED\FB\ED\ED\ED\F4\F9\FB\EF\EF\FD\ED\ED\EB\EB\EB\FB\EB\EB\F9\EE\EF\FA\EB\EB\EE\F4\F8\F2\F2\F2\FB\E9\E9\E9\F1\F8\EA\F1\F8\EC\F3\F8\FB\E9\EA\E6\F0\F6\E7\F1\F7\FC\EF\EF\EA\F1\F6\EF\EF\EF\ED\F3\F7\ED\F3\F8\E7\F0\F7\FA\EE\EE\FA\E9\E9\EA\F0\F8\ED\F2\F9\EA\F3\F9\F3\F2\F4\FC\EC\EB\E1"o\B6\E0"\E1!\E0!\E1! n\B5\E0 \E0k\B4\00f\B2n\B6\E0m\B5 n\B6\00g\B2\E0\E0\DFj\B3\00a\AF\DF\00e\B1l\B4\00i\B3\DF\E0!l\B5\00h\B2\DF\00`\AE m\B5\E0\E1"\DF\DF\00c\B0\E47:\DF
\E2'*\E0\E5:<\DE \DE\DF\00d\B0\00_\AE\F2\9D\9F\DE
y\BB\DF ........
This is the code or text behind the image photo.png but what it should displayed is:
photo.ong
The same happens when i want a PDF file i get the code and not the file...
import java.io.*;
import java.net.*;
public class Server {
public static void main (String[] args){
Socket socket;
ServerSocket serverSocket;
BufferedReader in = null;
PrintWriter out = null;
BufferedInputStream bis;
BufferedOutputStream bos;
try{
serverSocket=new ServerSocket(5555);
socket = serverSocket.accept();
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input, output;
input = in.readLine(); //(1)
out.println("Server: Connected. Input from Client:"+input); //(2)
input = in.readLine(); //(1)
out.println("Server: I am ready to recieve file. Input from Client:"+input); //(2)
bis = new BufferedInputStream(socket.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("photo.png"));
int length = Integer.parseInt(input);
int i=0;
int IN=0;
byte[] receivedData = new byte[1000];
while ((IN = bis.read(receivedData)) != length){ //in = int; receivedData = byte[]
bos.write(receivedData,0,IN);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
This server was implemented by http://www.coderanch.com

I think you're trying to perform File IO operations on an image. See the following:
How to make ImageIO read from InputStream :Java

Related

Problem with client-server application on java

Good day, I'm a beginner developer and I'm trying to write a client-server application. My application should work like this, it checks if the file has been modified and if so, it is sent to the server, the question is how to organize this?
I did it through the socket, but if I get a deadlock, I will be grateful for any comments, I attach the code further
Server
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket;
Socket client;
BufferedInputStream clientInputStream;
ByteArrayOutputStream byteArrayOutputStream;
StringBuilder fileName;
byte[] mass;
{
fileName = new StringBuilder("this.txt"); // give name for file
serverSocket = new ServerSocket(5000); // open server in 5000 port
mass = new byte[1]; // create new buffer array
client = serverSocket.accept(); // waiting connect
clientInputStream = new BufferedInputStream(client.getInputStream()); // to accept byte's array
byteArrayOutputStream = new ByteArrayOutputStream(); // to write byte array in file
}
{
BufferedOutputStream bufferedOutputStream;
int bytesRead;
// FIXME: 02.07.2022 первый файл
do {
byteArrayOutputStream.write(mass, 0, mass.length); // write by one byte in array
bytesRead = clientInputStream.read(mass, 0, mass.length);
} while (bytesRead != -1);
FileOutputStream writer = new FileOutputStream(fileName.toString()); // stream for file write
bufferedOutputStream = new BufferedOutputStream(writer);
bufferedOutputStream.write(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.toByteArray().length); // write file
System.out.println("first f");
bufferedOutputStream.flush();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
bufferedWriter.write("success"); // if file send success then write string in socket and send to client
}
{
serverSocket.close();
client.close();
clientInputStream.close();
byteArrayOutputStream.close();
}
}
}
And Client
import java.io.*;
import java.net.Socket;
public class Client {
private final static String serverIP = "192.168.0.47"; // server ip address
private final static int serverPort = 5000; // server port address
private final static String fileOutput = "first.txt"; // first file to send
private final static String fileOutput1 = "second.txt"; // second file to send
public static void main(String[] args) throws IOException, InterruptedException{
Socket client = new Socket(serverIP,serverPort); // make new connect with my server
BufferedInputStream inputFile = new BufferedInputStream(new FileInputStream(fileOutput)); // convert file to array bytes
// FIXME: 02.07.2022 first file
BufferedOutputStream clientSocketOutputWriter = new BufferedOutputStream(client.getOutputStream()); // this is the stream to send to the socket
byte[] massByte = inputFile.readAllBytes(); // this going on convert file to array bytes
clientSocketOutputWriter.write(massByte,0, massByte.length); // send array bytes to server
System.out.println("first file send");
//----------------if i remove this two string, program begin work successfully but i can't send several file because this beging deadlock
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
// String s = bufferedReader.readLine();
//----------------
System.out.println("the end");
clientSocketOutputWriter.close();
client.close();
inputFile.close();
}
}
If the TCP connection isn't closed,The variable bytesRead will never be -1 because the code clientInputStream.read(mass, 0, mass.length); keeps executing and waiting for data.If you want to send a file,you should send its length first then you can call byte[] data=clientInputStream.readNBytes(its length),or put its data into a object and use ObjectInputStream and ObjectOutputStream to send the object.
The variable bytesRead isn't -1 means the server cannot leave the loop,so the server cannot put the data which it has just received into a file.Just use writer.write(mass); instead of byteArrayOutputStream.write(mass, 0, mass.length);,write the data to the file directly then stop running the program can solve the problem too.

Display or download image through the browser

I have a server that displays what the user asks for from the browser, I am using linux and when i run the server and ask for a file like Image.png using this link localhost:9999/Image.png on FireFox i get this message:
The image "localhost:9999/Image.png" cannot be displayed because it
contains errors.
But when i change the variable fileName to an HTML file it works perfectly and i can visualize the html page.
What am I doing wrong??
This is my server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String args[]) throws IOException {
// Declarem les variables a utilitzar
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inS = null;
OutputStream outS = null;
try
{
serverSocket = new ServerSocket(9999);
while(true)
{
socket= serverSocket.accept();
inS = socket.getInputStream();
outS = socket.getOutputStream();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(inS));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outS));
System.out.println("THis is what the user wants = " + br.readLine());
String fileName = "Image.png";
String extension= "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i+1);
}
String dataReturn = "";
if(extension.equals("png"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: image/png\r\n");
bw.write("\r\n");
FileReader myFilepng = new FileReader(fileName);
Scanner scanner1 = new Scanner(myFilepng);
dataReturn = "";
while(scanner1.hasNextLine()) {
dataReturn = scanner1.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner1.close();
}else{
if(extension.equals("html"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: text/html\r\n");
bw.write("\r\n");
bw.write("<TITLE>"+fileName+"/TITLE>");
FileReader myFile = new FileReader(fileName);
Scanner scanner = new Scanner(myFile);
dataReturn = "";
while(scanner.hasNextLine()) {
dataReturn = scanner.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner.close();
}
}
bw.close();
}catch(Exception e)
{
}
}
}catch (IOException e) {
System.out.println(e);
}
inS.close();
outS.close();
socket.close();
}
}
You are not writing the contents of your png file to your bw BufferedWriter. Instead you are only sending the header of the response to the client. As you are indicating your response is a png image and there is no data, your browser is telling you the image contains errors (in fact, it does not contains nothing at all).
Open the png filename, write the data to your "bw" buffer to send it to the client. That should be enough.
Edit:
To to that, try the following code for your "if" is image:
if(extension.equals("png"))
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
DataOutputStream binaryOut = new DataOutputStream(outS);
binaryOut.writeBytes("HTTP/1.0 200 OK\r\n");
binaryOut.writeBytes("Content-Type: image/png\r\n");
binaryOut.writeBytes("Content-Length: " + data.length);
binaryOut.writeBytes("\r\n\r\n");
binaryOut.write(data);
binaryOut.close();
}
Note the use of a binary stream in comparison to the text stream you use in case of html.

TCP Server and Client java

I'm new at Java and have been having some issues while passing a variable from one class to another main class.
A little about the program -
I have one main class called "Server.java" and another main class called "Client.java"
This is a simple TCP Server-client program written in java. The server class is executed first so it can accept connection from the client, which is executed second.
Once the client is connected to the server, the client specifies the name of the file it wishes to receive from the server by typing, for instance, "alice.txt" and then the server sends the file with that name in it's directory to the client.
Where I'm stuck -
I'm only able to receive file on the client side if I hard code the name of the file first in the server (check the code below). I wish to take the file name from the client side and pass to the Server class so the code works for all the files and not just one, which was hard coded.
Any help is appreciated :)
Server.java
import java.io.*;
import java.net.*;
class Server
{
public static void main(String argv[]) throws Exception
{
//beginning of the try method
try
{
//create a new serversocket object with port no 6789
ServerSocket welcomeSocket = new ServerSocket(6789);
//while loop
while(true)
{
//create a new socket object and accept the connection and it waits for any connection from client
Socket connectionSocket = welcomeSocket.accept();
//display confirmation to the user
System.out.println("Connection accepted!");
System.out.println("File request recevied!");
//specify the file the server wants to send
File myFile = new File("alice.txt");
//THIS IS WHERE THE FILE FROM THE CLIENT IS HARD-CODED. I AM TRYING TO REPLACE THE FILE NAME WITH A VARIABLE THAT WAS PASSED FROM THE CLIENT SIDE
//get the byte array length of the file
byte [] bArray = new byte [(int)myFile.length()];
//open a new file object
FileInputStream f = new FileInputStream(myFile);
//new buffered input stream object
BufferedInputStream bs = new BufferedInputStream(f);
//read function of the inputput stream
bs.read(bArray, 0, bArray.length);
//declare new output strea object
OutputStream os = connectionSocket.getOutputStream();
//display messages to the users
System.out.println("Okay, sending the file now.");
//write the file
os.write(bArray, 0, bArray.length);
//flush the file
os.flush();
//close the connection
connectionSocket.close();
//display confirmation message to the user
System.out.println("File was successfully sent!");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
public static void main(String argv[]) throws Exception
{
try
{
//declare scanner object
Scanner s = new Scanner(System.in);
//display a message to the user
System.out.println("Enter the file name you wish to request");
//read the user input
String textFileName = s.nextLine();
//declare a new Socket object and specify the host name and the port number
Socket clientSocket = new Socket("localhost", 6789);
//make a byte array in which the transmitted file will be broken down into and sent
byte [] bArray = new byte[10000000];
//create new inputstream object and set it to the input stream from the client
InputStream is = clientSocket.getInputStream();
//open new fileinput object
FileOutputStream fos = new FileOutputStream(textFileName);
//get the value from the fileoutputstream to bufferedoutput stream
BufferedOutputStream bos = new BufferedOutputStream(fos);
//read function of the inputsteam object
int readFile = is.read(bArray,0,bArray.length);
//assign readfile to endile
int endFile = readFile;
do
{
readFile = is.read(bArray, endFile, (bArray.length-endFile));
if(readFile >= 0)
{
endFile = endFile + readFile;
}
}while(readFile > -1);
//write file
bos.write(bArray, 0, endFile);
//show the message to the user
System.out.println("File " + textFileName + " was successfully received!");
//flush the file
bos.flush();
//close the file
bos.close();
//close the socket
clientSocket.close();
///
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
How I would do :
PS : this has not been tested but the principle is here..
Server.java
import java.io.*;
import java.net.*;
class Server
{
public static void main(String argv[]) throws Exception
{
//beginning of the try method
try
{
//create a new serversocket object with port no 6789
ServerSocket welcomeSocket = new ServerSocket(6789);
//while loop
while(true)
{
//create a new socket object and accept the connection and it waits for any connection from client
Socket connectionSocket = welcomeSocket.accept();// TODO - Make a new thread after the connection is accepted
//display confirmation to the user
System.out.println("Connection accepted!");
// Recover the fileName from client
String fileName = "";
InputStream iS = connectionSocket.getInputStream();
InputStreamReader iSR = new InputStreamReader(iS);
BufferedReader bR = new BufferedReader(iSR);
fileName = bR.readLine();
System.out.println("File request received : " + fileName);
// Recover the file's byte array
File myFile = new File(fileName);
byte[] bArray = new byte[(int)myFile.length()];
FileInputStream f = new FileInputStream(myFile);
BufferedInputStream bs = new BufferedInputStream(f);
bs.read(bArray, 0, bArray.length);
//display messages to the users
System.out.println("Okay, sending the file now.");
//declare new output strea object
OutputStream os = connectionSocket.getOutputStream();
os.write(bArray, 0, bArray.length);
os.flush();
//close the connection
connectionSocket.close();
//display confirmation message to the user
System.out.println("File was successfully sent!");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
public static void main(String argv[]) throws Exception
{
try
{
//declare scanner object
Scanner s = new Scanner(System.in);
//display a message to the user
System.out.println("Enter the file name you wish to request");
//read the user input
String textFileName = s.nextLine();
//declare a new Socket object and specify the host name and the port number
Socket clientSocket = new Socket("localhost", 6789);
// Send the filename via the connection
OutputStream oS = clientSocket.getOutputStream();
BufferedWriter bW = new BufferedWriter(new OutputStreamWriter(oS));
bW.write(textFileName);
//make a byte array in which the transmitted file will be broken down into and sent
byte[] bArray = new byte[10000000];
//create new inputstream object and set it to the input stream from the client
InputStream is = clientSocket.getInputStream();
//open new fileinput object
FileOutputStream fos = new FileOutputStream(split[1]);
//get the value from the fileoutputstream to bufferedoutput stream
BufferedOutputStream bos = new BufferedOutputStream(fos);
//read function of the inputsteam object
int readFile = is.read(bArray,0,bArray.length);
//assign readfile to endile
int endFile = readFile;
do
{
readFile = is.read(bArray, endFile, (bArray.length-endFile));
if(readFile >= 0)
{
endFile = endFile + readFile;
}
}while(readFile > -1);
//write file
bos.write(bArray, 0, endFile);
//show the message to the user
System.out.println("File " + textFileName + " was successfully received!");
//flush the file
bos.flush();
//close the file
bos.close();
//close the socket
clientSocket.close();
///
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
As per your code, once connection established, client expecting something from server using InputStream, and server sending data to client using OutputStream. but you have to do in this way, once connection established, in client code, write filename to server using OutputStream and then read data from server, similarly, in server side first read something from client and then write something to client as you did.

Sending file from one client to another client using socket in java

Hello everyone ,
I am trying to develop the application for transfering/sending the file like SKYPE works.So I am using socket for transfering file from one computer(client) to another computer(client) .I am able to transfer file from one client to server using this. code.But when I try to send the same file from server to second client.It is transfering with 0 byte also give socket close exception so I try to create new socket object at client side.So Now the Exception not coming but file not transfering to client.After debugging I found that the file is successfully sent to client by server but at client side socket is not able to read the data and waiting for data.I can’t find any better solution.If anyone knows anything about this Please tell me.If you have any other solution for file transfer than also tell me.Thanks in advance
Below is my code
Server code:
public class ChatServer
{
serversocket = new ServerSocket(1436);
thread = new Thread(this);
thread.start();
/*************Thread Implementation***************/
public void run()
{
/*********Accepting all the client connections and create a seperate thread******/
while(thread != null)
{
try
{
/********Accepting the Server Connections***********/
socket = serversocket.accept();
/******* Create a Seperate Thread for that each client**************/
chatcommunication = new ChatCommunication(this,socket);
thread.sleep(THREAD_SLEEP_TIME);
}
catch(InterruptedException _INExc) { ExitServer(); }
catch(IOException _IOExc) { ExitServer(); }
}
}
protected void SendGroupFile(Socket ClientSocket, String FileName,String GroupName,String UserName) throws IOException
{
try
{
// receive file from Client
byte [] mybytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(Filepath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =is.read(mybytearray, current, (mybytearray.length-current));
System.out.println("Reading Bytes server"+bytesRead);
if(bytesRead >= 0)
current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray,0,current);
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/***** Function To Send a File to Client **********/
protected void SendGroupFileClient(Socket ClientSocket, String FileName,String GroupName,String UserName)
{
try {
int m_userListSize = userarraylist.size();
clientobject = GetClientObject(GroupName);
if(clientobject != null)
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
clientobject = (ClientObject) userarraylist.get(G_ILoop);
if((clientobject.getGroupName().equals(GroupName)) && (!(clientobject.getUserName().equals(UserName))))
{
{
File myFile = new File (Filepath);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = socket.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
os.close();
}
}catch(IOException _IOExc)
{
_IOExc.printStackTrace();
}
}
}
ChatCommunication .java
public class ChatCommunication implements Runnable,CommonSettings
{
Thread thread;
Socket socket;
DataInputStream inputstream;
String RFC;
ChatServer Parent;
/********Initialize the Socket to the Client***********/
ChatCommunication(ChatServer chatserver,Socket clientsocket)
{
Parent = chatserver;
socket = clientsocket;
try
{
inputstream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}catch(IOException _IOExc) { }
thread = new Thread(this);
thread.start();
}
public void run()
{
while(thread != null)
{
try {
RFC = inputstream.readLine();
if(RFC.startsWith("FILEGRUP"))
{
Parent.SendGroupFile(socket,RFC.substring(9,RFC.indexOf("!")),RFC.substring(RFC.indexOf("!")+1,RFC.indexOf("*")),RFC.substring(RFC.indexOf("*")+1));
}
if(RFC.startsWith("FILEGET"))
{
Parent.SendGroupFileClient(socket,RFC.substring(8,RFC.indexOf("!")),RFC.substring(RFC.indexOf("!")+1,RFC.indexOf("*")),RFC.substring(RFC.indexOf("*")+1));
}
}catch(Exception _Exc)
{
Parent.RemoveUserWhenException(socket);QuitConnection();
}
}
}
Client code
class Client extends JFrame
{
ServerName="192.168.1.103";
ServerPort=1436;
Client()
{
socket = new Socket(ServerName,ServerPort);
SendGroupFileToServer(Filepath,SelectedGroup);
}
/*******Function To Send File To Server and receiving the file ***********/
protected void SendGroupFileToServer(String FileName, String ToGroup)
{
try {
dataoutputstream.writeBytes(FileName.concat("!").concat(ToUser)+"\r\n");
//send file to sever
File myFile = new File (FileName.substring(9));
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
os.close();
System.out.println("File successfully Sended to server");
}catch(IOException _IoExc) { QuitConnection(QUIT_TYPE_DEFAULT);}
try {
socket1 = new Socket(ServerName,ServerPort); //Creating new Socket
dataoutputstream = new DataOutputStream(socket1.getOutputStream());
dataoutputstream.writeBytes("FILEGET"+FileName.concat("!").concat(ToGroup+"*"+UserName)+"\r\n"); //sending string to server
} catch (IOException e1) {
e1.printStackTrace();
}
// receive file sended by server
byte [] mybytearray = new byte [filesize];
InputStream is;
try {
is = socket1.getInputStream();
FileOutputStream fos = new FileOutputStream(Filepath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead; //up to this working fine
do {
bytesRead =is.read(mybytearray, current, (mybytearray.length-current)); //not reading the file data sent by server just waiting and not go ahead
if(bytesRead >= 0)
current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray,0,current);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
There are so many problems here that it is difficult to know where to start.
The thread.sleep() in the accept() loop is literally a waste of time. It serves no useful purpose except possibly to throttle the rate at which clients are accepted. If that wasn't your intention, don't do it.
All you are doing when you catch an exception is exiting the server without even printing the exception message. So when something goes wrong, as it is here, you can't possibly know what it was. Don't do that.
readLine() returns null at EOS, on which you must close the socket, stop reading, and exit the thread. You aren't testing that, and you are therefore omitting all three of those required steps. Don't do that.
You are constructing a DataInputStream around a BufferedInputStream for use when reading commands, but you aren't passing it to the methods that process those commands. You are just passing the socket. You are therefore losing data. Don't do that. Every part of the program must use the same input stream or reader for the socket.
You are reading the entire file into memory. This (a) assumes the file size fits into an int; (b) does not scale to large files; (c) wastes space, and (d) adds latency. Don't do that.
You are ignoring the result of the read() into that buffer and assuming it was filled. You can't do that. The correct way to copy streams in Java is shown below. This works with a buffer of any size, e.g. 8192, for an input of any length, and doesn't require you to buffer the entire input into memory. You can use this loop at both the client when sending the file and at the server when receiving it.
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Similarly to (4) above, you are using a DataOutputStream around a BufferedOutputStream for some things and the socket output stream directly for others. Don't do that. All parts of the program must the same output stream or writer for the socket.
You don't need to flush() before close(); it happens automatically.
For some reason after sending the file you are creating a new connection and sending another command. You aren't even closing the connection afterwards. The server will have no easy way of knowing that this connection and this command referred to the file just sent in the code above. It is also redundant, as the receipt of the final EOS tells the server that the file has been sent successfully. Don't do this. If you need to send more information with the file, send it first, before the file, on the same connection.
The reference you cite exhibits many of the above issues. Make an effort to find a reputable starting point.
This is the solution. Please Apply this logic to your code.
I am able to send a file from server to client and client to server.
Check the following code to send the file from Client to Server. It is working great.
If you have any issues let me know.
Server Side Code:
public class ServerRecieveFile {
public static void main(String[] args) throws IOException {// TODO Auto-enerated method stub int filesize=1022386;
int bytesRead; int currentTot= ;
ServerSocket serverSocket=new ServerSocket(15123);
Socket socket=rverSocket.accept();
byte [] bytearray = new byte [filesize];
InputStream is=socket.getInputStream();
File copyFileName=new File("c:/Files Sockets/2.txt");
FileOutputStream fos = new FileOutputStream(copyFileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do {
bytesRead =is.read(bytearray, currentTot, (bytearray.length-currentTot)); if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
Client Side code:
public class ClientSendFile {
public static void main(String[] args) throws UnknownHostException, IOException {// TODO Auto-generated method stub
Client client=new Client();
Socket socket = new Socket(InetAddress.getLocalHost(),15123);
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("c:/Files Sockets/1.txt");
byte [] bytearray = new byte (int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
socket.close();
System.out.println("File transfer complete");
}
}

Socket Programming file upload issues?Complete file not uploading

Hi i am using the following code for uploding my file from android phone to the server bt the file does not upload completely..e.g i uploded a 11kb file and got only 8kb file at the server.What am i doing wrong?
Client side
Socket skt = new Socket"112.***.*.**", 3000);
String FileName=fil.getName();
PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
out2.println("Upload");
out2.println(FileName);
out2.println(spinindx);
out2.println(singleton.arrylst_setngs.get(0).toString());
out2.println(singleton.arrylst_setngs.get(1).toString());
out2.println(singleton.arrylst_setngs.get(2).toString());
out2.println(singleton.arrylst_setngs.get(3).toString());
out2.println(singleton.arrylst_setngs.get(4).toString());
out2.flush();
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
out.write(buf,0,i);
publishProgress(in.available());
System.out.println(i);
}
//Close the writers,readers and the socket.
in.close();
out.flush();
out.close();
out2.close();
skt.close();
}
catch( Exception e ) {
System.out.println(e);
}
The server side
InputStream inStream = socket.getInputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
String Request=inm.readLine();
if(Request.equals("Upload")){
fileName = inm.readLine();
chosn = inm.readLine();
lt=inm.readLine();
cs = inm.readLine();
om = inm.readLine();
o = inm.readLine();
check=inm.readLine();
//Read, and write the file to the socket
BufferedInputStream in = new BufferedInputStream(inStream);
int i=0;
File f=new File("D:/data/"+filePrefx+fileName);
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream("D:/data/"+filePrefx+fileName);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
System.out.println(i);
out.write(buf,0,i);
System.out.println("Receiving data...");
}
in.close();
inStream.close();
out.close();
fos.close();
socket.close();
Looks like you are using both a BufferedReader and a BufferedInputStream on the same underlying socket at the server side, and two kinds of output stream/writer at the client. So your BufferedReader is buffering, which is what it's supposed to do, and thus 'stealing' some of the data you're expecting to read with the BufferedInputStream. Moral: you can't do that. Use DataInputStream & DataOutputStream only, and writeUTF()/readUTF() for the 8 lines you are reading from the client before the file.
You shared the same underlying InputStream between your BufferedReader and bufferedInputStream.
What happened is, when you do the reading through BufferedReader, it reads more than the a few lines you requested from the underlying InputStream into its own internal buffer. And when you create the BufferedInputStream, the data has already been read by the BufferedReader. So Apart from what EJP suggested not to use any buffered class, you can create the BufferedInputStream, and then create the Reader on Top of it. The code is something like this:
BufferedInputStream in = new BufferedInputStream(inStream);
Reader inm = new InputStreamReader(in);
Add it to the beginning of your server code and remove this line:
BufferedInputStream in = new BufferedInputStream(inStream);
See this, i never tried though
void read() throws IOException {
log("Reading from file.");
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
log("Text read in: " + text);
}
Shamelessly copied from
http://www.javapractices.com/topic/TopicAction.do?Id=42

Categories

Resources