Repository navigation
Expand file tree
/
Copy pathClient.java
More file actions
118 lines (101 loc) · 3.46 KB
/
Copy pathClient.java
File metadata and controls
118 lines (101 loc) · 3.46 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
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Client extends MessagingApp implements Runnable {
//192.168.148.223
Contacts reciever;
Msg sender;
Msg seen;
Msg status;
Client( 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 (Socket s = new Socket("localhost", 6666);) {
System.out.println("Connected to Server");
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
DataInputStream d = new DataInputStream(s.getInputStream());
Scanner sc = new Scanner(System.in);
Thread sendThread = new Thread(new Runnable() {
@Override
public void run() {
sending_msgs(dout, sc);
}
});
// thread for receiving messages
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
recieving_msgs(d);
}
});
// Start both threads
sendThread.start();
receiveThread.start();
sendThread.join();
receiveThread.join();
System.out.println("Returning to menu");
dout.close();
d.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void sending_msgs(DataOutputStream dout, Scanner sc) {
String input;
System.out.println("Write the msg : ");
try {
while (true) {
//String
// System.out.print(" Client : ");
input = sc.nextLine();
sender.setSender("Javairia");
seen.setSeen("unseen");
status.isStatus(true);
super.store_msgs(sender.getSender(), input,reciever,status.getStatus(),seen.getSeen());
dout.writeUTF(input);
dout.flush();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Client ended the chat");
break;
}
}
} catch (Exception s) {
System.out.println(s.getMessage());
}
}
public void recieving_msgs(DataInputStream d) {
String str;
try {
while (true) {
str = (String) d.readUTF();
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("Server ended the chat");
break;
}
}
} catch (Exception r) {
System.out.println(r.getMessage());
}
}
}
/*public class Client{
// void Msg_display();
public static void main(String[] arg) {
// Client_1 c= new Client();
Thread t=new Thread(new Client_1());
t.start();
}
}*/