Created
March 9, 2014 20:44
-
-
Save jaredsburrows/9454282 to your computer and use it in GitHub Desktop.
Basic Client and Server in Java
This file contains 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.DataOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
public class Client { | |
public static void main(final String[] argv) throws Exception { | |
final File folder = new File(System.getProperty("user.dir")); | |
while (true) { | |
final File[] listOfFiles = folder.listFiles(); | |
for (final File listOfFile : listOfFiles) { | |
if (listOfFile.isFile()) { | |
System.out.println(listOfFile.getAbsolutePath().toString()); | |
final File myFile = new File(listOfFile.getAbsolutePath() | |
.toString()); | |
final long size = myFile.length(); | |
if ((size / 1024) <= 100) { | |
final Socket sock = new Socket("127.0.0.1", 9999); | |
final byte[] mybytearray = new byte[(int) myFile | |
.length()]; | |
final BufferedInputStream bis = new BufferedInputStream( | |
new FileInputStream(myFile)); | |
if (mybytearray.length > 0) { | |
bis.read(mybytearray, 0, mybytearray.length); | |
} | |
final OutputStream os = sock.getOutputStream(); | |
final DataOutputStream dos = new DataOutputStream(os); | |
dos.writeUTF(myFile.getName()); | |
if (mybytearray.length > 0) { | |
dos.writeLong(mybytearray.length); | |
dos.write(mybytearray, 0, mybytearray.length); | |
} | |
dos.flush(); | |
if (mybytearray.length > 0) { | |
os.write(mybytearray, 0, mybytearray.length); | |
} | |
os.flush(); | |
sock.close(); | |
bis.close(); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains 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.BufferedOutputStream; | |
import java.io.DataInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Server { | |
public static void main(final String[] args) throws IOException { | |
final ServerSocket servsock = new ServerSocket(9999); | |
String fileName = "test"; | |
while (true) { | |
final Socket sock = servsock.accept(); | |
final byte[] mybytearray = new byte[1024]; | |
final InputStream is = sock.getInputStream(); | |
final DataInputStream clientData = new DataInputStream(is); | |
if (clientData != null) { | |
fileName = clientData.readUTF(); | |
} | |
final FileOutputStream fos = new FileOutputStream("..files/" | |
+ fileName); | |
System.out.println(fileName); | |
final BufferedOutputStream bos = new BufferedOutputStream(fos); | |
int bytesRead = 0; | |
while (bytesRead >= 0) { | |
bytesRead = is.read(mybytearray, 0, mybytearray.length); | |
if (bytesRead >= 0) { | |
bos.write(mybytearray, 0, bytesRead); | |
} | |
// if (bytesRead < 1024) | |
// { | |
// bos.flush(); | |
// break; | |
// } | |
} | |
bos.close(); | |
sock.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment