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 205cdcf

Browse files
java net framework for P2P TCP/IP
1 parent d46c43e commit 205cdcf

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

‎networking/p2p/Client.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,47 @@
1111
* @author rafiul islam
1212
*/
1313
public class Client {
14+
/**
15+
* Client will try to connect with server using same port as server has.
16+
* Here I define a constant port and other side username.
17+
* Before client try to connect with server, server must opened before.
18+
* @see networking.p2p.Server
19+
*/
20+
1421
private static final int PORT = 6789;
1522
private static final String SENDER = "SERVER: ";
23+
1624
public static void main(String[] args) {
25+
/**
26+
* A Scanner for input user message from console.
27+
*/
1728
Scanner scan = new Scanner(System.in);
1829

1930
try{
31+
/**
32+
* Client socket need an InedAdress for connection and a port that is
33+
* also used by server.
34+
*/
2035
InetAddress host = InetAddress.getByName("localhost");
2136
System.out.println("Trying to connect by"+host+" with port: "+PORT);
22-
Socket socket = new Socket(host,PORT);
23-
37+
Socket socket = new Socket(host, PORT);
38+
39+
/**
40+
* This thread will stream from remote side of the socket and display to user.
41+
* socket will return an InputStream to DataInputStream and then DataInputStream
42+
* will read the message as String and print it on console.
43+
*/
2444
new Thread(new Runnable(){
2545
public void run(){
46+
DataInputStream dis = null;
47+
try{
48+
dis = new DataInputStream(socket.getInputStream());
49+
} catch(IOException ex){
50+
System.err.println(ex);
51+
}
2652
String message = "";
27-
while(!socket.isInputShutdown()){
53+
while(!socket.isInputShutdown() || !socket.isClosed()){
2854
try {
29-
DataInputStream dis = new DataInputStream(socket.getInputStream());
3055
message = dis.readUTF();
3156
if(message.equalsIgnoreCase("exit")){
3257
System.out.println(SENDER+"Exited");
@@ -44,10 +69,14 @@ public void run(){
4469
}
4570
}
4671
}).start();
47-
72+
/**
73+
* DataOutputStream will write by socket OutputStream while socket is opened.
74+
* Scanner will take the message from console and pass it to DataOutputStream
75+
* Connection will shutdown after any of user type 'exit' (ignore case).
76+
*/
4877
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
4978
String yourMessage = "";
50-
while(!socket.isOutputShutdown()){
79+
while(!socket.isOutputShutdown() || !socket.isClosed()){
5180
yourMessage = scan.nextLine();
5281
if(yourMessage.equalsIgnoreCase("exit")){
5382
System.out.println("You are going to shut down this connection");

‎networking/p2p/KnockMultiP2PServer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package networking.p2p;
2+
3+
import java.io.DataInputStream;
4+
import java.io.IOException;
5+
import java.net.InetAddress;
6+
import java.net.Socket;
7+
8+
/**
9+
*
10+
* @author rafiul islam
11+
*/
12+
public class KnockMultiP2PServer {
13+
private static final int PORT = 9876;
14+
15+
public static void main(String[] args) {
16+
try {
17+
InetAddress host = InetAddress.getByName("localhost");
18+
Socket socket = new Socket(host, PORT);
19+
DataInputStream dis = new DataInputStream(socket.getInputStream());
20+
System.out.println(dis.readUTF());
21+
22+
dis.close();
23+
socket.close();
24+
25+
} catch (IOException ex) {
26+
System.err.println(ex);
27+
}
28+
}
29+
}

‎networking/p2p/MultipleP2PServer.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package networking.p2p;
2+
3+
import java.io.DataOutputStream;
4+
import java.io.IOException;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
/**
8+
*
9+
* @author rafiul islam
10+
*/
11+
public class MultipleP2PServer {
12+
private static final int PORT = 9876;
13+
private static final int MAX_WAIT = 100000;
14+
private static final int MAX_CLIENT_QUEUE = 5;
15+
16+
public static void main(String[] args) {
17+
try {
18+
ServerSocket ss = new ServerSocket(PORT, MAX_CLIENT_QUEUE);
19+
ss.setSoTimeout(MAX_WAIT);
20+
21+
while(true){
22+
Socket socket = ss.accept();
23+
System.out.println("Server is connected with "+socket.getRemoteSocketAddress());
24+
Thread sender = new Thread(new Runnable(){
25+
public void run(){
26+
try {
27+
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
28+
dos.writeUTF("Hello From Server");
29+
dos.flush();
30+
dos.close();
31+
} catch (IOException ex) {
32+
System.err.println(ex);
33+
}
34+
}
35+
});
36+
sender.start();
37+
}
38+
} catch (IOException ex) {
39+
System.err.println(ex);
40+
}
41+
}
42+
}

‎networking/p2p/Server.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,58 @@
1212
* @author rafiul islam
1313
*/
1414
public class Server {
15+
/**
16+
* Server class will create a SocketServer with a defined port.
17+
* To connect with this server, Client must use this same port.
18+
* After client send a request to this server to make a P2P connection,
19+
* server must accept the request before any communication between them.
20+
* After Server accept the request a Socket will return by the system.
21+
* Then Server's Socket and Client's Socket can communicate.
22+
* @see networking.p2p.Client
23+
*
24+
*/
1525
private static final int PORT = 6789;
16-
private static final int MAX_CLIENT_QUEUE = 5;
1726
private static final int MAX_WAIT = 100000; // sec
1827
private static final String SENDER = "CLIENT: ";
1928

2029
public static void main(String[] args) {
30+
/**
31+
* Scanner for input user message from console.
32+
*/
2133
Scanner scan = new Scanner(System.in);
2234

2335
try{
36+
/**
37+
* A ServerSocket is created with a defined port and set maximum time for
38+
* routing client.
39+
* When server accept a client, it will send a greeting message via OutputStream.
40+
*/
2441
ServerSocket ss = new ServerSocket(PORT);
25-
System.out.println("Server will waiting 100sec for client with port"+PORT);
42+
ss.setSoTimeout(MAX_WAIT);
43+
System.out.println("Server will waiting 100 sec for client with port "+PORT);
2644
Socket socket = ss.accept();
2745
System.out.println("Server is connected with "+socket.getRemoteSocketAddress());
2846

2947
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
3048
dos.writeUTF("Welcome !");
3149

50+
/**
51+
* This thread will stream from remote side of the socket and display to user.
52+
* socket will return an InputStream to DataInputStream and then DataInputStream
53+
* will read the message as String and print it on console.
54+
*/
3255
new Thread(new Runnable(){
3356
public void run(){
57+
DataInputStream dis = null;
58+
try{
59+
dis = new DataInputStream(socket.getInputStream());
60+
} catch(IOException ex){
61+
System.err.println(ex);
62+
}
3463
String message = "";
35-
while(!socket.isInputShutdown()){
64+
while(!socket.isInputShutdown() || !socket.isClosed()){
3665
try {
37-
DataInputStreamdis = newDataInputStream(socket.getInputStream());
66+
3867
message = dis.readUTF();
3968
if(message.equalsIgnoreCase("exit")){
4069
System.out.println(SENDER+"Exited");
@@ -54,7 +83,7 @@ public void run(){
5483
}).start();
5584

5685
String yourMessage = "";
57-
while(!socket.isOutputShutdown()){
86+
while(!socket.isOutputShutdown() || !socket.isClosed()){
5887
yourMessage = scan.nextLine();
5988
if(yourMessage.equalsIgnoreCase("exit")){
6089
System.out.println("You are going to shut down this connection");

0 commit comments

Comments
(0)

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