Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  java.net.BindException: Address already in use: Cannot bind  (Read 2650 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-05-11 20:03:08 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
[code]Alright, so I'm trying to make a upc client/server and I keep running into this problem no matter where I bind the socket. Maybe someone can tell me what I'm doing wrong?

Note: this is as small as I could make these test cases.

Server:
[code]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;


public class TestServer {
   static DatagramSocket socket;
   public static void main(String[] args){
      int MAX_PLAYERS = 2;
       int playerNum = 0;
       
       boolean listening = true;
       
       
       
        while(listening){
           byte[] buf = new byte[255];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            System.out.println("Waiting for packet from player: " + playerNum + "\n");
           
            try {
             socket = new DatagramSocket(4444);
          } catch (SocketException e1) {
             // TODO Auto-generated catch block
            e1.printStackTrace();
          }
           
            try {
            socket.receive(packet);
         } catch (IOException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
         }
           
           System.out.println("Waiting to connect with player: " + playerNum + "\n");
           new ClientThread(packet,playerNum).start();
           //stops here.
          System.out.println("Connected with player: " + playerNum + " Now incrementing" + "\n");
           playerNum++;
           System.out.println("Incremented to: " + playerNum+ "\n");
        }
       
       
       
        //serverSocket.close();
       System.exit(0);
   }
}


Client thread (server)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
import java.net.*;
import java.nio.ByteBuffer;

public class ClientThread extends Thread implements Runnable{
   int playerNum;;
   DatagramSocket socket;
   InetAddress address;
   int port;
   public ClientThread(DatagramPacket packet, int playerNum){
      super("ClientThread");
     
      address = packet.getAddress();
      port = packet.getPort();

      this.playerNum = playerNum;  
   }
   
   public void run(){
      try{
         System.out.println("Thread" + playerNum + ": " + "Accepted. Now creating I/O.\n");
           
           while(true){

              socket = new DatagramSocket(port);
              byte[] buf = new byte[255];
              DatagramPacket packet = new DatagramPacket(buf, buf.length);
               socket.receive(packet);
             
              ByteBuffer recieveBuf = ByteBuffer.wrap(packet.getData());
           
            recieveBuf.flip();
            System.out.println("Thread" + playerNum + ": " + recieveBuf.toString());
           
            ByteBuffer sendBuf = ByteBuffer.allocate(255);
            sendBuf.asCharBuffer();
            String s = "Thread" + playerNum + ": " + "sent data";
            sendBuf.put(s.getBytes("ASCII"));
            DatagramPacket packet2 = new DatagramPacket(sendBuf.array(), sendBuf.array().length, address, port);
            socket.send(packet2);
              socket.close();
             
             this.sleep(15);
           }

      }
     
      catch(Exception e){
         e.printStackTrace();
         System.exit(1);
      }
       
       
   }
   
}


Client
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;


public class TestClient {
   static ClientThread ct;
   static int playerNum = 0;
   
   public static void main(String[] args){
          //String ip = JOptionPane.showInputDialog("Input server IP.");
         ct = new ClientThread();
           ct.start();
           ct.setPriority(Thread.MAX_PRIORITY);
           
           playerNum = ct.playerNum;
   }
}

class ClientThread extends Thread implements Runnable{
   DatagramSocket socket;
   DatagramPacket packet;
   int playerNum;
    boolean loop = true;
    int port;
    byte [] addr;
    InetAddress address;
   
   public ClientThread(){
      super("ClientThread");
     
      try{
         playerNum = 0;
          System.out.println(playerNum);
         
          port = 4445;
          addr = new byte[] {(byte) 76,(byte) 121,(byte) 76,(byte) 188};
          address = InetAddress.getByAddress(addr);
         
          byte[] buf = new byte[10];
          socket = new DatagramSocket(4444);
          DatagramPacket packet = new DatagramPacket(buf, 10, address, 4444);
         System.out.println("send" + address.toString());
         socket.send(packet);
         
         
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
   
   public void run(){
      try{
         while(loop){
           
            try{
               socket = new DatagramSocket(4444);
               System.out.println("start");
               //byte[] sendBuf = new byte[256];
              ByteBuffer sendBuf = ByteBuffer.allocate(255);
               sendBuf.asCharBuffer();
               String s = "Thread" + playerNum + ": " + "sent data";
               sendBuf.put(s.getBytes("ASCII"));
               
               
               DatagramPacket packet = new DatagramPacket(sendBuf.array(), sendBuf.array().length, address, port);
               System.out.println("send" + address.toString());
               socket.send(packet);
               System.out.println("sent");
               
               byte[] buf = new byte[255];
               packet = new DatagramPacket(buf, buf.length);
               socket.receive(packet);
               ByteBuffer receiveBuf = ByteBuffer.allocate(512);
               receiveBuf.put(packet.getData());

               receiveBuf.flip();
               System.out.println("Thread" + playerNum + ": " + receiveBuf.toString());
               
               
               this.sleep(15);
              }
            catch(Exception e){
                   e.printStackTrace();
                   //socket.close();
            }  
         }
         
         
         
      }
     
      catch(Exception e){
         e.printStackTrace();
      }
   }
}


Server output:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
Waiting for packet from player: 0

Waiting to connect with player: 0

Connected with player: 0 Now incrementing

Incremented to: 1

Waiting for packet from player: 1

Thread0: Accepted. Now creating I/O.

java.net.BindException: Address already in use: 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 java.net.DatagramSocket.<init>(Unknown Source)
   at ClientThread.run(ClientThread.java:24)


Client Exception:
1  
2  
java.net.BindException: Address already in use / at java.net.PlainDatagramSocketImpl.bind0(Native Method) / at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:85) / at java.net.DatagramSocket.bind(DatagramSocket.java:373) / at java.net.DatagramSocket.<init>(DatagramSocket.java:229) / at java.net.DatagramSocket.<init>(DatagramSocket.java:282)
at java.net.DatagramSocket.<init>(DatagramSocket.java:254) / at ClientThread.run(TestClient.java:60)


So can anyone see what I'm doing wrong?[/code][/code]
Offline CyanPrime
« Reply #1 - Posted 2011-05-11 20:05:41 »

Okay, I edited out the top two "code" tags, but they keep coming back. Wtf?
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #2 - Posted 2011-05-11 20:35:51 »

You're not supposed to turn it into an article/code section. They are used for any tutorials/useful code you can share. Use normal threads for discussions and questions.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #3 - Posted 2011-05-11 20:50:08 »

Done, but it's still flipped out.  Shocked
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #4 - Posted 2011-05-12 00:32:23 »

Fixed it for you

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #5 - Posted 2011-05-12 02:38:31 »

@Riven
There is already another thread he started with the same content so you can just completely delete this one.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (109 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.068 seconds with 20 queries.