-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CORNOU Benjamin
committed
Oct 8, 2018
1 parent
223982f
commit 9026622
Showing
3 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package fr.bencor29.datatransfer.server; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.Socket; | ||
|
||
import fr.bencor29.datatransfer.Utils; | ||
import fr.bencor29.datatransfer.events.TransferEvent; | ||
import fr.bencor29.datatransfer.events.listeners.TransferListener; | ||
|
||
public class Client { | ||
|
||
private static int lastID = 0; | ||
|
||
private int id; | ||
private Socket socket; | ||
private Thread th; | ||
|
||
public Client(Socket so, DTServer server) { | ||
lastID++; | ||
|
||
this.id = lastID; | ||
this.socket = so; | ||
th = new Thread(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
while(!socket.isClosed()) | ||
try { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
String str = reader.readLine(); | ||
if(str != null) { | ||
TransferEvent tev = new TransferEvent(socket, str); | ||
for(TransferListener tl : server.getListeners()) | ||
tl.onReceive(tev); | ||
} | ||
} catch (IOException e) {} | ||
} | ||
}); | ||
} | ||
|
||
public int getID() { | ||
return id; | ||
} | ||
|
||
public Socket getSocket() { | ||
return socket; | ||
} | ||
|
||
public void close() { | ||
try { | ||
socket.close(); | ||
th.interrupt(); | ||
} catch (IOException e) {} | ||
} | ||
|
||
public void sendString(String str) throws IOException { | ||
Utils.sendString(socket, str); | ||
} | ||
|
||
} |
This file contains 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
This file contains 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