https://github.com/dmlloyd/openjdk
okay, on this occasion I will share a tutorial how to make a connection between two computers using a peer to peer network, the first step we do is to create a program for the server computer, after that we will create a program for the client computer. we will determine the server and port number to match.
Okay, we just started the tutorial.
package clientserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Server {
public static void main(String[] args) throws IOException{
int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
ServerSocket serverSock=new ServerSocket(6066);
Socket Sock=serverSock .accept();
DataOutputStream out =new DataOutputStream(Sock.getOutputStream());
out.writeUTF("i am fine, thank you");
DataInputStream in= new DataInputStream(Sock.getInputStream());
System.out.println(in.readUTF());
Sock.close();
}
}
import java.io.DataInputStream; This program is a program that functions for input types with Stream typesimport java.io.DataOutputStream; this is also Stream type, but this is for the output section.import java.io.IOException;this is a class that functions to handle the two previous classes, namely input and output.import java.net.ServerSocket; this is a class that works to run a server.import java.net.Socket; this is a class that must exist on each socket programming.import javax.swing.JOptionPane;this is the class we use if we implement the JOptionpane input method.public class Server { this is a class name declarationpublic static void main(String[] args) throws IOException{this is the main function that must be present in every java programming.int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));this is a declaration of port variables with integer data types.String IP = JOptionPane.showInputDialog("Input Your IP Server : "); this is a declaration of IP variables with varchar data types.ServerSocket serverSock=new ServerSocket(6066);this is the determination of the port number that is = 6066Socket Sock=serverSock .accept();this is the creation of an accept action if the client computer with the same port number.out.writeUTF("connecting succes"); this is a notification if both computers are successfully connected.package clientserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Client {
public static void main(String[] args)throws IOException {
int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
Socket sock=new Socket("localhost", 6066);
DataInputStream in= new DataInputStream(sock.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("waiting for connection");
sock.close();
}}
input port number
input your IP
OK, the program is finished, the two computers can be connected to each other through the peer to peer network
https://gist.github.com/andilapulga/4cd8c88b2e686720e5a4c24ef094394d