can i speed up the scanning port process? - java

import java.net.*;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PortScanner {
public static void main(String[] args) {
InetAddress ia=null;
String host=null;
try {
host=JOptionPane.showInputDialog("Enter the Host name to scan:\n example: example.com");
if(host!=null){
ia = InetAddress.getByName(host);
scan(ia); }
}
catch (UnknownHostException e) {
System.err.println(e );
}
System.out.println("Bye from NFS");
//System.exit(0);
}
public static void scan(final InetAddress remote) {
//variables for menu bar
int port=0;
String hostname = remote.getHostName();
for ( port = 70; port < 65536; port++) {
try {
Socket s = new Socket(remote,port);
System.out.println("Server is listening on port " + port+ " of " + hostname);
s.close();
break;
}
catch (IOException ex) {
// The remote host is not listening on this port
System.out.println("Server is not listening on port " + port+ " of " + hostname);
}
}//for ends
}
}
please help me.

I am not sure if this will speed things up, but since each socket your making is independent of the next socket, have you tried making more threads so that you can create new sockets when older sockets are waiting for their handshake to complete.

Instead of using the line
Socket s = new Socket(remote,port);
You should use
Socket s = new Socket();
int timeout = 100; // milliseconds
s.connect( new InetSocketAddress( remote, port ), timeout );
This way you won't have to wait for the default TCP timeout to see that this port is blocked by a firewall without any response.

Related

Having trouble reaching my PC (or a PI) from outside LAN

so today I've tried to install Citadel Email server on a raspberry pi which went ok but unfortunantly I cannot reach it from outside LAN.
I've tried to pinpoint the problem and use scanner tools that look for open ports like these :https://www.whatsmyip.org/port-scanner/
I've verified that my public IP adress is the same as my domain returns. Which indeed it is.
I've checked port forwarding severel times.
Last but not least I've wrote this java code to have a really simple example:
package main;
import java.io.IOException;
public class Main {
public static void main(String... args){
try {
Server server = new Server(8080);
Client client = new Client(8080);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private boolean stop = false;
public Server(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
Runnable runnable = new Runnable() {
#Override
public void run() {
while(stop == false) {
try {
Socket socket = serverSocket.accept();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("Hello World!");
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String input = inputStream.readUTF();
System.out.println("Client wrote: " + input);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable, "server executor");
thread.start();
}
public void stop(){
this.stop = true;
}
}
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static final String LOCALHOST = "localhost";
public static final String KIRAZUKE = "---";
public static final String PUBLIC_IP_4 = "---";
public Client(int port) {
try{
doTest(LOCALHOST, port);
} catch (IOException e) {
e.printStackTrace();
}
try{
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
doTest(KIRAZUKE, port);
} catch (IOException e) {
e.printStackTrace();
}
try{
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
try{
doTest(PUBLIC_IP_4, port);
} catch (IOException e) {
e.printStackTrace();
}
}
private void doTest(String host, int port) throws IOException {
System.out.println("Opening to: " + host);
Socket socket = new Socket(host, port);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("Hello dear Server ... calling from " + host + " ... over.");
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String string = inputStream.readUTF();
System.out.println("Response from server after " + host + " call: " + string);
}
}
So I've replaced the domain name and my public ip with dashes for privacy reasons. But what happens is that when using the localhost connection everything works fine the server prints the text sent by the client and vise versa. While using either the public IP or Domain name it fails due to time out.
What could be reasons that any incoming traffic is blocked altough port forwarding is enabled for the 8080 port (and other ports that I tried) ?
Note: I've also called my ISP, according to them they "don't block anything". Additionally I tried port forwarding port 3389 and tried remote desktop to my pi but that also failed.
Thanks in advance :)

Java UDP p2p chatprogram.

im making a P2P chat program. for this i use UDP datagrams. but im having some problems.
sometimes the program works without any problems. but most of the times only 1 of the 2 people recieve the message or sometimes neither of the 2 people get a message. im thinking about going to TCP but i want to keep it P2P so no central server.
my code:
package herexChatProg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.swing.JOptionPane;
import login.MainScreen;
public class MessageSender extends Thread {
private int Port;
private String recIP;
private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private MainScreen Screen;
private DatagramSocket ds = null;
private DatagramPacket dp = null;
public MessageSender(MainScreen m, String ip, int port) throws Exception {
recIP = ip;
Port = port;
Screen = m;
System.out.println("chat program: IP address: " + recIP + " port " + Port);
start();
}
public void run() {
try {
// open DatagramSocket to receive
ds = new DatagramSocket(Port);
// loop forever reading datagrams from the DatagramSocket
while (true) {
byte[] buffer = new byte[65000]; // max char length
dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
String s = new String(dp.getData(), 0, dp.getLength());
Screen.writeText(s);
// System.out.println("UDP datagram length " + s.length() + "
// from IP " + dp.getAddress() + " received: " + s);
}
} catch (SocketException se) {
System.err.println("chat error (Socket Closed = good): " + Se.getMessage());
JOptionPane.showMessageDialog(null, "Please check your connection or try to log on again");
} catch (IOException se) {
System.err.println("chat error: " + se.getMessage());
}
}
public void Stop() {
if (ds != null) {
ds.close();
ds = null;
}
}
public boolean sendMessage(String message) throws IOException {
try {
System.out.println("Sending to " + recIP + " socket " + Port + " data: " + message);
byte[] data = message.getBytes();
DatagramSocket theSocket = new DatagramSocket();
DatagramPacket theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(recIP), Port);
theSocket.send(theOutput);
Screen.writeText(message);
return true;
} catch (IOException e) {
return false;
}
}
}
if any of you spot the problem or can help me that would be great
thanks DenTilloZie
if any of you spot the problem or can help me that would be great
The "problem" of not all messages reaching their destination is normal, it's because UDP is unreliable. It isn't due to your code (well due to the fact you're using UDP instead of TCP).
If you want to be sure that each message arrives at its destination you have 2 possibilities. You could use TCP instead of UDP as TCP guarantees each message arrive (and guarantees even more). If you really want to continue with UDP you will have to send an acknowledge (to the original sender of the message) when receiving a message. When a sender receives the acknowledge he can be sure the message arrived at its destination. However there are a lot of extra problems you should fix if you use UDP (message order, ...). So i would just recommend to use TCP instead of reinventing the wheel.
im thinking about going to TCP but i want to keep it P2P so no central server.
This will be hard to achieve. There are different possibilities for implementing a Peer-to-peer system :
With a central coordinater
By flooding
By distributed hash tables
The first approach is the simplest but is not possible as you explicitly want no central server. But the other approaches are considerably more difficult.

Type of scan used in this program

I came across a relatively simple java code while trying to design a Tcp PortScanner. The following code checks for the listening ports available on the local machine.
I wonder what kind of scan it uses to check its status, i mean is it using Tcp Syn scan , or Tcp connect scan or anything other than this. I'm grateful for your response.
import java.net.*;
import java.net.InetAddress;
import java.net.Socket;
class PortScanner {
public static void main(String []args) {
for (int port = 1; port <= 65535; port++) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost", port), 1000);
socket.close();
System.out.println("Port " + port + " is open");
} catch (Exception ex) {
}
}
}
}

Create ServerSocket java with my public IP

Well, here's my code:
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TcpServerEcho {
public static void main(String argv[]) {
int port = Integer.parseInt(argv[0]);
ServerSocket server_socket = null;
try {
InetAddress thisIp = InetAddress.getByName("kiravaio.homepc.it");
System.out.println(thisIp.getHostAddress());
//or
//server_socket = new ServerSocket(port, 10, InetAddress.getByName("79.47.49.68"));
server_socket = new ServerSocket(port, 10, thisIp);
}
catch (Exception e) {
System.err.println("Impossible to create socket server!");
System.out.flush();
System.exit(1);
}
System.out.printf("Server active on port: %d and on address %s\n", port, server_socket.getInetAddress());
Socket client_socket = null;
boolean exec = true;
while(exec) {
try {
client_socket = server_socket.accept();
InputStream is = client_socket.getInputStream();
OutputStream os = client_socket.getOutputStream();
boolean stop = false;
System.out.println("Received: ");
while(!stop) {
int b = is.read();
System.out.print((char)b);
if(b == -1) {stop = true;}
else {os.write( (byte)b );}
}
}
catch (IOException ioe) {
System.err.println("Error I/O!");
} finally {
try {
client_socket.close();
}
catch (IOException ioe) {}
}
System.out.println("");
}
try {
/* Never executed */
server_socket.close();
} catch (IOException ioe) {}
}
}
There's no way for me to create a socket on my public ip.
I have a domain
kiravaio.homepc.it
always updated everytime my ip public changes.
So as first, I get my ip address with
InetAddress thisIp = InetAddress.getByName("kiravaio.homepc.it");
then I create the socket:
server_socket = new ServerSocket(port, 10, thisIp);
But it always fails and I don't know why,
Only creating a socket on 127.0.0.1 works!
:(
Looking up homepc.it reveals this is a dyndns service domain name. That domain name is resolving to the IP of your router not your PC.
Your PC will have a private network IP address (e.g. 192.168.1.x) assigned to it by your router (that IP is being used by your PC to talk to your router which is performing NAT (Network Address Translation)) .
You have to bind to that address (or simply use the constructor for ServerSocket that only takes a port; this will bind to 0.0.0.0 (INADDR_ANY) and listen on all interfaces), and configure your router to forward the port you have chosen to that IP address.
(If you don't know how to do that, you should consult your router's manual or perhaps post a question to https://superuser.com/)
I strongly believe that for ServerSocket, you don't need to specify which ip address is going to be used, just do new ServerSocket(port) and the client should be able to connect via your dynamic domain.

close all the sockets after using it? on server side

Do I have to close all the sockets after using it? Where should I put them in this code? My program just works normally when I run it. However, when I re-run it, it said "Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind". Therefore, I think I did not close all the socket after using it.
import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
public static void main(String args[]) throws Exception {
int PORT = 5555; // Open port 5555
//open socket to listen
ServerSocket server = new ServerSocket(PORT);
Socket client = null;
while (true) {
System.out.println("Waiting for client...");
// open client socket to accept connection
client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
}
}
}
Call server.close() in a finally block.
ServerSocket server = new ServerSocket(PORT);
try {
while (true) {
System.out.println("Waiting for client...");
// open client socket to accept connection
Socket client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
}
} finally {
server.close();
}
Address already in use: JVM_Bind - means, that you operation system is not closed socket after previous use. It closes on timeout about 30-180 seconds.
I don't realy know how to do this in java, but in C code it may be done like this, before bind system function call:
int yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
That mean: set the flag (option) SO_REUSEADDR to sockfd socket.
In java must exists appropriate mechanism for do the same.
You are running an infinite while loop , have a boolean variable to say when to stop , i think you are not exiting gracefully, that is why port is not closed.
May be you can try like this
import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
static int NUM_CONN_TO_WAIT_FOR=15;
boolean exitServer =false;
public static void main(String args[]) throws Exception {
int PORT = 5555; // Open port 5555
//open socket to listen
ServerSocket server = new ServerSocket(PORT);
Socket client = null;
static int connections =0;
try
{
while (!exitServer ) {
System.out.println("Waiting for client...");
// open client socket to accept connection
if ( connections < NUM_CONN_TO_WAIT_FOR )
{
client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
} else
{
exitServer =true;
}
connections++;
}
} catch (Exception e)
{
System.out.println(e.printStackTrace());
}
finally
{
if ( client != null)
client.close();
if ( server!= null)
server.close();
}
}
}

Categories