mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 18:16:29 +02:00
quemen redes
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package es.um.redes.nanoFiles.tcp.server;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
@@ -11,22 +14,26 @@ public class NFServer implements Runnable {
|
||||
|
||||
public static final int PORT = 10000;
|
||||
|
||||
|
||||
private NFServerState state = new NFServerState();
|
||||
|
||||
private ServerSocket serverSocket = null;
|
||||
|
||||
public ServerSocket getServerSocket() {
|
||||
return serverSocket;
|
||||
}
|
||||
|
||||
public NFServer() throws IOException {
|
||||
/*
|
||||
* TODO: (Boletín SocketsTCP) Crear una direción de socket a partir del puerto
|
||||
* done: (Boletín SocketsTCP) Crear una direción de socket a partir del puerto
|
||||
* especificado (PORT)
|
||||
*/
|
||||
/*
|
||||
* TODO: (Boletín SocketsTCP) Crear un socket servidor y ligarlo a la dirección
|
||||
* done: (Boletín SocketsTCP) Crear un socket servidor y ligarlo a la dirección
|
||||
* de socket anterior
|
||||
*/
|
||||
|
||||
|
||||
|
||||
InetSocketAddress serverSocketAddress = new InetSocketAddress(PORT);
|
||||
serverSocket = new ServerSocket();
|
||||
serverSocket.bind(serverSocketAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,16 +54,40 @@ public class NFServer implements Runnable {
|
||||
|
||||
while (true) {
|
||||
/*
|
||||
* TODO: (Boletín SocketsTCP) Usar el socket servidor para esperar conexiones de
|
||||
* done: (Boletín SocketsTCP) Usar el socket servidor para esperar conexiones de
|
||||
* otros peers que soliciten descargar ficheros.
|
||||
*/
|
||||
/*
|
||||
* TODO: (Boletín SocketsTCP) Tras aceptar la conexión con un peer cliente, la
|
||||
* done: (Boletín SocketsTCP) Tras aceptar la conexión con un peer cliente, la
|
||||
* comunicación con dicho cliente para servir los ficheros solicitados se debe
|
||||
* implementar en el método serveFilesToClient, al cual hay que pasarle el
|
||||
* socket devuelto por accept.
|
||||
*/
|
||||
|
||||
boolean connectionOk = false;
|
||||
Socket socket = null;
|
||||
try {
|
||||
socket = serverSocket.accept();
|
||||
connectionOk = true;
|
||||
} catch (IOException e) {
|
||||
// conn refused
|
||||
}
|
||||
|
||||
if (connectionOk) {
|
||||
System.out.println("accepted");
|
||||
try {
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
int intNumber = dis.readInt();
|
||||
System.out.println("received " + intNumber);
|
||||
|
||||
int newInt = intNumber + 1;
|
||||
dos.writeInt(newInt);
|
||||
System.out.println("sent " + newInt);
|
||||
} catch (IOException e) {
|
||||
// ???????
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -69,6 +100,7 @@ public class NFServer implements Runnable {
|
||||
* @see java.lang.Runnable#run()
|
||||
*/
|
||||
public void run() {
|
||||
boolean stopServer = false; // HAY QUE CAMBIAR ESTO PORQUE NO SÉ DE DÓNDE COJONES SALE (ver TODO l. 147)
|
||||
/*
|
||||
* TODO: (Boletín SocketsTCP) Usar el socket servidor para esperar conexiones de
|
||||
* otros peers que soliciten descargar ficheros
|
||||
@@ -87,6 +119,26 @@ public class NFServer implements Runnable {
|
||||
* hilo es el que se encarga de atender al cliente conectado, no podremos tener
|
||||
* más de un cliente conectado a este servidor.
|
||||
*/
|
||||
|
||||
while (!stopServer) {
|
||||
Socket socket = null;
|
||||
boolean connectionOk = false;
|
||||
try {
|
||||
socket = serverSocket.accept();
|
||||
connectionOk = true;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Connection refused");
|
||||
}
|
||||
|
||||
if (connectionOk) {
|
||||
NFServerThread serverThread = new NFServerThread(socket, state);
|
||||
serverThread.start();
|
||||
int connNum = state.getNumberOfConnections();
|
||||
InetSocketAddress clientAddr = (InetSocketAddress) socket.getRemoteSocketAddress();
|
||||
System.out.println("New connection from " + clientAddr);
|
||||
System.out.println("Total connections = " + connNum);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -129,7 +181,15 @@ public class NFServer implements Runnable {
|
||||
* de su hash completo.
|
||||
*/
|
||||
|
||||
|
||||
InetSocketAddress clientAddr = (InetSocketAddress) socket.getRemoteSocketAddress();
|
||||
try {
|
||||
while(true) {
|
||||
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
|
||||
DataInputStream dis = new DataInputStream(socket.getInputStream());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
29
es/um/redes/nanoFiles/tcp/server/NFServerState.java
Normal file
29
es/um/redes/nanoFiles/tcp/server/NFServerState.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package es.um.redes.nanoFiles.tcp.server;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NFServerState {
|
||||
private int numberOfConnections;
|
||||
private ArrayList<Socket> sockets;
|
||||
|
||||
public NFServerState() {
|
||||
numberOfConnections = 0;
|
||||
sockets = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int getNumberOfConnections() {
|
||||
return numberOfConnections;
|
||||
}
|
||||
|
||||
public List<Socket> getSockets() {
|
||||
// tu puta madre va a hacer copias
|
||||
return sockets;
|
||||
}
|
||||
|
||||
public void updateState(Socket socket) {
|
||||
sockets.add(socket);
|
||||
numberOfConnections++;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,15 @@ public class NFServerThread extends Thread {
|
||||
* (un socket distinto para "conversar" con un cliente)
|
||||
*/
|
||||
|
||||
|
||||
|
||||
private Socket socket;
|
||||
|
||||
public NFServerThread(Socket externalSocket, NFServerState state) {
|
||||
socket = externalSocket;
|
||||
state.updateState(socket);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
NFServer.serveFilesToClient(socket);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user