-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
56 lines (48 loc) · 1.56 KB
/
Server.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
import java.net.*;
import java.io.*;
import java.util.*;
public class Server extends Thread {
Socket theConnection;
public Server(Socket s) {
theConnection = s;
}
public static void main(String[] args) {
int thePort;
ServerSocket listeningSocket;
// set the port to listen on
try {
thePort = Integer.parseInt(args[0]);
if (thePort < 0 || thePort > 65535)
thePort = 3000;
} catch (Exception e) {
thePort = 3000;
}
try {
int index = 0;
Socket sockets[] = new Socket[10];
listeningSocket = new ServerSocket(thePort);
System.out.println("Accepting connections on port " + listeningSocket.getLocalPort());
while (true) {
sockets[index++] = listeningSocket.accept();
System.out.println(sockets.length);
if (index == 3) {
sendMess(sockets);
index++;
}
}
} catch (IOException e) {
System.err.println("Server aborted prematurely");
}
}
public static void sendMess(Socket[] sockets) {
for (int i = 0; i < 3; i++) {
try {
DataOutputStream outToClient = new DataOutputStream(sockets[i].getOutputStream());
String toSend = "String to send\n";
outToClient.writeBytes(toSend);
} catch (IOException e) {
System.out.println("Error\n");
}
}
}
}