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 908e307

Browse files
Create ChatClient.java
1 parent 37dcb5e commit 908e307

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//version 11.1
2+
3+
//This is the chat client program, run on client PCs. Same as simple server chatting
4+
5+
// ChatClient.java
6+
7+
import java.net.*;
8+
import java.io.*;
9+
10+
class ChatClient
11+
{
12+
public static void main(String s[])
13+
{
14+
try
15+
{
16+
System.out.println("client started");
17+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
18+
System.out.println("enter user name");
19+
String name=br.readLine();
20+
System.out.println("welcome "+name);
21+
Socket sk=new Socket("localhost",1500);
22+
System.out.println("connection established");
23+
new ReaderThread(sk);
24+
25+
while(true)
26+
{
27+
System.out.println("enter msg and enter stop to terminate");
28+
String str=br.readLine();
29+
DataOutputStream dout=new DataOutputStream(sk.getOutputStream());
30+
if(str.equals("stop"))
31+
{
32+
dout.writeUTF(str);
33+
break;
34+
}
35+
else
36+
{
37+
dout.writeUTF(name+": "+str);
38+
dout.flush();
39+
}
40+
}
41+
}
42+
catch(Exception e)
43+
{ e.printStackTrace();
44+
}
45+
}
46+
}
47+
class ReaderThread extends Thread
48+
{
49+
Socket sk;
50+
ReaderThread(Socket sk)
51+
{
52+
this.sk=sk;
53+
setDaemon(true);
54+
start();
55+
}
56+
public void run()
57+
{
58+
try
59+
{
60+
DataInputStream din=new DataInputStream(sk.getInputStream());
61+
while(true)
62+
{
63+
String str=din.readUTF();
64+
System.out.println(str);
65+
}
66+
}
67+
catch(Exception e)
68+
{ e.printStackTrace();
69+
}
70+
}
71+
}

0 commit comments

Comments
(0)

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