Forums Register Login

File Transfer via Sockets

+Pie Number of slices to send: Send
Hi,
I am facing a strange problem. Any help would be apreciated.
What I am trying to do :
----------------------
Transfer a datafile(ascii) from a client to a server using TCP sockets.

My Problem :
----------
The file contents & size varies at the server end after the transfer is complete. This happens when the server is run on a different PC, however if the client and the server are run on the same PC (localhost) this does not happen.
The Client Code:
------------------
import java.net.*;
import java.io.*;
public class tcpClient {

public static void main(String[] args) {
int port = 1500;
// String server = "10.172.30.33"; // this is anothe PC
String server = "localhost";
Socket socket = null;
int ERROR = 1;

// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server "+socket.getInetAddress()+":"+socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}

try {
byte[] buf = new byte[1024];
OutputStream os = socket.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(os,1024);
// I am hardcoding the file name, for test purpose
String file = "C://source_code.sql";
FileInputStream in = new FileInputStream(file);
int i;
int bytecount=1024;
while ((i = in.read(buf,0,1024)) != -1) {
bytecount=bytecount+1024;
out.write(buf,0,1024);
out.flush();
}
in.close();
out.close();
System.out.println("Bytes Sent :"+bytecount);
}
catch (IOException e) {
System.out.println(e);
}
try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
The Server code:
-----------------
import java.net.*;
import java.io.*;
public class tcpServer {

public static void main(String args[]) {

int port;
ServerSocket server_socket;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
}
try {

server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + server_socket.getLocalPort());
// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
try {
byte[] b = new byte[1024];
int len=0;
int bytcount=1024;
String FileName="C://ftptest.txt";
FileOutputStream inFile = new FileOutputStream(FileName);
InputStream is = socket.getInputStream();
BufferedInputStream in2 = new BufferedInputStream(is,1024);
while( (len = in2.read(b,0,1024)) != -1 ) {
bytcount=bytcount+1024;
inFile.write(b,0,1024);

}
in2.close();
inFile.close();
System.out.println("Bytes Writen : "+bytcount);
}
catch(IOException e) {
System.out.println("Unable to open file" + e);
return;
}
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}

}

}

catch (IOException e) {
System.out.println(e);
}
}
}


+Pie Number of slices to send: Send
Well I'm not sure if you're still reading this reply since I am replying almost about 3 weeks later but maybe your'e reading this.
Anyways, I had a similar problem when I was attempting the same project that you are currently working on. The way to get around this is to read each BYTE one by one. If you used a buffer to read the bytes, somehow, extra bytes were added to the file length on the other side. I've managed to get it working beautifully with just reading one byte at a time.
Most of your code is almost exactly like mine except that you're using a buffer to store the bytes into whereas I was just reading the bytes one at a time and writing them.
Try it out and let me know if it works.
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2006 times.
Similar Threads
FTP(File transfer)
Java Server limit
Java net BindException:Address already in use: JVM_Bind
sockets: How to receive server response in client
Please help with BufferReader.read()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:58:52.