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!

UDP Server/Client Socket bind exception

843790May 5 2007 — edited May 7 2007
Hi

I have written a simple server based on UDP
MainServer waits for packets and creates a MainServerThread which processes the input data

MainServer.java
import java.net.*;
import java.io.*;
import java.sql.*;

class MainServer {
    
    //UDP Server
    int ServerPort;
    int backlog;
    DatagramSocket server;
    InetAddress host;
    DatagramPacket recievePacket;
    byte[] recieveData;
    
    public MainServer(){
        
        backlog = 75;
        ServerPort = 25519;
        
        try {
            host = InetAddress.getByName("beer-game.selfip.org");
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }
        recieveData = new byte[1024];
                
        try {
            server = new DatagramSocket(ServerPort,host);
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void listen(){        
        int i=0;
        while(true){
            try {
                recievePacket = new DatagramPacket(recieveData,recieveData.length);
                server.receive(recievePacket);                
                MainServerThread MST = new MainServerThread(recievePacket);
		MST.start();
                System.out.println("Client : " + (++i));
            } catch (IOException ex) {
                ex.printStackTrace();
            }            
        }
    }
    
    public static void main(String[] args){
        MainServer ServerObj = new MainServer();
        ServerObj.listen();
    }    
}
MainServerThread.java
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.Socket;

abstract class MainServerThrd extends DBAccess{}
class MainServerThred extends Thread{}

class MainServerThread extends MainServerThred implements Runnable{
    
    //Client Info
    DatagramPacket recievedPacket;
    int ClientPort;  
    InetAddress ClientIP;
    String InputData;
    
    //Parser
    String Keyword;
    String[] Data;
    String Delimiter;
    
MainServerThread(DatagramPacket rcvdPacket){
    recievedPacket = rcvdPacket;
    ClientIP = recievedPacket.getAddress();
    ClientPort = recievedPacket.getPort();    
    Delimiter = ":";
}

public void run(){
    InputData = new String(recievedPacket.getData());
    parser(InputData);
}

void parser(String InputData){
    System.out.println(InputData + " will be parsed ");
    Keyword = InputData.substring(0,5);
    System.out.println(Keyword + " service requested ");
    Data = InputData.split(Delimiter);
    for(int i=0;i<Data.length;i++)
        System.out.println(Data);
}

public void ExecQuery(){}
}


I can compile them but cannot execute them
I get the following exception when i try to run MainServer

java.net.BindException: Cannot assign requested address: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at MainServer.<init>(MainServer.java:28)
at MainServer.main(MainServer.java:49)
Exception in thread "main" java.lang.NullPointerException
at MainServer.listen(MainServer.java:39)
at MainServer.main(MainServer.java:50)

I am really low on time
A hasty reply is much much appreciated.
Thanks,
Abhi
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 4 2007
Added on May 5 2007
3 comments
301 views