This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Transfer via Sockets

 
vidya sagar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}


 
Rehan Malik
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic