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);
}
}
}