Java client server program to read text file using Socket Programming
Simple java network program in which client send a text file to server and server returns number of characters, lines, words and sentences of that file to client.
Code:
Server:
package aj.pkg8;
import java.net.*;
import java.io.*;
public class aj8server {
public static void main (String [] args ) throws IOException {
ServerSocket serverSocket = new ServerSocket(1111);
Socket socket = serverSocket.accept();
File transferFile = new File ("sample.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();
}
}
import java.net.*;
import java.io.*;
public class aj8client{
public static void main (String [] args) throws IOException {
int filesize=1024;
int bytesRead;
int currentTot = 0,tchar=0,word=0,line=0,sentance=0;
Socket socket = new Socket("127.0.0.1",1111);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("sample2.txt");
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();
FileReader fr = new FileReader("sample.txt");
int i;
while ((i=fr.read()) != -1){
tchar++;
if ((char) i == ' ') {
word++;
}
if ((char) i == '.') {
sentance++;
}
if ((char) i == '\n') {
line++;word++;
}}
line++;word++;
String answer = "Words: "+word+" Lines: "+line+" Sentances: "+sentance+" Total Characters: "+tchar;
System.out.println(answer);
OutputStream ostream = socket.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(answer);
socket.close();
}}
import java.net.*;
import java.io.*;
public class aj8server {
public static void main (String [] args ) throws IOException {
ServerSocket serverSocket = new ServerSocket(1111);
Socket socket = serverSocket.accept();
File transferFile = new File ("sample.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();
}
}
Client:
package aj.pkg8;import java.net.*;
import java.io.*;
public class aj8client{
public static void main (String [] args) throws IOException {
int filesize=1024;
int bytesRead;
int currentTot = 0,tchar=0,word=0,line=0,sentance=0;
Socket socket = new Socket("127.0.0.1",1111);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("sample2.txt");
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();
FileReader fr = new FileReader("sample.txt");
int i;
while ((i=fr.read()) != -1){
tchar++;
if ((char) i == ' ') {
word++;
}
if ((char) i == '.') {
sentance++;
}
if ((char) i == '\n') {
line++;word++;
}}
line++;word++;
String answer = "Words: "+word+" Lines: "+line+" Sentances: "+sentance+" Total Characters: "+tchar;
System.out.println(answer);
OutputStream ostream = socket.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(answer);
socket.close();
}}
Comments
Post a Comment