-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketServer.java
59 lines (49 loc) · 1.6 KB
/
socketServer.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.net.*;
import java.io.*;
/**
*
* @author Robert
*/
public class socketServer {
/**
* @param args the command line arguments
*/
public int _port = -1;
boolean _isRunning = false;
private ServerSocket server = null;
private Socket client = null;
private PrintWriter outStream = null;
private BufferedReader inStream = null;
public socketServer() {}
public socketServer(int port) {_port = port;}
public int getPort () {return _port;}
public void setPort(int port) {_port = port;}
public boolean isRunning () { return _isRunning;}
final public void run()
{
try { server = new ServerSocket(getPort()); }
catch (IOException ex) { System.err.println(ex.getMessage()); return;}
while(isRunning())
{
try {client = server.accept(); }
catch (IOException ex) {System.err.println(ex.getMessage()); continue;}
try {
outStream = new PrintWriter(client.getOutputStream(), true);
inStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
outStream.println(processMessage(inStream.readLine()));
}
catch (IOException ex) {System.err.println(ex.getMessage()); continue;}
}
}
public String processMessage(String message)
{
return message;
}
public static void main(String[] args) {
// TODO code application logic here
}
}