简体   繁体   中英

how to close the client socket in java

I have opened a server socket port in a new thread and connected the client to that port.i wish to close the client socket after it has completed its work.(and i know there is some problem in my while loop of the run() method but i am unable to understand it. when i run my server application to open a port and listen to a connection it gives an error saying java.net.BindException: Address already in use: JVM_Bind

please tell me what i am doing wrong here in detail and give a theoratical solution for it in detail . It would be really appriciated.please forgive me if its a dumb question

Jshirwani

server

import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.*;

public class TryThreads extends Thread 
{
    private int Portnumber;
    private static String inputLine;

    public TryThreads(int portNumber)

    {

        Portnumber = portNumber; 

        setDaemon(false);

    }

    public static void main(String[] args)

    {

        //create three threads

        Thread first = new TryThreads(63400);

        Thread second = new TryThreads(63401);


        first.start();

        second.start();

        //third.start();

        System.out.println("ending main");

        return;


    }

    public void run()

    {
        //System.out.println("one socket port opened");

        try

        {

            System.out.println("one socket port opened");

            System.out.println("one socket port opened");

            while (true)

            {

                ServerSocket serverSocket = new ServerSocket(Portnumber);

                System.out.println("ending main2");



            //System.out.println("one socket port opened");

            Socket clientSocket = serverSocket.accept();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            while((inputLine = bufferedReader.readLine()) != null)

                 System.out.println(inputLine);

            }




        }

        catch(IOException e)

        {

            System.out.println(e);

        }

    }

}

client

import java.io.*;
import java.net.Socket;
public class client 
{

    private static PrintWriter printWriter;
    public static void main(String[] args) 
    {
        BufferedReader in = null;

        try
        {
            Socket socket = new Socket("localhost",63400);
            printWriter = new PrintWriter(socket.getOutputStream(),true);
            printWriter.println("Hello Socket");
            printWriter.println("EYYYYYAAAAAAAA!!!!");

            socket.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

You need to change the loop so that you create the server socket outside the loop. You can open only one socket per port. After you have it opened you can accept as many connections on that socket as you want.

ServerSocket serverSocket = new ServerSocket(Portnumber);
while (true) {
    System.out.println("ending main2");

    //System.out.println("one socket port opened");

    Socket clientSocket = serverSocket.
        BufferedReader bufferedReader = new BufferedReader(new 
            InputStreamReader(clientSocket.getInputStream()));

    while((inputLine = bufferedReader.readLine()) != null)
        System.out.println(inputLine);

}

Update with clarifications:

The exception you describe means that there is already a connection opened on that port, it can be from your application (you are opening a socket with the same port in a loop) or it can be from a different application which is just listening on a port (any service running, bittorrent, skype, ...)

java.net.BindException - description:

Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned.

Other reasons for that exception could include that you don't have high enough permissions, however this is unlikely since you use such a high port number.

If you can'T guarantee, that the port you want to use will be open, than you should have a loop and as long as you'll keep getting the java.net.BindException, you should keep trying the following ports ( port + 1 ). This way you will eventually find an empty one which you are allowed to use. You can also not specify any port than a random available port will be chosen for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM