Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
113 lines (100 loc) · 3.41 KB
/
Copy pathServer.java
File metadata and controls
113 lines (100 loc) · 3.41 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
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Server extends MessagingApp implements Runnable {
Contacts reciever;
Msg sender;
Msg seen;
Msg status;
Server( Msg sender,Contacts reciever, Msg[] messages, int msg_count,Msg seen,Msg status) {
this.sender=sender;
this.reciever = reciever;
this.messages = messages;
this.msg_count = msg_count;
this.seen=seen;
this.status=status;
}
@Override
public void run() {
try (ServerSocket ss = new ServerSocket(6666);) {
System.out.println("Waiting for the Client");
Socket s = ss.accept();
System.out.println("Client Connected");
//for reading
DataInputStream d = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
Scanner sc = new Scanner(System.in);
Thread s_thread = new Thread(new Runnable() {
@Override
public void run() {
send_msg(sc,dout);
}
});
Thread r_thread = new Thread(new Runnable() {
@Override
public void run() {
rcv_msgs(d);
}
});
s_thread.start();
r_thread.start();
s_thread.join();
r_thread.join();
System.out.println("Returning to menu");
d.close();
dout.close();
} catch (Exception e) {
System.out.println("In Server Exception ");
}
}
public void send_msg(Scanner sc, DataOutputStream dout) {
String msg;
System.out.println("Write the message : ");
try {
while (true) {
// System.out.print(" Server : ");
//String
msg = sc.nextLine();
sender.setSender("Javairia");
seen.setSeen("unseen");
status.isStatus(true);
super.store_msgs(sender.getSender(), msg,reciever,status.getStatus(),seen.getSeen());
dout.writeUTF(msg);
dout.flush();
if (msg.equalsIgnoreCase("exit")) {
System.out.println("Server ended the chat");
break;
}
}
} catch (Exception r) {
System.out.println(r.getMessage());
}
}
public void rcv_msgs(DataInputStream d) {
String str;
try {
while (true) {
str = (String) d.readUTF();
//Msg sender=new Msg(reciever.getName());
sender.setSender(reciever.getName());
Contacts reciever=new Contacts("Javairia");
seen.setSeen("seen");
status.isStatus(true);
super.store_msgs(sender.getSender(),str,reciever,status.getStatus(),seen.getSeen());
System.out.println(sender.getSender()+" : "+ str);
if (str.equalsIgnoreCase("exit")) {
System.out.println("Client ended the chat");
break;
}
}
} catch (Exception r) {
System.out.println(r.getMessage());
}
}
}
/*public class Server {
public static void main(String[] args){
Thread t2=new Thread(new Server_1());
t2.start();
}
}*/