|
Re: send a string from client to server
|
Posted: Jun 21, 2017 10:57 PM
|
|
I can send a string after a file I sent.
this is my code:
ClientSide
//import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; //import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket;
public class Client_R01 {
public final static int SOCKET_PORT = 13267; // you may change this public final static String SERVER = "127.0.0.1"; // localhost public final static String FILE_TO_RECEIVED = "F:/E/C drive/fglog.txt"; // you may change this, I give a // different name because i don't want to // overwrite the one used by server...
public final static int FILE_SIZE = 6022386; // file size temporary hard coded // should bigger than the file to be downloaded
public static void main (String [] args ) throws IOException { int bytesRead; int current = 0; FileOutputStream fos = null; BufferedOutputStream bos = null; Socket sock = null; BufferedReader br = null; try { sock = new Socket(SERVER, SOCKET_PORT); System.out.println("Connecting...");
// receive file byte [] mybytearray = new byte [FILE_SIZE]; InputStream is = sock.getInputStream(); fos = new FileOutputStream(FILE_TO_RECEIVED); bos = new BufferedOutputStream(fos); 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); bos.flush(); System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)"); String Fhash = MD5.getMD5(Byte_change(mybytearray));//Changing a Byte br = new BufferedReader(new InputStreamReader(is)); String Rhash = br.readLine(); System.out.println("Right Hash is : " + Rhash); System.out.println("False Hash is : " + Fhash); } finally { if (fos != null) fos.close(); if (br !=null) br.close(); if (bos != null) bos.close(); if (sock != null) sock.close(); } } public static String Byte_change(byte[] b) { // String s = b.toString(); StringBuilder s1 = new StringBuilder(b.toString()); s1.setCharAt(2, '#'); b = s1.toString().getBytes(); return s1.toString(); } } /////////////
ServerSide
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket;
/** * * @author saeed */ public class server_R01 { public final static int Socket_Port = 13267; public final static String File_To_Send = "F:/E/C drive/Filter builder/fglog.txt"; public static void main (String [] args){ FileInputStream fis = null; BufferedInputStream bis = null; OutputStream os = null; ServerSocket servsock = null; Socket sock1 = null; Socket sock2 = null; PrintWriter pw = null; try { servsock = new ServerSocket(Socket_Port); while (true) { System.out.println("Waiting..."); try { sock1 = servsock.accept(); System.out.println("Accepted connection : " + sock1); // send file File myFile = new File (File_To_Send); byte [] mybytearray = new byte [(int)myFile.length()]; fis = new FileInputStream(myFile); bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); os = sock1.getOutputStream(); System.out.println("Sending " + File_To_Send + "(" + mybytearray.length + " bytes)"); os.write(mybytearray,0,mybytearray.length); String hash1 = MD5.getMD5(mybytearray.toString()); pw = new PrintWriter(os,true); pw.write(hash1); // pw.close(); // os.close(); os.flush(); System.out.println("Done."); } catch (IOException ex){ System.out.println("Connection and sending of file is not occured."); } finally { if (bis != null) bis.close(); if (pw != null) pw.close(); if (os != null) os.close(); if (sock1!=null) sock1.close(); if (sock2!=null) sock2.close(); if (servsock != null) servsock.close(); } } }catch (IOException ex){ System.out.println("that doesn't work."); } } }
|
|