Chani
Chani

Reputation: 5175

Trying to get a java socket program to work, but getting "java.net.BindException: Address already in use 6666 "

Here is the code

I have written a server and client. But when i run them, (as you can see in the last program), I get the following error:

Whoop s! java.net.BindException: Address already in use 6666 6666 is the port no. i specified.

import java.net.*;
import java.io.*;
import java.lang.*;

public class processSendHelper
{
    Process p;
    String address;
    int port;
    long msg_data;

    processSendHelper(int pid, int current_round, long address, long msg_data, int port)
    {
        try
        {
            ServerSocket sSoc = new ServerSocket(port);

                Socket inSoc = sSoc.accept();
                msg_Thread msgT = new msg_Thread(inSoc, msg_data);
                msgT.start();
                Thread.sleep(5000);
                sSoc.close();

        }
        catch(Exception e)
        {
            System.out.println("Whoop  s! " + e.toString());
        }

    }

}

/* sends out (or rather just makes available) the provided msg 
 * */
class msg_Thread extends Thread 
{

    Socket threadSoc;
    long msg_data;

    msg_Thread (Socket inSoc, long msg_data) 
    {
        threadSoc = inSoc;
        this.msg_data = msg_data;
    }

    public void run()
    {
        try 
        {
              PrintStream SocOut = new
              PrintStream(threadSoc.getOutputStream());
              SocOut.println(msg_data);
        }

        catch (Exception e)
        {
            System.out.println("Whoops!" + e.toString());
        }

        try 
        {
            threadSoc.close();
        }
        catch (Exception e) 
        {
            System.out.println("Oh no! " +
            e.toString());
        }
    }
}





import java.net.*;
import java.io.*;


public class processReceiveHelper 
{
        Socket appSoc;
        BufferedReader in;
        String message;
        String host;
        int port;

        processReceiveHelper(String host,int port) 
        {
            try 
            {
                appSoc = new Socket(host,port);
                in = new BufferedReader(new InputStreamReader(appSoc.getInputStream()));
                message = in.readLine();
                System.out.println(message); 
                    /* Tokenizer code comes here 
                     * Alongwith the code for 
                     * updating the process object's
                     * data
                     * */

            }
            catch (Exception e) 
            {
                System.out.println("Died... " +
                e.toString());
            }

        }
}




public class Orchestrator 
{



    public static void main(String[] args)
    {
        processSendHelper psh = new processSendHelper(1, 2, 1237644, 6666, 2002);
        processReceiveHelper prh = new processReceiveHelper("localhost", 2002);

    }
}

EDIT:

I found the problem. The reason was that i was running both the server and client from the same main program.

the following worked:

enter image description here

Upvotes: 0

Views: 371

Answers (3)

Bartosz Moczulski
Bartosz Moczulski

Reputation: 1239

Does it happen when you run the program for the second time? You may want to setReuseAddress(true) on this socket.

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

That means that the port 6666 is already being used. There are two main causes/solutions for this:

  • Some other program is using that port. Solution: Choose a different port.
  • Your old Java program is hanging and still "using" that port. Close all of your hanging Java programs and try again. If that doesn't solve your problem, choose a different port.

Upvotes: 1

christophmccann
christophmccann

Reputation: 4211

That means there is already an application operating on port 6666 preventing your Java application using it. However, it is equally possible there is a running process of your Java application still holding onto 6666. Terminate any running java processes and try re-running the code - if it still fails then you have some other application using 6666 and you would be better using a different port.

Upvotes: 2

Related Questions