-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnection.java
More file actions
75 lines (65 loc) · 2.74 KB
/
Connection.java
File metadata and controls
75 lines (65 loc) · 2.74 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
package javaapplication;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedList;
import javax.swing.JTextArea;
public class Connection implements Runnable{
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
JTextArea textServer;
private LinkedList<Socket> clients = new LinkedList<Socket>();
String noms[] = {"Devansh", "Gaurav", "Mrinal"};
private String connecter;
private static int nb=0;
public Connection(Socket s,LinkedList client,JTextArea textServer ){
socket = s;
clients = client;
this.textServer=textServer;
}
@Override
public void run() {
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
if(nb==0)
textServer.setText("Server "+"\n_________________________________________\n"+"server is Connected....\n"+"Devansh is connected... \n");
else if(nb==1){
textServer.append("Devansh is connected... \n");
textServer.append("Gaurav is connected... \n");}
else if(nb==2){
textServer.append("Devansh is connected... \n");
textServer.append("Gaurav is connected... \n");
textServer.append("Mrinal is connected... \n"); }
out.writeUTF("Client "+"\n_______________________________________________\n");
nb++;
while(true){
String msg = in.readUTF();
String[] t4=msg.split("@");
String recipient=t4[1];
String sender=t4[2];
msg=t4[0];
textServer.append(sender+" sends a message to: "+recipient+" \n");
for(int i=0;i<noms.length;i++){
if(noms[i].equals(recipient)){
out = new DataOutputStream(clients.get(i).getOutputStream());
out.writeUTF(sender+" :"+msg+"\n");
}
if(noms[i].equals(sender)){
out = new DataOutputStream(clients.get(i).getOutputStream());
out.writeUTF("You :"+msg+"\n");
}
}
}
} catch (IOException e) {
for (int i = 0; i < clients.size(); i++) {
if(clients.get(i) == socket){
clients.remove(i);
break;
}
}
}
}
}