-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
154 lines (134 loc) · 3.57 KB
/
Server.java
File metadata and controls
154 lines (134 loc) · 3.57 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
public class Server
{
private int count = 0;
private ArrayList<DataOutputStream> clientOutputStreams = new ArrayList<DataOutputStream>();
private ArrayList<DataInputStream> serverInputStreams = new ArrayList<DataInputStream>();
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private String stdin;
public Server()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int port;
System.out.print("port : ");
port = Integer.parseInt(in.readLine());
System.out.println("Waiting for connection");
ServerSocket serverSocket = new ServerSocket(port);
Typeto t = new Typeto();
Thread th = new Thread(t);
th.start();
while (t.contin)
{
Socket clientSocket = serverSocket.accept();
if(clientSocket.isConnected())
{
try
{
count ++ ;
System.out.println("Currently " + count + " Clients");
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
clientOutputStreams.add(dos);
dos.writeUTF("you are the " + count + " client");
Thread thread = new Thread(new Clientto(clientSocket , clientOutputStreams));
thread.start();
}
catch (Exception e)
{
count --;
System.out.println(e.toString() + "count = " + count);
}
}
}
br.close();
in.close();
serverSocket.close();
System.out.println("Server is Closed");
}
catch (Exception e)
{
System.out.println("Someone left");
}
}
static class Typeto implements Runnable
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static boolean contin = true;
@Override
public void run() {
while (true)
{
try {
String stdin = br.readLine();
if(!stdin.equals("Quit"))
Clientto.broadCast("Server: " + stdin);
else
Clientto.broadCast(stdin);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static class Clientto implements Runnable
{
DataInputStream dis;
static ArrayList<DataOutputStream> clientOutputStreams = new ArrayList<DataOutputStream>();
protected int count ;
protected Socket clientSocket;
public Clientto(Socket clientSocket , ArrayList<DataOutputStream> clientOutputStreams )
{
this.clientOutputStreams = clientOutputStreams;
this.clientSocket = clientSocket;
try
{
dis = new DataInputStream(clientSocket.getInputStream());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void run()
{
try
{
while (true)
{
String read = dis.readUTF();
System.out.println(read);
broadCast(read);
}
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
public static void broadCast(String message)
{
DataOutputStream writer = null;
Iterator<DataOutputStream> it = clientOutputStreams.iterator();
while( it.hasNext() )
{
try
{
writer = it.next();
writer.writeUTF(message);
writer.flush();
}
catch (Exception e)
{
System.out.println(e.toString());
clientOutputStreams.remove(writer);
}
}
}
}
}