Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37dcb5e

Browse files
Update and rename Networking/Basic/Client.java to Networking/Console-Broadcast-Server-Chatting/BroadcastServer.java
1 parent 96b054c commit 37dcb5e

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

‎Networking/Basic/Client.java‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//version 11.1
2+
3+
//This is the broadcast server
4+
5+
import java.util.*;
6+
import java.io.*;
7+
import java.net.*;
8+
9+
class BroadcastServer
10+
{
11+
public static void main(String s[])
12+
{
13+
try
14+
{
15+
System.out.println("server started");
16+
ServerSocket ss=new ServerSocket(1500);
17+
ArrayList al=new ArrayList();
18+
// Collection prepared, contains all connected clients
19+
20+
while(true)
21+
{
22+
Socket sk=ss.accept();
23+
System.out.println("client connected");
24+
al.add(sk);
25+
new ClientThread(al,sk);
26+
}
27+
}
28+
catch(Exception e)
29+
{ e.printStackTrace();
30+
}
31+
}
32+
}
33+
class ClientThread extends Thread
34+
{
35+
Socket sk;
36+
ArrayList al;
37+
ClientThread(ArrayList al,Socket sk)
38+
{
39+
this.al=al;
40+
this.sk=sk;
41+
start();
42+
}
43+
public void run()
44+
{
45+
try
46+
{
47+
DataInputStream din=new DataInputStream(sk.getInputStream());
48+
while(true)
49+
{
50+
String str=din.readUTF();
51+
if(str.equals("stop"))
52+
{
53+
al.remove(sk);
54+
break;
55+
}
56+
else
57+
{
58+
broadcast(str);
59+
}
60+
}
61+
}
62+
catch(Exception e)
63+
{ e.printStackTrace();
64+
}
65+
}
66+
void broadcast(String msg)
67+
{
68+
try
69+
{
70+
for(Object ob:al)
71+
{
72+
Socket sk=(Socket)ob;
73+
DataOutputStream dout=new DataOutputStream(sk.getOutputStream());
74+
dout.writeUTF(msg);
75+
dout.flush();
76+
}
77+
}
78+
catch(Exception e)
79+
{ e.printStackTrace();
80+
}
81+
}
82+
}
83+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /