Skip to content

Commit 09bd3ee

Browse files
committed
Server Chat Application
1 parent a99d1ce commit 09bd3ee

File tree

5 files changed

+130
-33
lines changed

5 files changed

+130
-33
lines changed

Socket-Programming/.idea/workspace.xml

Lines changed: 78 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.

Socket-Programming/src/MyClient1.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.net.*;
2+
import java.io.*;
3+
4+
public class MyClient1 {
5+
public static void main(String[] args) throws Exception{
6+
Socket s = new Socket("localhost",3333);
7+
DataInputStream din = new DataInputStream(s.getInputStream());
8+
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
String str="",str2="";
11+
while(!str.equals("stop")){
12+
str = br.readLine();
13+
dout.writeUTF(str);
14+
dout.flush();
15+
str2 = din.readUTF();
16+
System.out.println("Server says: " + str2);
17+
}
18+
dout.close();
19+
s.close();
20+
}
21+
}

Socket-Programming/src/MyServer1.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.net.*;
2+
import java.io.*;
3+
4+
public class MyServer1 {
5+
public static void main(String[] args) throws Exception{
6+
// open socket and bind it 3333
7+
ServerSocket ss = new ServerSocket(3333);
8+
// listens until a client asked for a connection
9+
Socket s = ss.accept();
10+
DataInputStream din = new DataInputStream(s.getInputStream());
11+
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
String str = "",str2="";
14+
15+
// read until client says stop
16+
while(!str.equals("stop")){
17+
str = din.readUTF();
18+
System.out.println("clients says: " + str);
19+
str2 = br.readLine();
20+
dout.writeUTF(str2);
21+
/*
22+
* The java.io.Writer.flush() method flushes the stream. If the stream has saved any characters from
23+
* the various write() methods in a buffer, write them immediately to their intended destination.
24+
*/
25+
dout.flush();
26+
}
27+
din.close();
28+
s.close();
29+
ss.close();
30+
}
31+
}

0 commit comments

Comments
 (0)