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 d46c43e

Browse files
networking api's
1 parent fcec7ad commit d46c43e

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

‎networking/p2p/Client.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package networking.p2p;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.InetAddress;
7+
import java.net.Socket;
8+
import java.util.Scanner;
9+
/**
10+
*
11+
* @author rafiul islam
12+
*/
13+
public class Client {
14+
private static final int PORT = 6789;
15+
private static final String SENDER = "SERVER: ";
16+
public static void main(String[] args) {
17+
Scanner scan = new Scanner(System.in);
18+
19+
try{
20+
InetAddress host = InetAddress.getByName("localhost");
21+
System.out.println("Trying to connect by"+host+" with port: "+PORT);
22+
Socket socket = new Socket(host,PORT);
23+
24+
new Thread(new Runnable(){
25+
public void run(){
26+
String message = "";
27+
while(!socket.isInputShutdown()){
28+
try {
29+
DataInputStream dis = new DataInputStream(socket.getInputStream());
30+
message = dis.readUTF();
31+
if(message.equalsIgnoreCase("exit")){
32+
System.out.println(SENDER+"Exited");
33+
34+
socket.shutdownInput();
35+
socket.shutdownOutput();
36+
37+
socket.close();
38+
}
39+
else
40+
System.out.println(SENDER+message);
41+
} catch (IOException ex) {
42+
System.err.println(ex);
43+
}
44+
}
45+
}
46+
}).start();
47+
48+
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
49+
String yourMessage = "";
50+
while(!socket.isOutputShutdown()){
51+
yourMessage = scan.nextLine();
52+
if(yourMessage.equalsIgnoreCase("exit")){
53+
System.out.println("You are going to shut down this connection");
54+
dos.writeUTF(yourMessage);
55+
56+
socket.shutdownOutput();
57+
socket.shutdownInput();
58+
59+
break;
60+
}
61+
else
62+
dos.writeUTF(yourMessage);
63+
}
64+
65+
dos.flush();
66+
dos.close();
67+
68+
socket.close();
69+
} catch(IOException ex){
70+
System.err.println(ex);
71+
}
72+
}
73+
}

‎networking/p2p/Server.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package networking.p2p;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.ServerSocket;
7+
import java.net.Socket;
8+
import java.util.Scanner;
9+
10+
/**
11+
*
12+
* @author rafiul islam
13+
*/
14+
public class Server {
15+
private static final int PORT = 6789;
16+
private static final int MAX_CLIENT_QUEUE = 5;
17+
private static final int MAX_WAIT = 100000; // sec
18+
private static final String SENDER = "CLIENT: ";
19+
20+
public static void main(String[] args) {
21+
Scanner scan = new Scanner(System.in);
22+
23+
try{
24+
ServerSocket ss = new ServerSocket(PORT);
25+
System.out.println("Server will waiting 100sec for client with port"+PORT);
26+
Socket socket = ss.accept();
27+
System.out.println("Server is connected with "+socket.getRemoteSocketAddress());
28+
29+
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
30+
dos.writeUTF("Welcome !");
31+
32+
new Thread(new Runnable(){
33+
public void run(){
34+
String message = "";
35+
while(!socket.isInputShutdown()){
36+
try {
37+
DataInputStream dis = new DataInputStream(socket.getInputStream());
38+
message = dis.readUTF();
39+
if(message.equalsIgnoreCase("exit")){
40+
System.out.println(SENDER+"Exited");
41+
42+
socket.shutdownInput();
43+
socket.shutdownOutput();
44+
45+
socket.close();
46+
}
47+
else
48+
System.out.println(SENDER+message);
49+
} catch (IOException ex) {
50+
System.err.println(ex);
51+
}
52+
}
53+
}
54+
}).start();
55+
56+
String yourMessage = "";
57+
while(!socket.isOutputShutdown()){
58+
yourMessage = scan.nextLine();
59+
if(yourMessage.equalsIgnoreCase("exit")){
60+
System.out.println("You are going to shut down this connection");
61+
dos.writeUTF(yourMessage);
62+
socket.shutdownOutput();
63+
socket.shutdownInput();
64+
break;
65+
}
66+
else
67+
dos.writeUTF(yourMessage);
68+
}
69+
70+
dos.flush();
71+
dos.close();
72+
73+
socket.close();
74+
ss.close();
75+
76+
} catch(IOException ex){
77+
System.err.println(ex);
78+
}
79+
}
80+
}

0 commit comments

Comments
(0)

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