ServerSocket and Socket

2

I'm trying to create a ServerSocket :

public class RunServer {

public static void main(String[] args) {
    try {

        byte[] buffer = new byte[1024];
        String passwordCript;

        ServerSocket socketRecepcao = new ServerSocket(22300);
        System.out.println("Servidor esperando conexão na porta 22300");

        while (true) {

            Socket socketConexao = socketRecepcao.accept();
            System.out.println("Conexão estabelecida na porta "
                    + socketConexao.getPort());

            InputStream is = socketConexao.getInputStream();
            OutputStream os = socketConexao.getOutputStream();

            is.read(buffer);

            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(buffer, 0, buffer.length);

            BigInteger hash = new BigInteger(1, md.digest());
            passwordCript = hash.toString(16);

            buffer = passwordCript.getBytes();
            os.write(buffer);

            socketConexao.close();
        }

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
}

NOTE: This is a simple code to create ServerSocket ,

But I get this exception:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at com.iamExport.RunServer.main(RunServer.java:20)
  • Why is this exception being thrown?
  • What care should I take after ServerSocket works?
  • NOTE: I know there are other connection types like API HttpClient , etc., but I'm studying socket ...

        
    asked by anonymous 02.04.2014 / 19:48

    1 answer

    5

    Basically, there is something listening on this door (22300). Possibly your own program, running in background - take a look at the java processes, make sure you kill everyone, and try again.

        
    02.04.2014 / 19:50