Created
November 11, 2014 09:11
-
-
Save AmirHooshangi/2dba6a84a809754c1247 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.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