java.net.BindException: Address already in use: JVM_Bind

My server class is completed. When I run it...

It gives me an error message

java.net.BindException: Address already in use: JVM_Bind

I am having a localhost:81 ...

I haven't assign any port 81 ...neither in router nor the IIS ...

May I know what should I do to remove this message ?

Thanks

[334 byte] By [BB81a] at [2007-11-26 16:47:08]
# 1
maybe you're opening more than once that port... post your code when you start the (i'm assuming it) ServerSocket
Ruly-o_Oa at 2007-7-8 23:14:35 > top of Java-index,Archived Forums,Socket Programming...
# 2

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.concurrent.*;

public class PxyServer{

protected String request;

protected String response;

protected static ExecutorService pool;

File file;

intsize;

byte[] data = new byte[1000];

protected ServerSocket welcomeSocket;

protected Socket connectionSocket;

protected InputStream inFromClient;

protected InputStream inFromWeb;

protected OutputStream outToClient;

private OutputStream outToWeb;

private FileInputStream fin;

int clientPort;

public static void main(String argv[]) throws Exception{

PxyServer myServer = new PxyServer();

HttpRequest request = new HttpRequest();

pool.submit(new Thread(request));

}

protected void checkSite(){//will check if the site is in black list or not

int index = 0;//error check for tokenizing (host: )

String urlToCheck = new String();

StringTokenizer tokens = new StringTokenizer(request);

while(tokens.hasMoreTokens())//read host from http request

{

urlToCheck = tokens.nextToken();

if(urlToCheck.equalsIgnoreCase("Host:"))

{

urlToCheck = tokens.nextToken();

index = -1;

break;

}

}

if(index != -1)

{

System.out.println("Host name could not be found");

System.exit(-1);

}

System.out.println("url is:" + urlToCheck);

System.out.println("length is:" + urlToCheck.length());

getPage(request, urlToCheck);

}

private void getPage(String request, String url)

{

try{

Socket webSocket = new Socket(url, 80);

outToWeb = webSocket.getOutputStream();

inFromWeb = webSocket.getInputStream();

int outToWebPort = webSocket.getLocalPort();

outToWeb.write(request.getBytes());//make request from web server

outToWeb.flush();

do{//get response and forward it to browser

size = inFromWeb.read(data);

if(size < 1)

{

System.out.println("size < 0" + size);

break;

}

response = new String(data, 0, size, "ISO-8859-1");

System.out.println(response);

outToClient.write(response.getBytes());

}while(size > 1);

}

catch(IOException e){

e.printStackTrace();

}

}

public PxyServer(){

try{

welcomeSocket = new ServerSocket(8080);

}catch(IOException e){

System.out.println(e);

}

pool = Executors.newFixedThreadPool(1000);

}

}

and HttpRequest class is:

import java.io.IOException;

import java.net.*;

public class HttpRequest extends PxyServer implements Runnable{

public HttpRequest(){

try{

connectionSocket = null;

inFromClient = null;

outToClient = null;

}catch(Exception e){

e.printStackTrace();

}

}

public void run(){

while(true){

try {

Thread.sleep(500);//sleep 500ms for other threads can run

} catch (InterruptedException e) {

e.printStackTrace();

}

try{

connectionSocket = welcomeSocket.accept();

inFromClient = connectionSocket.getInputStream();

outToClient = connectionSocket.getOutputStream();

clientPort = connectionSocket.getPort();

System.out.println("local port: " + clientPort);

size = inFromClient.read(data);

request = new String(data, 0, size);

System.out.println("FROM CLIENT: " + "\n" + request);

}catch(IOException e){

System.out.println(e);

}

checkSite();

}

}

}

here it gives the same error. i open a socket (as server socket), listen port 8080 as a proxy server. then i parse http request and read url. after reading url, i open one more socket to the host, and forward the http request. after that, i read the response from web, and forward it to the browser.

