-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerThread.java
More file actions
45 lines (41 loc) · 1.25 KB
/
ServerThread.java
File metadata and controls
45 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.net.*;
import java.io.*;
public class ServerThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerThread(Socket socket){
super("ServerThread");
this.socket = socket;
try{
this.in = new BufferedReader(new InputStreamReader (socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Conexao feita: " + socket.toString());
}
catch(IOException e){
e.printStackTrace();
}
}
public void run(){
while(true){
try{
String inputLine;
while((inputLine = in.readLine()) != null){
System.out.println("Mensagem recebida.");
out.println("Pong");
break;
}
}
catch(IOException e){
System.out.println(e.getMessage() + ": " + socket.getPort());
break;
}
}
try{
socket.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}