Hai
Please check the below code and let me know your suggestions.Here i am only able to perform one way communication..not get the same file as response from server..
Thanks in advance
Server code:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static Socket socket;
static int current=0;
public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text1.txt";
public final static int FILE_SIZE = 4939993 ;
public static void main(String[] args)
{
try
{
int port = 6777;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 6777");
while(true)
{
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte [] mybytearray = new byte [FILE_SIZE];
int bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
System.out.println("Message received from client is "+current);
OutputStream out = socket.getOutputStream();
File myFile=new File( FILE_TO_RECEIVED);
FileInputStream fis=new FileInputStream(myFile);
byte [] mybytearray1 = new byte [(int)myFile.length()];
int len ;
while ((len = fis.read(mybytearray1)) >= 0) {
out.write(len);
System.out.println("Message sent to the client is "+ current);
out.flush();
}
}}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
Client Code:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client
{
public final static String FILE_TO_SEND = "/home/sosdt010/Desktop/doc/character.txt";
private static Socket socket;
public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text2.txt";
public final static int FILE_SIZE = 4939993 ;
static int current=0;
public static void main(String args[])
{
try
{
String host = "10.170.0.38";
int port = 6777;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
OutputStream out = socket.getOutputStream();
File myFile=new File( FILE_TO_SEND);
FileInputStream fis=new FileInputStream(myFile);
BufferedInputStream bis=new BufferedInputStream(fis);
byte [] mybytearray1 = new byte [(int)myFile.length()];
int len ;
while ((len = fis.read(mybytearray1)) >= 0) {
out.write(mybytearray1,0,len);
out.flush();
System.out.println("Message sent to the server is "+ len);
}
InputStream is = socket.getInputStream();
FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte [] mybytearray = new byte [FILE_SIZE];
int bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
System.out.println("Message received from server is "+current);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}