-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadChat.java
150 lines (116 loc) · 4.8 KB
/
ThreadChat.java
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
package MYwork;
import java.io.*;
import java.net.*;
public class ThreadChat extends Thread {
protected ServerSocket ss;
protected Socket as ;
protected BufferedReader i;
protected PrintWriter o;
public String aIP , nameOtherAgent ,myName, text ,inputb;
protected int chatPort ;
protected String St ;
public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// constructor
public ThreadChat (int chatPort,String myName,String status) throws IOException
{
super("ThreadChat");
this.chatPort= chatPort ;
this.aIP = "";
this.nameOtherAgent="";
this.myName =myName ;
this.St=status ;
}
public ThreadChat (int chatPort, String aIP ,String nameOtherAgent,String myName,String status) throws IOException
{
super("ThreadChat");
this.chatPort= chatPort ;
this.aIP = aIP ;
this.nameOtherAgent= nameOtherAgent;
this.myName =myName ;
this.St=status ;
}
public boolean workAsServer()
{
try{
ss = new ServerSocket( this.chatPort);
//do
//{
System.out.println("waiting for connection..........");
as = ss.accept();
System.out.println(" connection with someone");
// System.out.print( as.getInetAddress().getHostAddress().toString());
//} while (!as.getInetAddress().getHostAddress().toString().equals(this.aIP));
return (true);
} catch (IOException e)
{
System.err.println("Accept failed.");
return (false);
}
}
public boolean workAsClient()
{
try
{
as = new Socket (this.aIP,this.chatPort);
System.out.println(" make connection with "+this.nameOtherAgent);
return (true);
} catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to:"+this.nameOtherAgent);
return (false);
}
}
public void run()
{
boolean x = false;
try{
if (this.St.equals("l"))
x = workAsServer() ; //work as server for the agent who want to listen
else if ( this.St.equals("t"))
x=workAsClient(); //work as server for the agent who want to talk
if(x == true) // make sure that everything working
{
o = new PrintWriter(as.getOutputStream(), true);
i = new BufferedReader(new InputStreamReader(as.getInputStream()));
System.out.println("\t\t\t +++++ ( Successful Connection )+++++\t\t\t\n");
if (this.St.equals("l"))
{
while ((inputb=i.readLine())!= null )
{
// recieve message from the other Agent who is connect with you
System.out.println(inputb);
if ( inputb.toLowerCase().contains("end chat")) break;
// send the meesage to the other Agent who is connect with you
System.out.print("|<"+this.myName+"> : ");
do { text =input.readLine();} while (text.equals(""));
o.println("|<"+this.myName+"> : "+text);
if (text.toLowerCase().contains("end chat")) break ;
}// end while
ss.close();
}
else if (this.St.equals("t"))
{ // to make some kind of synchronization amon chatting ,
//intial step one agent sends message as first message and the Other should wait this message
System.out.print("|<"+this.myName+"> : ");
do { text =input.readLine();} while (text.equals(""));
o.println("|<"+this.myName+"> : "+text);
while ((inputb=i.readLine())!= null )
{// recieve message from the other Agent who is connect with you
System.out.println(inputb);
if ( inputb.toLowerCase().contains("end chat")) break;
// send the meesage to the other Agent who is connect with you
System.out.print("|<"+this.myName+"> : ");
do { text =input.readLine();} while (text.equals(""));
o.println("|<"+this.myName+"> : "+text);
if (text.toLowerCase().contains("end chat")) break ;
}
}
o.close();
i.close();
as.close();
}// end if
else System.out.println("\t\t++++++++ Error is encountered ++++++++\t\t");
}catch(Exception e) { System.err.println(e) ; }
System.out.println("\t\t++++++++ End Session ++++++++\t\t");
}// end run
}// end ThreadChat class