Application java with socket - java

I have a problem with the socket in java: I have a server on the desktop PC that I've a ServerSocket object Instantiated such:
ServerSocket socket = new ServerSocket(port);
where "port" is a number of port.
This serverSocket accept a client. This client is another application java that runs on another desktop PC that connected to the same net of the server PC. In this application java, I Instantiated a Socket object which connects with local IP of server PC (example: 192.168.1.11) and port of serverSocket. When I run the server and the client, the client does not connect to the server. If I run the server and the client on the same desktop PC, the client manages to connect to the server.
I was thinking of a problem with the firewall configuration but I did not manage to solve it. Help me please.
This is code of server:
//I will accept one client
ServerSocket server = new ServerSocket(19999);
Socket client = server.accept();
new Thread(new ManagerConnectionThread(client)).start(); //this thread manage the requests
ManagerConnectionThread is a class that manage the communication between server and client:
//This code is on the server
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
This is code of client:
public static void main(String[] args){
Socket client = new Socket(InetAddress.getByName("192.168.1.11"), 19999); //192.168.1.11 is IP of "server" pc
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}

do you have a router involved? if yes you will need to configure some port-forwarding and try to access via the external address, meaning that you will try to connect through the router that will point to your machine.
Also, you might need to do some tweaking with the firewall, at some point I was doing something similar for school, but as a server, I was using a Linux based OS so the procedure might be different, but I definitely needed to allow in/out for the port that I was using.
you might find some answers in this post which is similar
Java Chat with TCP/IP over LAN
Edit: I found my old homework, and I have definitely used the external address (especially because I wanted to access it from school, while the server was home) so I would connect to my external address of the laptop and then in the router's settings I would forward the port that I was using to the server, in the server allowing incoming traffic for the specific port.

Related

How can I connect a client to a server in Java using Socket

I am trying to write a client-server app that connects a PC as a client to my PC as a server.
When I enter 127.0.0.1 as server IP in client side, in my PC, it works properly so it's not a coding problem. Also when I enter my IP (got it from nslookup command in kali) and connect to internet, client connects to server properly.
But when I open my client app in other PC and server app in my PC, the client side a "Connection Time out" Exception will be thrown.
I have tried turning off the firewall in the client side (Windows 10) but not from the server side.
Here are my codes:
Server:
ServerSocket server = new ServerSocket(SERVER_PORT);
Socket client = server.accept();
//Some codes
Client:
Socket server = new Socket(SERVER_IP, SERVER_PORT);
For better answer we need more informations from you. But from this what you post, this is best answer i can make. You didn't explain what is your goal, do you want to make connection inside local network or you would like to make connection to 'outside world', over internet.
If you like to make two PCs communicate inside local network, than you must make sure they are on same network, or more accurate, that they are connected to same router. You have to set your server IP address to IP address on your local network, that would probably be something like 192.168.xxx.xxx
If you want to communicate over internet, i suggest test in local network first, as described before. If it works on local network, then you have to deal with firewalls, router setting,etc to make it work on the internet too.
Take look at this too
Use Socket-communication over different networks

Trying to create a socket with public IP , java without port forwarding?

I am trying to open a socket with my public IP using Java.
I have a server program and a client program, and I can connect them both using localhost. I also portforwarded my PC on a port, and using this port I can connect the programs.
However, is it possible to connect them without opening ports?
I am developing this IP chat program, and it's quite inconvenient to have all users open their ports.
Server.java contains:
ServerSocket Server = new ServerSocket(Port number);
Client.java contains:
try {
Socket clientSocket = new Socket(public_ip, Port number);
} catch (Exception e) {
e.printStackTrace();
}
I am grateful for any assistance, I'm very new to sockets!
Inbound connections are blocked by default, this is done due to security reasons. Outbound connections aren't normally blocked. So the best solution is to have a server. Clients will connect to it without problems (outbound connections are allowed), and the server will transfer data to clients.
In case you're curios how the server will be able to deliver messages to the clients using connection, outbound for client - the client keeps asking server if there are new messages, and if so return them in response.

Java Client Server Application - Address already in use: connect

I am currently working on a simple multiplayer game where serveral clients need to connect to a server.
My server consits of a single serverSocket. This serverSocket accepts incoming connections and hands them over to connection object that starts a separate thread.
ServerSocket seso = new ServerSocket(12345);
while(true){
Socket toClient = seso.accept();
new Connection(toClient); //creates a thread that opens streams etc
}
Clients open a new Socket and connect to this server.
Socket toServer = new Socket();
toServer.setReuseAddress(true);
toServer.bind(new InetSocketAddress(65432)); //always using the same port
toServer.connect(new InetSocketAddress(serverIP,12345));
Now if i close the connection to the server using toServer.close(); and try to connect again to the server, i get an "address already in use: connect" exception.
Using TCPView i can see that the state of the client procress changes to TIME_WAIT. But shouldn't i be able to use this port again because of setReuseAddress(true)? Am i using it wrong or is it an server problem?
I do always call .close() on toClient and toServer. Nevertheless i always have to wait until the socket is completely closed (after TIME_WAIT) before this client can connect again to the server.
When i close the entire application, the socket is immediately closed (not in state TIME_WAIT) and this client can connect to my server. (And ofc there is a connection reset exception in my server)
How can I do that without always closing the application ?
Thanks for your help.
To expand on my comment, a client / server protocol requires the server to listen on a port known to or discoverable by the client -- that can be considered the definition of "server" -- but it does not ordinarily require clients to connect from a specific port. If you do not bind the client socket to a particular port, then the underlying system will choose an available (source) port automatically and transparently.
If the server depends for some reason on clients connecting from a particular port, then you should re-evaluate that aspect of your design. If it does not, then you are making your own trouble by having clients connect that way. This should be all you need to do:
Socket toServer = new Socket();
toServer.connect(new InetSocketAddress(serverIP, 12345));

Why can not I create tcp socket from Android device to PC

I'm trying to connect my Android app to desktop server. The problem is when I try to create LAN via portable Wi-Fi hotspot on my smartphone everything works fine, but when I connect PC and phone to the router I have TimeOutException creating a socket in Android app. On the other hand, when I connect desktop client to desktop server via router it works fine too, so the problem occures only when I try to connect mobile client to desktop server via router.
Client code (Java):
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000); // TimeOutException
Server code (C++/Qt):
QTcpServer m_tcp_server;
// ...
m_tcp_server->listen(QHostAddress::Any, m_port);
ip address and port are right (100%)
UPD:
Thank you, guys.
It was just a router problem. I tested in on the another one and there is no problem
try check which ip you have assigned on android pohone and if you have route to the destination server ip.
Is possilbe you get different ip on wifi and different ip by cable.
Also you can try an android net tool to try connect to the server.
I had same problem, In my case i created server socket with wrong ip address.
Hotspot network band is 192.168.43.1~255
You need to create server socket with ip address that hotspot dhcp made like 192.168.43.11
I had wrongfully made server socket with ip 192.168.0.22, this is wrong.
And your android app should open client socket with it.

How to make Java Programs communicate on the internet? [duplicate]

This question already exists:
How make every remote ip address connect to the Java RMI's server?
Closed 7 years ago.
I am developing a client/server application using Sockets and here is the basic code for the client:
Socket sock = new Socket("serversip",9999);
PrintStream pr = new PrintStream(sock.getOutputStream());
And here's for the server:
ServerSocket ser = new ServerSocket(9999);
Socket sock = ser.accept();
BufferedReader ed = new BufferedReader(new InputStreamReader(sock.getInputStream()));
Whenever, the client and the server applications are on the same network, this works absolutely flawlessly(i use Inet.getLocalhost() and supply the servers's ip to the client manually)
But when they are on different networks(i.e. Internet) and I use:
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ipp = in.readLine();
to obtain the server's public ip address and provide it manually to the client it just won't work.
It always says:
java.net.ConnectException: Connection timed out: connect
I have used Java RMI before and it too gave the same result i.e. not working on the internet, just working on private network.
So, how am I supposed to establish a connection between remote java programs on the internet, is it even possible?? It makes me wonder then how all these chat applications,etc we use work.
Please help me!! I would be grateful to anyone who reads my problem and at least provides a response. Thank you for reading!
checkip returns the server's public IP, but it is likely that the server is behind a NAT. In that case the server is part of a Local Area Network and it has a local IP address (such as 192.168.1.2). You will have to map the port 9999 on the NAT device (e.g. the router provided by the ISP in a residential connection) to the same port on the server at 192.168.1.2. Some ISP manage the NAT itself and don't allow them to be configured by the users.
Another point to consider are firewalls. There is a (software) firewall running on the server itself (such as the Windows Firewall, or iptables in Linux), as well as there may be a firewall on the client side that is blocking the outgoing connections to port 9999. In the case of residential connections, the ISP may also be blocking traffic on that port. In corporate networks, outgoing connections to arbitrary ports are sometimes blocked too.

Categories