Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple Socket Problem ( ? )

843790Apr 15 2008 — edited Apr 15 2008
Hey guys,

When I try to run the following code on my system (Linux Kubuntu), I get the exception below (see bottom please):
/*
 * WriteServer.java
 *
 * Created on 15 April 2008, 14:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.rmi.RMISecurityManager;

/**
 *
 * @author ltsmm
 */
public class WriteServer
{
    public static int            serverPort = 998;
    public static int            clientPort = 999;
    public static int            buffer_size = 1024;
    public static DatagramSocket ds;
    public static byte           buffer[]   = new byte[buffer_size];
    
    public static void TheServer() throws Exception {
        int pos = 0;
        while(true) { //keep looping
            int c = System.in.read();
            switch(c) {
                case -1:
                    echo("Server Quits");
                    return;
                
                case '\r':
                    break;
                
                case '\n':
                    ds.send(new DatagramPacket(buffer, pos, InetAddress.getLocalHost(), clientPort) );
                    pos = 0;
                    break;
                
                default:
                    buffer[pos++] = (byte) c;
            }
        }       
    }
    
    public static void TheClient() throws Exception {
        while(true) {
            DatagramPacket p = new DatagramPacket(buffer, buffer.length);
            ds.receive(p);
            echo( new String(p.getData(), 0, p.getLength()));
        }
        
    }
    
    public static void main(String args[]) throws Exception {
                     
        if(args.length == 1) {
            ds = new DatagramSocket(serverPort); //error occurs here
            TheServer();
        }
        else {
            ds = new DatagramSocket(clientPort); //or here (if args.length == 0)
            TheClient();
        }
    }
    
    
    private static void echo(String s) {
        System.out.println(s);
    }
    
}
Exception in thread "main" java.net.BindException: Permission denied
        at java.net.PlainDatagramSocketImpl.bind0(Native Method)
        at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:82)
        at java.net.DatagramSocket.bind(DatagramSocket.java:368)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:210)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:261)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:234)
        at org.WriteServer.main(WriteServer.java:73)
I've tried using a security manager thingie, according to this page: http://www.nabble.com/Permission-denied-td3565150.html
but with no success :-(

If anyone has ideas how to get around this - much appreciated!

Gerry
sockets newbie

Edited by: gvanto on Apr 15, 2008 4:54 AM

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 13 2008
Added on Apr 15 2008
6 comments
593 views