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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

EJP
You have a local firewall installed, e.g. a Windows firewall, that is blocking you from using UDP ports 998/999.
843790
Ah sweet, just tried 1200 and 1201 and it works - thanks alot!!
EJP
Actually I am wrong, as you are on Linux you need root privilege to use a port < 1024.
843790
Hey ejp,

Thanks for the ( valuable ) advice!

Ok I got the little java app to talk to itselfs (2 instances, one as client, one as server) which is great.

Ok this is perhaps a little out of line (being C++ and all hehe) but I was wondering if a UDP message sent by my C++ app will be read-able by the java app (in theory it should right?)

I have tried my little C++ app as follows, but the messages do not get received (they DO get received by the client c++ version of this app):


//
// UDP_Talker: Sending some UDP packets
//
// File: main.cc// Author: ltsmm
//
// Created on 6 November 2007, 14:27
//



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define SERVERPORT 1200 // the port users will be connecting to



int main(int argc, char** argv)
{
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;

if(argc != 3)
{
fprintf(stderr, "usage: talker hostname message \n");
exit(1);
}

if((he = gethostbyname(argv[1])) == NULL) // get the host info
{
herror("gethostbyname");
exit(1);
}

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

their_addr.sin_family = AF_INET; // HBO
their_addr.sin_port = htons(SERVERPORT); // short, NBO
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);

if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof their_addr)) == -1)
{
printf("error sending!");
perror("sendto");
exit(1);
}

printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
close(sockfd);

return (EXIT_SUCCESS);
}




Usage is: ltsmm@gertlx:~/Cpp/UDP_Talker/dist/Debug/GNU-Linux-x86$ ./udp_talker localhost "helloworld"



-G






ps: the listener C++ app for completeness:

// 
// UDP_Listener: Listening to UDP packets coming in
//
// File:   main.cc// Author: ltsmm
//
// Created on 6 November 2007, 14:27
//


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MYPORT 1200 // the port users will be connecting to
#define MAXBUFLEN 100

int main(int argc, char** argv) 
{
    int                 sockfd;
    struct sockaddr_in  my_addr;    // MY address info
    struct sockaddr_in  their_addr; // THEIR address info
    socklen_t           addr_len; 
    int                 numbytes;
    char                buf[MAXBUFLEN];

    printf("****** UPD_Listener Running ********\n");

    /****** Get socket file descriptor **********/
    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    /****** Set address info **********/
    my_addr.sin_family = AF_INET;           // HBO
    my_addr.sin_port = htons(MYPORT);       // short, NBO
    my_addr.sin_addr.s_addr = INADDR_ANY;   // Automagically fill with my IP
    memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

    /****** Bind socket **********/
    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1)
    {
        perror("bind");
        exit(1);
    }

    while(1) // run loop forever
    {
        /******** Do recvfrom() *********/
        addr_len = sizeof their_addr;
        if( (numbytes = recvfrom(sockfd, buf, MAXBUFLEN - 1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)
        {
            perror("recvfrom");
            exit(1);
        }

        printf("got packet from %s\n", inet_ntoa(their_addr.sin_addr));
        printf("packet is %d bytes long\n", numbytes);
        buf[numbytes] = '\0';
        printf("packet contains \"%s\" \n", buf);
    }


    // Close socket:
    close(sockfd); 






    return (EXIT_SUCCESS);
}

 
EJP
Should work as long as you have someone receiving UDP datagrams on port 1200.
843790
ha ha ok thats my bad, i had the talker talkin to PORT 1201 instead of 1200

cheers EJP for your help!!

how sweet is c++ talkin to java over network?! :-D
1 - 6
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
559 views