Skip to content

Instantly share code, notes, and snippets.

@AmirHooshangi
Created November 11, 2014 09:11
Show Gist options
  • Save AmirHooshangi/2dba6a84a809754c1247 to your computer and use it in GitHub Desktop.
Save AmirHooshangi/2dba6a84a809754c1247 to your computer and use it in GitHub Desktop.
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = null;
try {
server = new ServerSocket(9090);
} catch (IOException e) {
System.out.println(e);
}
Socket clientSocket = server.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream());
while (true) {
if( is.available() > 0 ){
if(is.readBoolean() == true){
//TODO: read a random line from a file and write from Response
os.writeUTF("Response");
}else{
//it's file
byte[] mybytearray = new byte[10024];
FileOutputStream fos = new FileOutputStream("/Users/amir/Desktoptest.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment