JAVA:java.net.BindException: Address already in use: JVM_Bind [duplicate] - java

This question already has answers here:
How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?
(22 answers)
Closed 5 years ago.
I've wrote these two classes, one for client and the other for server. When I run both of them I get the following error:
java.net.BindException: Address already in use: JVM_Bind...
What is the problem? Also I use TCPview software and there were just two java.exe that use the same port. These two java.exe processes belong to the apps.
Here is the code:
Server Code
/**
*
* #author casinoroyal
*/
public class server {
public static ServerSocket socket1;
public static void main(String[] args) {
try {
socket1 = new ServerSocket(1254);
String request="";
Socket mylink=socket1.accept();
System.out.println("server feels=====");
DataInputStream input= new DataInputStream(mylink.getInputStream());
DataOutputStream output=new DataOutputStream(mylink.getOutputStream());
Scanner chat=new Scanner(System.in);
while(!request.equals("QUIT")){
request=input.readUTF();
output.writeUTF(chat.next());
}
socket1.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Client Code
package javaapplication9;
import java.net.*;
import java.io.*;
import java.util.*;
public class client {
//main
public static void main(String[] args) {
System.out.println("client want to be connected");
try {
Socket mysock = new Socket(InetAddress.getLocalHost(),1254);
System.out.println("client has been connected");
DataInputStream input = new DataInputStream(mysock.getInputStream());
DataOutputStream output = new DataOutputStream(mysock.getOutputStream());
String reque="";
Scanner scan1=new Scanner(System.in);
String sendmsg=scan1.next();
while(!reque.equals("QUIT")){
output.writeUTF (sendmsg);
reque=input.readUTF();
}
mysock.close();
} catch (IOException ex) {
System.out.println("client rejected"+ex);
}
}
}

What is the problem? Also I use TCPview software and there were just two java.exe that use the same port. These two java.exe processes belong to the apps.
Here is your problem.
You tried to bind 2 sockets at the same port of your computer, and you can't bind 2 sockets at the same port on the same computer.
It's probably because you had an existing process that is listening at the port 1254 ( probably an instance of your server app ), and you tried to run your server app which tried to bind also at the port 1254

Related

Java Client Server Socket Error: java.net.BindException: Address already in use: bind

This is the server side code
I'm pretty sure the error is that the Server Socket Isn't closing the port?
There is no problem when I run the program but, When I run it again I get this Error:
Exception in thread "main" java.net.BindException: Address already in use: bind
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:550)
at java.base/sun.nio.ch.Net.bind(Net.java:539)
at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:643)
at java.base/java.net.ServerSocket.bind(ServerSocket.java:396)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:282)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:173)
at internet.server.main(server.java:11)
But if I change the port number it fixes for that one time running. Someone please help also I haven't learned java server sockets or client sockets yet soo I need help as far as I know no one else is having this problem
package internet;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class server {
public static void main(String[] args) throws IOException {
println("Compiled");
ServerSocket ss = new ServerSocket(5502);
Socket s = ss.accept();
println("Client Connected!");
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader bf = new BufferedReader(in);
String str = bf.readLine();
println("Client: " + str);
s.close();
ss.close();
}
public static void println(String args) {
System.out.println(args);
}
}
This will works fine as long as you terminate your previous execution before you re run it.
Try closing those streams, InputStreamReader and BufferedReader as well. Close them before socket and ServerSocket.
bf.close();
in.close();

How to run a server-client application in JAVA [duplicate]

This question already has answers here:
Exception in thread "main" java.net.BindException: Address already in use - Error in Netbeans only
(6 answers)
Closed 7 years ago.
I am trying to run the following code by first running the chatserver file and then the chatclient file.
import java.net.*;
import java.io.*;
public class chatserver
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(2000);
Socket sk=ss.accept();
BufferedReader cin=newBufferedReader(newInputStreamReader(sk.getInputStream()));
PrintStream cout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
s=cin.readLine();
if (s.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"\n");
System.out.print("Server : ");
s=stdin.readLine();
cout.println(s);
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}
public class chatclient
{
public static void main(String args[]) throws Exception
{
Socket sk=new Socket("192.168.0.19",2000);
BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}
But it is not working. What are the correct procedure/steps to run a this kind of application?
Running the server file gives the following error.
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at javaapplication1.chatserver.main(chatserver.java:19)
Java Result: 1
Address already in use means some program is already listening on port 2000 (most likely the same program that you didn't shutdown properly).
Try out a different port.
You may use the netstat command on your command line to see which ports are currently in use.
Here is your working example:
// Server
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public Server() {
// TODO Auto-generated constructor stub
}
public static void main(String args[]) throws Exception
{
System.out.println("Server started");
ServerSocket ss=new ServerSocket(8888);
System.out.println("Server waiting for connection");
Socket sk=ss.accept();
System.out.println("Server Connected");
BufferedReader cin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream cout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
s=cin.readLine();
if (s.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"\n");
System.out.print("Server : ");
s=stdin.readLine();
cout.println(s);
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}
// Client
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Client {
public Client() {
// TODO Auto-generated constructor stub
}
public static void main(String args[]) throws Exception
{
System.out.println("Client started");
Socket sk=new Socket("localhost",8888);
System.out.println("Client Connected");
BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while ( true )
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}
Address already in use: JVM_Bind
Java application is trying to connect on port 2000 but that port is already used by some other process and JVM Bind to that particular port 2000, is failed. Now to fix this error you need to find out which process is listening of port 2000
Windows users:
In Command Prompt, send command "netstat -ao". You can get network information for all processes.
Find out the one using port 2000, get the PID.
Find out the process with the PID you just got from windows task manager and shut it down. (By default the Task Manager doesn't show the PID. You have to add it from the menu View | Select columns)
Restart Server.
Restart the application you've just shutdown.
Unix users:
Most Unix systems have the built-in fuser command that returns the process which is engaging a port:
fuser -v -n (tcp | udp) <port#>
but if you can not kill that process than you need to change your web-server configuration or eclipse configuration to listen on different port. In case of tomcat you can change it on connector section of server.xml and in case of eclipse you can see here setting up Eclipse for Java remote debugging.
Change the port number you are using to run your application.
For more check this link- http://www.mastertheboss.com/jboss-server/jboss-configuration/solving-javanetbindexception-address-already-in-use-jvmbind
your port 2000 is already in use by another process.
try using the netstat command on your commandline to see which port are in use.
I usually use 5 digit ports like 10999 which is uncommon.
You are using the same port for both client and server: 2000, that's why starting client fails because only one process can use a socket, so if server takes it then client can't.

java.net.BindException: Permission denied when creating a ServerSocket on Mac OSX

I Tried to run a Java socket in mac with eclipse but it doesn't work. I got this error:
Exception in thread "main" java.net.BindException: Permission denied
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:521)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:414)
at java.net.ServerSocket.bind(ServerSocket.java:326)
at java.net.ServerSocket.<init>(ServerSocket.java:192)
at java.net.ServerSocket.<init>(ServerSocket.java:104)
at server.MessageServer.main(MessageServer.java:11)
How can i make it to run?
package server; //ChatServer
import java.io.*;
import java.net.*;
public class MessageServer {
public static void main (String args[]) throws IOException {
int port = 100;
ServerSocket server = new ServerSocket (port);
System.out.println("Server is started!");
while (true) {
Socket client = server.accept ();
System.out.println ("Accepted from " + client.getInetAddress ());
MessageHandler handler = new MessageHandler (client);
handler.start();
}
}
}
You can't open a port below 1024, if you don't have root privileges and from the code you posted in your comment, you seem to be trying to open port 100 which confirms my theory.
You need to use a port which is higher than 1024, if you're running the code under a non-root user.
Unix-based systems declare ports < 1024 as "privileged" and you need admin rights to start a server.
For testing, use a port number >= 1024.
When deploying the server in production, run it with admin rights.
I had the same issue and my port numbers were below 1024 changing port number to above 1024 solved my problem. Ports below 1024 are called Privileged Ports and in Linux (and most UNIX flavors and UNIX-like systems), they are not allowed to be opened by any non-root user.
Many systems declare ports that are less than 1024 as "admin rights" ports. Meaning, if you're only using this for basic testing use a higher port such as 2000. This will clear the exception that you're getting by running your current program.
int port = 100;
ServerSocket server = new ServerSocket (port);
Change that to something such as:
int port = 2000;
ServerSocket server = new ServerSocket (port);
MyServer.java
import java.io.*;
import java.net.*;
public class MyServer
{
ServerSocket ss;
Socket s;
DataOutputStream dos;
DataInputStream dis;
public MyServer()
{
try
{
System.out.println("Server Started ");
ss=new ServerSocket(4444);
s=ss.accept();
System.out.println(s);
System.out.println("Client Connected");
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
ServerChat();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String arg[])
{
new MyServer();
}
public void ServerChat()throws IOException
{
String str;
do
{
str=dis.readUTF();
System.out.println("Client msg : "+str);
dos.writeUTF("Hello "+str);
dos.flush();
}while(!str.equals("stop"));
}
}
MyClient.java
import java.io.*;
import java.net.*;
public class MyClient
{
Socket s;
DataInputStream din;
DataOutputStream dout;
public MyClient()
{
try
{
s=new Socket("localhost",4444);
System.out.println(s);
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
ClientChat();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void ClientChat() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1;
do
{
s1=br.readLine();
dout.writeUTF(s1);
dout.flush();
System.out.println("Server Msg : "+din.readUTF());
}while(!s1.equals("stop"));
}
public static void main(String arg[])
{
new MyClient();
}
}
Run Server program with root (Administrator).
Windows: Run as Administrator the IDE/Editor.
Ubuntu/macOS: sudo java...
This is an old question, and I might be replying too late, but I would like to anyways share my experience in case anyone hits the issue.
I was using port# 8000, but still unable to bind to the port from a java program. It was network filter running as part of eset endpoint security that was blocking the connection.
I added a rule in eset firewall to allow port 8000, and it started working.

How to connect 2 different computers on a network using a chat application in Java?

I have a simple pair of client and server programs. Client connects to server and when it does connect, the server replies with a "Hello there" message. How should I modify the program if I want the client and server programs to run on different systems?
Here is the code for the client side..
package practice;
import java.io.*;
import java.net.*;
public class DailyAdviceClient
{
public static void main(String args[])
{
DailyAdviceClient dac = new DailyAdviceClient();
dac.go();
}
public void go()
{
try
{
Socket incoming = new Socket("127.0.0.1",4242);
InputStreamReader stream = new InputStreamReader(incoming.getInputStream());
BufferedReader reader = new BufferedReader(stream);
String advice = reader.readLine();
reader.close();
System.out.println("Today's advice is "+advice);
}
catch(Exception e)
{
System.out.println("Client Side Error");
}
}
}
and here is the code for the server
package practice;
import java.io.*;
import java.net.*;
public class DailyAdviceServer
{
public static void main(String args[])
{
DailyAdviceServer das = new DailyAdviceServer();
das.go();
}
public void go()
{
try
{
ServerSocket serversock = new ServerSocket(4242);
while(true)
{
Socket outgoing = serversock.accept();
PrintWriter writer = new PrintWriter(outgoing.getOutputStream());
writer.println("Hello there");
writer.close();
}
}
catch(Exception e)
{
System.out.println("Server Side Problem");
}
}
}
just change "127.0.0.1" on the client with the server's IP and make sure the port 4242 is open.
Socket incoming = new Socket("127.0.0.1",4242);
This is creating a socket listening to the server at the address 127.0.0.1 on port 4242. If you change the server to another address, for example of a different pc, then change the ip address that your socket is listening to.
It is also worth noting that you will probably have to open up or allow access to the ports you are using.
Client requires ip address and port of server, means ip of that system which you making server and port (4242).so in client you need to change
Socket incoming = new Socket("127.0.0.1",4242); BY
Socket incoming = new Socket("IP address of server",4242);
And make sure both system is connected via wired or wireless network.

muilti threading in java [duplicate]

This question already has an answer here:
Java server multithreading [closed]
(1 answer)
Closed 9 years ago.
so what i am trying to do is write a server and a client. the server should listen for a connection and service that connection on a differ thread and keep listening for more client. once connected the client will send over the IP it wished the server to resolve. the server sould write this back the the client. this is what i got so far... how do i make the server better. and how to write the client
How do you write the client file to connect to this and send it the ip they want.
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable {
Socket csocket;
Server(Socket csocket) {
this.csocket = csocket;
}
public static void main(String args[])
throws Exception {
ServerSocket ssock = new ServerSocket(6053);
System.out.println("Listening");
while (true) {
Socket sock = ssock.accept();
System.out.println("Connected");
new Thread(new Server(sock)).start();
}
}
public void run() {
try {
PrintStream pstream = new PrintStream
(csocket.getOutputStream());
pstream.close();
csocket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
what you can do is make it for "user friendly", i have done this thing before and made a successful IM program.
ideas:
make it able to get global IP
display a handshake (if connected = true){ system.out.print( has connected!);}

Categories