Created
November 11, 2014 01:37
-
-
Save OneRaynyDay/9c6fc5c9e2bc9f9609f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.*; | |
public class Client { | |
Socket socket; | |
String DELIMITER_FOR_FILES = "\\w*(?=\\.)"; | |
public Client() { | |
} | |
public void connect(String fName, String host, int port) { | |
try { | |
socket = new Socket(host, port); | |
File myFile = new File(fName); | |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream( | |
myFile)); | |
byte[] mByteArray = new byte[(int) myFile.length()]; | |
System.out.println(mByteArray.length); | |
bis.read(mByteArray, 0, mByteArray.length); | |
DataOutputStream os = new DataOutputStream(new BufferedOutputStream( | |
socket.getOutputStream())); | |
// header for size of file | |
os.writeLong(myFile.length()); | |
os.write(mByteArray, 0, mByteArray.length); | |
os.flush(); | |
String destination = (fName.split(DELIMITER_FOR_FILES))[0] + ".xml"; | |
acceptFile(destination, host, port); | |
// While there's still something inside of the BufferedReader | |
} catch (Exception e) { | |
System.out.println("could not connect"); | |
e.printStackTrace(); | |
} | |
} | |
public void acceptFile(String fileDestination, String host, int port) { | |
try { | |
DataInputStream dis = new DataInputStream(new BufferedInputStream( | |
socket.getInputStream())); | |
FileOutputStream fos = new FileOutputStream(new File(fileDestination)); | |
BufferedOutputStream bos = new BufferedOutputStream(fos); | |
int bytesToRead = (int) dis.readLong(); | |
byte[] bytearray = new byte[bytesToRead]; | |
dis.read(bytearray, 0, bytearray.length); | |
bos.write(bytearray, 0, bytesToRead); | |
bos.flush(); | |
bos.close(); | |
socket.close(); | |
} catch (Exception e) { | |
System.out.println("trouble receiving io file"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClientTester { | |
public static void main(String[] args){ | |
Client client = new Client(); | |
client.connect("Restaurants.csv", "127.0.0.1", 1234); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment