I have program write it in java it's wait a connection on port
when I scan the server by nmap the program is stop (erorr)
this is my code
SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
sslServerSocket = (SSLServerSocket)sslServerSocketfactory.createServerSocket(intSSLport);
out = new PrintWriter(sslSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
String value = in.readLine();
thanks jtahlborn ,,,, I solved by using try and catch ,,, and make catch return to
sslSocket = (SSLSocket)sslServerSocket.accept()
again
nmap is scanning open ports, by trying to connect to them (sending TCP SYN).
Since you've opened server socket, it returns when there's a client connecting to it.
In your case it's nmap, which tries to connect.
AFAIK, nmap won't complete TCP connection procedure (3 way handshake). nmap will receive TCP SYN/ACK sent as result of your call to accept(), and will conclude the port is open.
That it's all, nmap won't send 3rd message required to complete TCP connection procedure (ACK).
Related
I am doing a project for which connection between server and client is required.
I did it by adding TCP sockets.
Here is the code fraction :
Server:
ServerSocket welcomeSocket = new ServerSocket(80);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
WorkerThread wt = new WorkerThread(connectionSocket, id);
Thread t = new Thread(wt);
t.start();
workerThreadCount++;
}
Client :
Socket skt = new Socket("192.168.0.108", 80); // The IP address is from cmd->ipconfig/all-> IPv4 Address
outToServer = new PrintWriter(skt.getOutputStream(), true);
inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
It all works when both ends are in same device/under same WiFi.But I don't understand what to do for creating connection over internet.
Please help with clear steps.
Here:
Socket skt = new Socket("192.168.0.108", 80);
That is local address. If you want to have a server that is reachable on the internet, then that server needs to have its global public IP address!
In other words: you have to make sure that the server can be reached from the internet somehow. For example by turning to some service provider that hosts servers that you can then equip with your code!
The whole purpose of 192.168 addresses is to be defined only in a local subnet.
Alternatively, you have to check if your ISP has a service where the ISP assigns an IP address to your connection, and that allows calls from the internet to go to your "place".
Meaning: when you want to receive phone calls, you need a phone that is connected to the phone net!
In order to connect to a socket over WAN, you must port forward that port to your local device. This can be done in your routers' settings.
192.168.0.108 --> That's your local IP-address.
This can be used on your local network without any requirements for port forwarding whatsoever. However, to use it over WAN, execute the following steps:
Step 1: Search for your routers' model number and port forwarding on Google on how-to forward port 80 to your local IP-address. Warning: use a static IP-address on your local device to prevent your IP from changing after a reboot.
Step 2: Go to a website like IP Chicken and find your external IP-address.
You can then connect to your socket using:
Socket skt = new Socket("[EXTERNALIP]", 80);
Please be noticed: unless you have a business network, your external IP-address will probably change from time to time.
I have created a simple java serversocket that listens to port 3306(in myEclipse). Then I try to connect to MySQL using toad. When I connect, the code reaches the break point that I have placed in the serversocket class. I can extract the IP and port number using: System.out.println(s.toString());//where s is the Socket.
Brief code:
ServerSocket ss = new ServerSocket(3306); Socket s = ss.accept();
System.out.println(s.toString());//Socket[addr=/192.0.0.6,port=57915,localport=3306]
What I need is the remaining details that are sent while creating connection like username and password.
How can these be extracted?
The purpose is to monitor the queries and allow only specific ones to go through.
Thanks.
I'm stuck at a homework assignment for my university course.
We are supposed to write a game of Rock-Paper-Scissors using Client and Server, choosing TCP or UDP.
The assignment for the client part is:
"Get the IP-address and port of the server at the beginning and than use this information to connect to the server."
And server:
"The port needs to be set to a number between 10000 and 20000 at the start using command line input."
Now this got me wondering. How is the Client supposed to get the Ip-Adress and port of the server if it is not connected to the server yet?
And normally the client and the server creates a socket and the server listens if a client wants to connect and then accepts the request, making a connection, not the client, like it is requested in the assignment. Isn't it impossible to know the server, if not connected yet?
I got a version working, if the server goes:
// Setting the port via console, making an output: "please enter valid port" and returns the entered port number
ServerTest.port = ServerTest.getPort();
...
ServerSocket testSocket = new ServerSocket(ServerTest.port);
and the client:
private static String host = "localhost";
private static Integer port = 1337;
...
Socket clientSocket = new Socket(ClientTest.host, ClientTest.port);
if I set the port to 1337 when starting the server.
Then I tried something like
//Client
port = ServerTest.getServerPort();
...
Socket clientSocket = new Socket(ClientTest.host, ClientTest.port);
and in the server-class:
public static Integer getServerPort(){
return port;
}
But that throws an "Connection refused"-exception, even if I first the server at first, set the port and than start the client.
Does anyone have an idea how to solve this?
I am making a chat in Java which uses a TCP protocol.
I have a client and a server side.
To send a message to another user, I have to send the message to the server through my client, and the server has to send it to another client.
The server holds the addresses of both online users. When I send a private message, the server finds the ip and a port and creates a socket from them.
The problem is that it doesn’t work correctly.
Here’s the code:
int portNumber = 4444;
String host = "192.168.0.100”;
Socket link;
try {
link = new Socket(host, portNumber);
// Then I set to already created PrintWriter the outputstream
out = new PrintWriter(link.getOutputStream(), true);
} catch (Exception e) {}
// Unfortunately the server freezes here (it doesn't show anything).
How to solve this problem? Where dod I make a mistake?
Thank you in advance.
You shouldn't create a new Socket to send a message. Instead, use a socket of an existing connection.
The sequence should be the following:
Client A connects to the server (server stores the connection as SocketA).
Client B connects to the server (server stores the connection as SocketB).
Server reads a private message from SocketA. The message is addressed to client B.
Server finds the existing socket for client B. It's SocketB.
Server sends the message into SocketB.
I am fist connecting through vpn client then I am able to do telnet and also able to get response when i paste request string on terminal.
Same request if I am trying through java program, I am not getting any response.
I can see using netstat there is established TCP connection when i try through java.
TCP 10.2.2.22:1154 184.23.23.61:7565 ESTABLISHED
Here is the java client code which sends the request.
Socket client = new Socket(serverIp, port);
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
String test = "TUE231363**";
StringBuffer response = new StringBuffer("response : ");
out.write(test.getBytes());
out.flush();
int c;
System.out.println("waiting for response.......>>>>>>>>>>>>>");
while ((c = in.read()) != -1) {
if (isEndOfResponse(c))
break;
System.out.print((char) c);
response.append(c);
}
client.close();
System.out.println(response.toString());
every time after few minutes [5-6 min] it exits without any response.
I am bit new to networking, can anyone suggest what I am missing.
I assume that you are telnetting to the same IP address and port that you are trying to connect to from Java.
The fact that telnet connects, and your client also appears to connect would imply that the server is running, and that it has created an bound a ServerSocket on the right IP / host. It is probably even calling accept properly. But is seems like the server is either not reading the request at all, or it is failing to send a response.
Either way, the problem is most likely on the server side ... and there's not much we can say without seeing the server-side code.