mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 12:17:21 +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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user