while reading a url, i must make a request for each object. so i tried to do it with thread, but i could not achieve. how can do that?

SevenHilla at 2007-7-8 23:14:35 > top of Java-index,Archived Forums,Socket Programming...
# 3
Start your own thread, rather than hijacking this one, and when you post code, use code tags (the "code" button above the box where you enter your text).
masijade.a at 2007-7-8 23:14:35 > top of Java-index,Archived Forums,Socket Programming...
# 4

these are my own code. at the bottom of the code, i explained what i want to do. sorry for the previous one, this is the code again.

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.concurrent.*;

public class PxyServer{

protected String request;

protected String response;

protected static ExecutorService pool;

File file;

intsize;

byte[] data = new byte[1000];

protected ServerSocket welcomeSocket;

protected Socket connectionSocket;

protected InputStream inFromClient;

protected InputStream inFromWeb;

protected OutputStream outToClient;

private OutputStream outToWeb;

private FileInputStream fin;

int clientPort;

public static void main(String argv[]) throws Exception{

PxyServer myServer = new PxyServer();

HttpRequest request = new HttpRequest();

pool.submit(new Thread(request));

}

protected void checkSite(){//will check if the site is in black list or not

int index = 0;//error check for tokenizing (host: )

String urlToCheck = new String();

StringTokenizer tokens = new StringTokenizer(request);

while(tokens.hasMoreTokens())//read host from http request

{

urlToCheck = tokens.nextToken();

if(urlToCheck.equalsIgnoreCase("Host:"))

{

urlToCheck = tokens.nextToken();

index = -1;

break;

}

}

if(index != -1)

{

System.out.println("Host name could not be found");

System.exit(-1);

}

System.out.println("url is:" + urlToCheck);

System.out.println("length is:" + urlToCheck.length());

getPage(request, urlToCheck);

}

private void getPage(String request, String url)

{

try{

Socket webSocket = new Socket(url, 80);

outToWeb = webSocket.getOutputStream();

inFromWeb = webSocket.getInputStream();

int outToWebPort = webSocket.getLocalPort();

outToWeb.write(request.getBytes());//make request from web server

outToWeb.flush();

do{//get response and forward it to browser

size = inFromWeb.read(data);

if(size < 1)

{

System.out.println("size < 0" + size);

break;

}

response = new String(data, 0, size, "ISO-8859-1");

System.out.println(response);

outToClient.write(response.getBytes());

}while(size > 1);

}

catch(IOException e){

e.printStackTrace();

}

}

public PxyServer(){

try{

welcomeSocket = new ServerSocket(8080);

}catch(IOException e){

System.out.println(e);

}

pool = Executors.newFixedThreadPool(1000);

}

import java.io.IOException;

import java.net.*;

public class HttpRequest extends PxyServer implements Runnable{

public HttpRequest(){

try{

connectionSocket = null;

inFromClient = null;

outToClient = null;

}catch(Exception e){

e.printStackTrace();

}

}

public void run(){

while(true){

try {

Thread.sleep(500);//sleep 500ms for other threads can run

} catch (InterruptedException e) {

e.printStackTrace();

}

try{

connectionSocket = welcomeSocket.accept();

inFromClient = connectionSocket.getInputStream();

outToClient = connectionSocket.getOutputStream();

clientPort = connectionSocket.getPort();

System.out.println("local port: " + clientPort);

size = inFromClient.read(data);

request = new String(data, 0, size);

System.out.println("FROM CLIENT: " + "\n" + request);

}catch(IOException e){

System.out.println(e);

}

checkSite();

}

}

}

SevenHilla at 2007-7-8 23:14:35 > top of Java-index,Archived Forums,Socket Programming...
# 5
Start your own thread, and when you do, provide the full text and stack trace of any exception(s) you are getting.
ejpa at 2007-7-8 23:14:35 > top of Java-index,Archived Forums,Socket Programming...