FILE TRANSFER VIA LAN NETWORK.. SOCKET PROGRAMMING IN JAVA
CLIENT.JAVA
import java.net.*;
import java.io.*;
public class Client { public static void main (String [] args ) throws IOException
{
int filesize=1022386;
int bytesRead;
int currentTot = 0;
Socket socket = new Socket("127.0.0.1",15123);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("copy.doc");
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();
}
}
SERVER.JAVA
import java.net.*;
import java.io.*;
public class Server
{
public static void main (String [] args ) throws IOException
{
ServerSocket serverSocket = new ServerSocket(15123);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("Document.doc");
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");
}
}
CLIENT.JAVA
import java.net.*;
import java.io.*;
public class Client { public static void main (String [] args ) throws IOException
{
int filesize=1022386;
int bytesRead;
int currentTot = 0;
Socket socket = new Socket("127.0.0.1",15123);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("copy.doc");
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();
}
}
SERVER.JAVA
import java.net.*;
import java.io.*;
public class Server
{
public static void main (String [] args ) throws IOException
{
ServerSocket serverSocket = new ServerSocket(15123);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("Document.doc");
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");
}
}
No comments:
Post a Comment