Lakaj
09-02-2007, 15:59
Ciao ragazzi, ho nuovamente problemi...
NetBeans 5.5 sputa fuori queste eccezioni, in un applicativo che usa java.net.*
java.net.BindException: Permission denied
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at simserver.Interf1.<init>(Interf1.java:30)
at simserver.Main.main(Main.java:27)
Ora non so se è un fatto di porte non nattate del router dannazione...
Il codice è questo:
/*
* Main.java
*
* Created on 9 febbraio 2007, 9.50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import java.io.*;
/**
*
* @author ale
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Interf1 interf=new Interf1(900);
interf.setVisible(true);
}
}
/*
* Mserver.java
*
* Created on 9 febbraio 2007, 9.50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* @author ale
*/
public class Mserver extends JFrame implements Runnable {
protected Socket client;
private JLabel notifica;
Thread thr;
/** Creates a new instance of Mserver */
public Mserver(Socket skt) {
super(skt.getInetAddress().toString());
this.setVisible(true);
notifica=new JLabel("Arrivato nuovo client da "+ skt.getInetAddress());
client=skt;
}
public void init() {
thr=new Thread(this);
getContentPane().add(notifica);
pack();
}
public void start() {
thr.start();
}
public void run() {
try {
InputStream i=client.getInputStream();
OutputStream o=client.getOutputStream();
PrintStream p=new PrintStream(o);
p.println("Benvenuti");
p.println("Digitare HELP per lista servizi");
int x;
ByteArrayOutputStream command=new ByteArrayOutputStream();
String helpcommand=new String("HELP");
String quitcommand=new String("QUIT");
while((x=i.read())>-1) {
o.write(x);
if(x==13) {
p.println();
if(helpcommand.equalsIgnoreCase(command.toString()))
p.println("Il solo servizio disponibile è l'help");
else if(quitcommand.equalsIgnoreCase(command.toString())) {
p.println("BYE.");
try {
Thread.sleep(1000);
}
finally {
break;
}
}
else
p.println("Comando non disponibile.");
command.reset();
}
else if(x!=10)
command.write(x);
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
notifica.setText("Connessione chiusa con " + client.getInetAddress());
try {
client.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
/*
* Interf1.java
*
* Created on 9 febbraio 2007, 10.42
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* @author ale
*/
public class Interf1 extends JFrame {
private JLabel message;
private JLabel attesa;
/** Creates a new instance of Interf1 */
public Interf1(int port) {
message=new JLabel("Server in partenza sulla porta " + port);
getContentPane().add(message);
pack();
try {
ServerSocket svr=new ServerSocket(port);
message.setText("Server partito sulla porta " + svr.getLocalPort());
Socket client;
attesa=new JLabel("");
getContentPane().add(attesa);
while(true) {
client=svr.accept();
attesa.setText("Richiesta di connessione da " + client.getInetAddress());
(new Mserver(client)).start();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Si tratta di un demone in ascolto su una determinata porta, che accetta connessioni in multithread, io credo che il codice sia giusto l ho guardato 1000 volte :(
Provato con altre porte diverse da 900 ma mi da lo stesso risultato, sia con windows che con Linux :(
NetBeans 5.5 sputa fuori queste eccezioni, in un applicativo che usa java.net.*
java.net.BindException: Permission denied
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at simserver.Interf1.<init>(Interf1.java:30)
at simserver.Main.main(Main.java:27)
Ora non so se è un fatto di porte non nattate del router dannazione...
Il codice è questo:
/*
* Main.java
*
* Created on 9 febbraio 2007, 9.50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import java.io.*;
/**
*
* @author ale
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Interf1 interf=new Interf1(900);
interf.setVisible(true);
}
}
/*
* Mserver.java
*
* Created on 9 febbraio 2007, 9.50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* @author ale
*/
public class Mserver extends JFrame implements Runnable {
protected Socket client;
private JLabel notifica;
Thread thr;
/** Creates a new instance of Mserver */
public Mserver(Socket skt) {
super(skt.getInetAddress().toString());
this.setVisible(true);
notifica=new JLabel("Arrivato nuovo client da "+ skt.getInetAddress());
client=skt;
}
public void init() {
thr=new Thread(this);
getContentPane().add(notifica);
pack();
}
public void start() {
thr.start();
}
public void run() {
try {
InputStream i=client.getInputStream();
OutputStream o=client.getOutputStream();
PrintStream p=new PrintStream(o);
p.println("Benvenuti");
p.println("Digitare HELP per lista servizi");
int x;
ByteArrayOutputStream command=new ByteArrayOutputStream();
String helpcommand=new String("HELP");
String quitcommand=new String("QUIT");
while((x=i.read())>-1) {
o.write(x);
if(x==13) {
p.println();
if(helpcommand.equalsIgnoreCase(command.toString()))
p.println("Il solo servizio disponibile è l'help");
else if(quitcommand.equalsIgnoreCase(command.toString())) {
p.println("BYE.");
try {
Thread.sleep(1000);
}
finally {
break;
}
}
else
p.println("Comando non disponibile.");
command.reset();
}
else if(x!=10)
command.write(x);
}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
notifica.setText("Connessione chiusa con " + client.getInetAddress());
try {
client.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
/*
* Interf1.java
*
* Created on 9 febbraio 2007, 10.42
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package simserver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* @author ale
*/
public class Interf1 extends JFrame {
private JLabel message;
private JLabel attesa;
/** Creates a new instance of Interf1 */
public Interf1(int port) {
message=new JLabel("Server in partenza sulla porta " + port);
getContentPane().add(message);
pack();
try {
ServerSocket svr=new ServerSocket(port);
message.setText("Server partito sulla porta " + svr.getLocalPort());
Socket client;
attesa=new JLabel("");
getContentPane().add(attesa);
while(true) {
client=svr.accept();
attesa.setText("Richiesta di connessione da " + client.getInetAddress());
(new Mserver(client)).start();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Si tratta di un demone in ascolto su una determinata porta, che accetta connessioni in multithread, io credo che il codice sia giusto l ho guardato 1000 volte :(
Provato con altre porte diverse da 900 ma mi da lo stesso risultato, sia con windows che con Linux :(