hi this is my client program for transfering Image , while transfering the image file it is getting corrupted , not able to open that image file , i'm not able to identify the bug , can any one help me.
DataInputStream input = new DataInputStream(s.getInputStream());
DataOutputStream output = new DataOutputStream(s.getOutputStream());
System.out.println("Writing.......");
FileInputStream fstream = new FileInputStream("Blue hills.jpg");
DataInputStream in = new DataInputStream(fstream);
byte[] buffer = new byte[1000];
int bytes = 0;
while ((bytes = fstream.read(buffer)) != -1) {
output.write(buffer, 0, bytes);
}
in.close();
I assume that s is a Socket and you're attempting to transfer a file over the network? Here's an example of sending a file with sockets. It just sets up a server socket in a thread and connects to itself.
public static void main(String[] args) throws IOException {
new Thread() {
public void run() {
try {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
InputStream in = socket.getInputStream();
FileOutputStream out = new FileOutputStream("out.jpg");
copy(in, out);
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
Socket socket = new Socket("localhost", 3434);
OutputStream out = socket.getOutputStream();
FileInputStream in = new FileInputStream("in.jpg");
copy(in, out);
out.close();
in.close();
}
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
Related
I'm trying to send a string which is the filename and then the file itself to a server. The string is being received and used to create the file by the server. However, there isn't any data actually written into the file by the server.
I got the file transfer to work before I added the Writer's (with the file names being hard-coded) to the server and client but now I can't get both to work at the same time.
Client:
public class Client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
String fileName = sc.nextLine();
System.out.println(fileName);
try {
File file = new File(fileName);
Socket socket = new Socket("localhost", 15000);
OutputStream os = socket.getOutputStream();
Writer w = new OutputStreamWriter(os, "UTF-8");
w.write(fileName);
w.close();
os.flush();
byte[] mybytearray = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, 0, mybytearray.length);
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
socket.close();
} catch (Exception e) { }
}
}
}
Server:
public class Server extends Thread {
public static final int PORT = 15000;
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket sock = serverSocket.accept();
readFile(sock);
}
} catch (Exception e) {
}
}
private void readFile(Socket socket) throws Exception {
InputStream ois = socket.getInputStream();
Reader r = new InputStreamReader(ois, "UTF-8");
String filename = "";
int ch = r.read();
while(ch != -1) {
filename += (char) ch;
System.out.println(filename);
ch = r.read();
}
r.close();
System.out.println(filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] bytearr = new byte[4096];
System.out.println("Reading file...");
BufferedOutputStream bos = new BufferedOutputStream(fos);
while ((ois.read(bytearr)) > 0) {
bos.write(bytearr);
}
bos.close();
System.out.println("Writing file complete...");
}
public static void main(String[] args) {
new Server().start();
}
}
This is my solution, this approach needs some improvements:
Explanation:
In position 0: Length of file name
In position 1 to the length of the file name: filename as bytes.
In position 1 + length of file name til length: The content of the file.
Basically, I'm sending all the information to the server at once (This is one improvement that you will need to figure out).
Another improvement is to send the file by chunks and not all the file at once.
Client class:
public class Client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
String fileName = sc.nextLine();
System.out.println(fileName);
try {
File file = new File(fileName);
byte[] mybytearray = new byte[1 + fileName.getBytes().length + (int) file.length()];
mybytearray[0] = (byte) fileName.length();
System.arraycopy(fileName.getBytes(), 0, mybytearray, 1, fileName.getBytes().length);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, fileName.getBytes().length + 1, (int) file.length());
Socket socket = new Socket("localhost", 15000);
OutputStream os = socket.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Server class:
public class Server extends Thread {
public static final int PORT = 15000;
public static void main(String[] args) {
new Server().start();
}
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket sock = serverSocket.accept();
readFile(sock);
}
} catch (Exception e) {
}
}
private void readFile(Socket socket) throws Exception {
InputStream ois = socket.getInputStream();
byte[] resultBuff = new byte[0];
byte[] buff = new byte[1024];
int k;
while ((k = ois.read(buff, 0, buff.length)) > -1) {
byte[] tbuff = new byte[resultBuff.length + k];
System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length);
System.arraycopy(buff, 0, tbuff, resultBuff.length, k);
resultBuff = tbuff;
}
byte lengthOfFileName = resultBuff[0];
byte fileNameBytes[] = new byte[lengthOfFileName];
System.arraycopy(resultBuff, 1, fileNameBytes, 0, lengthOfFileName);
String filename = new String(fileNameBytes);
FileOutputStream fos = new FileOutputStream(filename + System.currentTimeMillis());
byte[] bytearr = new byte[resultBuff.length - (lengthOfFileName + 1)];
System.out.println("Writing file...");
System.arraycopy(resultBuff, lengthOfFileName + 1, bytearr, 0, bytearr.length);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(bytearr);
bos.close();
System.out.println("Writing file complete...");
}
}
Hope this helps!
Happy coding time!
You need to close the File Ouptut Stream as well. With
bos.close;
Add
fos.close;
hi guys i tried to send txt through socket it worked using the following source code but i got an exception when i want to send the zip files.Any help will be appreciated thanks
Java server
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub}
sendFile("FileTest.rar");
}
public static Boolean sendFile(String strFileToSend) {
try {
ServerSocket serverSocket = new ServerSocket(1592);
Socket socket = serverSocket.accept();
System.out.println("Connection accepted from " + socket);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
File file = new File(strFileToSend);
// send file length
out.println(file.length());
// read file to buffer
byte[] buffer = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.read(buffer, 0, buffer.length);
// send file
BufferedOutputStream bos = new BufferedOutputStream(
socket.getOutputStream());
bos.write(buffer);
bos.flush();
// added
dis.close();
serverSocket.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
C# client
public static bool receiveFile(string ip,string strFilePath)
{
try
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(ip, 1592);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader sr = new StreamReader(networkStream);
//read file length
int length = int.Parse(sr.ReadLine());
//read bytes to buffer
byte[] buffer = new byte[length];
networkStream.Read(buffer, 0, (int)length);
//write to file
BinaryWriter bWrite = new BinaryWriter(File.Open(strFilePath, FileMode.Create));
bWrite.Write(buffer);
bWrite.Flush();
bWrite.Close();
return true;
}
catch(Exception exException)
{
MessageBox.Show(exException.Message);
return false;
}
}
The problem is when i want to deal with rar or zip files.
Thanks in advance
i got the exeption below.It occurs at this line in the client app
int length = int.Parse(sr.ReadLine());
I'm making simple Client-Server FileTransfer application in java.
Here is the concept of this application:
Client connects to server
Server shows menu to Client which looks like this
2a) Make copy of file on server
2b) Download copy of file from
server
2c) Exit
Client choose one of those options (Client sends message by ObjectOutputStream, code below).
The problem is when i choose one of options (for example Option #2a) I can't choose another one when previous is done. It says that socket is close.
There is my code:
Client:
public class Client {
private Socket s = new Socket("localhost",3002);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
FileInputStream fis;
public Client(String host, int port) throws IOException {
//s = new Socket(host, port);
try {
System.out.println("Hello!");
boolean finished = false;
Scanner sc = new Scanner(System.in);
while(!finished){
System.out.println("\n\n1.Make copy of file on server");
System.out.println("2.Restore copy");
System.out.println("3.Exit\n");
char c = sc.nextLine().charAt(0);
switch(c){
case '1':
this.sendMessage(1);
makeCopy();
oos.close();
break;
case '2':
this.sendMessage(2);
saveFile(s);
break;
case '3':
this.sendMessage(3);
finished=true;
System.exit(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Client client = new Client("localhost",3002);
}
public void sendMessage(int message_id) throws IOException{
oos.writeInt(message_id);
oos.flush();
}
private void makeCopy() throws IOException {
File file = new File("D:\\klient\\doKopii.bmp");
File dest = new File("D:\\serwer\\kopiaPliku.bmp");
boolean ifExists = dest.exists();
if(ifExists && !file.isDirectory()){
System.out.println("Copy is already on server.");
}
else{
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
oos.write(buffer);
}
}
//fis.close();
//oos.close();
}
private void saveFile(Socket clientSock) throws IOException {
//DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("D:\\klient\\przywroconaKopia.bmp");
File zSerwera = new File("D:\\serwer\\kopiaPliku.bmp");
byte[] buffer = new byte[4096];
int filesize = (int)zSerwera.length();
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = ois.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
//fos.close();
//ois.close();
}
}
And Server side code:
public class Server extends Thread{
private ServerSocket ss;
Socket clientSock;
ObjectInputStream ois;
ObjectOutputStream oos;
//DataOutputStream dos;
FileInputStream fis;
//DataInputStream dis;
FileOutputStream fos;
public Server(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
clientSock = ss.accept();
oos = new ObjectOutputStream(clientSock.getOutputStream());
ois = new ObjectInputStream(clientSock.getInputStream());
serviceClient(clientSock);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void serviceClient(Socket s) throws Exception{
int message_id;
try{
message_id = ois.readInt();
switch(message_id){
case 1:
saveFile(s);
oos.flush();
break;
case 2:
sendFile(s);
oos.flush();
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
public void sendFile(Socket s) throws IOException {
File file = new File("D:\\serwer\\kopiaPliku.bmp");
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
oos.write(buffer);
}
}
public void saveFile(Socket s) throws IOException{
//dis = new DataInputStream(s.getInputStream());
File copy = new File("D:\\serwer\\kopiaPliku.bmp");
if(copy.exists() && !copy.isDirectory()){
}
else{
fos = new FileOutputStream(copy);
File zSerwera = new File("D:\\klient\\doKopii.bmp");
byte[] buffer = new byte[4096];
int filesize = (int)zSerwera.length();
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = ois.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
}
}
public static void main(String[] args) {
Server server = new Server(3002);
server.start();
}
}
Exact Exception:
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(ObjectOutputStream.java:1890)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1875)
at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1822)
at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:719)
at com.luki.classes.Client.sendMessage(Client.java:58)
at com.luki.classes.Client.<init>(Client.java:33)
at com.luki.classes.Client.main(Client.java:53)
Where do I have to close all of those streams to prevent socket closing ?
You close your ObjectOutputStreamat the end of your first case statement in Client for no apparent reason. Remove that line and you won't get the error.
i've write a code for transferring file over sockets,
the file is transfers properly but it isn't closed even after calling .close() method.
however the file closes after closing the "sockets", but i want to keep the connection open.
here the server sends the file to client
SERVER CODE
public void sendFile(String sfileName) throws IOException{
try{
in = new FileInputStream(sfileName);
out = socket.getOutputStream();
transferData(in,out);
}
finally {
in.close();
in = null;
System.gc();
}
}
private void transferData(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while(in.available()==0);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
CLIENT CODE :
public void recieveFile(String rfileName) throws IOException{
try{
in = socket.getInputStream();
System.out.println("Reciever file : " + rfileName);
out = new FileOutputStream(rfileName);
transferData(in,out);
}
finally{
out.flush();
out.close();
out = null;
System.gc();
}
}
private void transferData(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while(in.available()==0);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
what is wrong with the code ?
I think you should use Socket.shutdownOutput()
I'm trying to build a client/server application that sends files but I'm having an issue with larger files. I'm using a BufferedInputStream to read information from a file and OutputStream to write to the socket. I have a loop that reads 1 KB from the file and then sends it, which works fine for the first 25 loops then crashes with a socket write error. Any ideas? Here's the code.
Client
import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TCPClient
{
public static void main(String[] args)
{
/*Variables*/
int serverPort = 8899;
String ip = "localhost";
File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt
System.out.println(myFile.length());
try
{
/*Connect to Server*/
Socket sock = new Socket(ip, serverPort);
System.out.println("Connection Made");
/*Create Streams*/
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream clientOutput = sock.getOutputStream();
/*This is the old code for transfer*/
/*Create Byte Array
byte[] myByteArray = new byte[(int) myFile.length()]; //was 1024
/*Send File
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
*/
for(long i = 0; i <= myFile.length(); i += 1024)
{
byte[] myByteArray = new byte[1024];
bis.read(myByteArray, 0, 1024);
clientOutput.write(myByteArray, 0, 1024);
clientOutput.flush();
System.out.println("i is: " + i);
}
System.out.println("File Written");
sock.close();
} catch (IOException ex)
{
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("You can't do that!");
}
System.out.println("Finished");
}
}
Server
import java.io.*;
import java.net.*;
public class RequestHandler
{
public void handleRequest()
{
try
{
ServerSocket welcomeSocket = new ServerSocket(8899);
while(true)
{
Socket socket = welcomeSocket.accept();
System.out.println("Socket Open");
/* Create byte array */
byte[] mybytearray = new byte[1024 * 512];
/* Create streams */
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("newFile.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
/*Write to file*/
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
/*Close Stream and Socket*/
bos.close();
socket.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
RequestHandler rq = new RequestHandler();
rq.handleRequest();
System.out.println("Here");
}
}
Your copying technique is incorrect. This is how to copy streams in Java:
byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();
You need this at both ends. You cannot assume that any given read will fill the buffer, so you must loop until EOS. Note that you don't flush inside the loop.