Results 1 to 5 of 5
  1. #1
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Exclamation BindException(not getting why this error is coming)

    i was working on a small program that writes a line in one run window of Netbeans and the line will be seen at a different run window of Netbeans,here's my code-

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class WriteServer {
        public static int serverPort=80;
        public static int clientPort=80;
        public static int buffer_size=1024;
        public static DatagramSocket ds;
        public static byte buffer[]=new byte[buffer_size];
        
        public static void TheServer() throws IOException{
        int pos=0;
        while(true){
        int c=System.in.read();
        switch(c){
            case -1:
                System.out.println("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 IOException{
        while(true){
        DatagramPacket p=new DatagramPacket(buffer, buffer.length);
        ds.receive(p);
            System.out.println(new String(p.getData(),0,p.getLength()));
        }
        }
        
        
        public static void main(String[] args) throws IOException{
            int i=System.in.read();
            if(i==1){
            ds=new DatagramSocket(serverPort);
            TheServer();
            }
            else
            {
            ds=new DatagramSocket(clientPort);
            TheClient();
            }
        }
    }
    but i am getting this error-

    Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
    at java.net.PlainDatagramSocketImpl.bind0(Native Method)
    at java.net.PlainDatagramSocketImpl.bind(PlainDatagra mSocketImpl.java:91)
    at java.net.DatagramSocket.bind(DatagramSocket.java:3 72)
    at java.net.DatagramSocket.<init>(DatagramSocket.java :211)
    at java.net.DatagramSocket.<init>(DatagramSocket.java :262)
    at java.net.DatagramSocket.<init>(DatagramSocket.java :235)
    at WriteServer.main(WriteServer.java:51)
    Java Result: 1


    please help me in this regard,thanks in advance....

  2. #2
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: BindException(not getting why this error is coming)

    someone please help me.....

  3. #3
    Briksins is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Re: BindException(not getting why this error is coming)

    Did u read the error? it says "Address already in use: Cannot bind"
    It mean that internet address @ localhost port 80 is already in use

    first of all separate Server and client by classes, don't mix everything together.
    another think i'm not that cool at network but it looks like u didnt define properly in server and client sockets information

    I would recomend to you have a look at this web - Socket Programming tutorial

    There is basic Client - Server TCP/UDP examples for many languages including java

  4. #4
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: BindException(not getting why this error is coming)

    someone please reply...i am having a lot of trouble with it....i have tried many port numbers,but i didnt succeed....please help me....thanks in advance....

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: BindException(not getting why this error is coming)

    A few things after a quick look -

    Don't use System.in to read data from the console, you're not getting what you expect from this line
    Java Code:
    int i=System.in.read();
    Instead try using the Scanner class.

    The bind exception you are receiving is due to the code trying to bind two datagramsockets to the same port. The server is the only part that needs to bind to the port, take a look at the examples here.

Similar Threads

  1. where is 6 coming from
    By loja11 in forum Advanced Java
    Replies: 4
    Last Post: 10-16-2011, 03:48 PM
  2. i cant undersatand y this error is coming
    By RajeshReddy in forum AWT / Swing
    Replies: 0
    Last Post: 07-21-2011, 03:53 PM
  3. Jax is coming to the us!!!
    By blease87 in forum Reviews / Advertising
    Replies: 0
    Last Post: 03-04-2011, 01:21 PM
  4. java.net.BindException
    By techbossmb in forum JDBC
    Replies: 0
    Last Post: 10-13-2009, 05:56 PM
  5. java.net.BindException
    By techbossmb in forum XML
    Replies: 0
    Last Post: 10-13-2009, 05:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •