Skip to content

Instantly share code, notes, and snippets.

@yasincidem
Created May 2, 2019 11:12
Show Gist options
  • Save yasincidem/7529a7b6c8dcf436da4f03749f86ac65 to your computer and use it in GitHub Desktop.
Save yasincidem/7529a7b6c8dcf436da4f03749f86ac65 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Created by yasin_000 on 1.5.2019.
*/
public class Server {
public static void main(String[] args) throws IOException, URISyntaxException {
Socket socket = createSocketForServer(4999);
System.out.println("Client Connected");
final InputStream socketInputStream = socket.getInputStream();
byte[] buffer = new byte[40 * 1024];
socketInputStream.read(buffer, 0, buffer.length);
File file;
Path path = Paths.get("imageeeeee.jpg");
if (Files.exists(path)) file = new File(path.toString());
else {
Files.createFile(path.getFileName());
file =new File(path.toString());
}
Utils.bytesToFile(buffer, file);
}
private static Socket createSocketForServer(int port) {
Socket serverSocket = null;
try {
serverSocket = new ServerSocket(port).accept();
} catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
return serverSocket;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment