UDP

Hello, im new in Java.
I testing UDP Client + Server , but i don’t know how fix errors.
Maybe any can help me ?
Im testing this tutorial : http://www.corvstudios.com/tutorials/udpMultiplayer.php
Im run UDPServer.java and show on console:

User-PC
169.254.181.57

I run UDPClient.java and show on console:

java.net.SocketException: Unrecognized Windows Sockets error: 0: Cannot bind
        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 UDPServer.ServerThread.<init>(UDPServer.java:100)
        at UDPServer.Game.<init>(UDPServer.java:46)
        at UDPServer.UDPServer.main(UDPServer.java:22)
Exception in thread "Server Thread" java.lang.NullPointerException
        at UDPServer.ServerThread.receivePacket(UDPServer.java:131)
        at UDPServer.ServerThread.run(UDPServer.java:115)

Maybe any can help me ? :slight_smile:
Thanks. :wink:

no sure but maybe check your firewall ?

Check , but error has not changed.
And thanks for you reply DzzD!

http://www.coderanch.com/forums/posts/watch/0/207423

Seems to mean you might have something else running on the same port? (though that doesn’t really make sense with UDP does it?)

Kev

[quote]Seems to mean you might have something else running on the same port? (though that doesn’t really make sense with UDP does it?)
[/quote]
for a server it can

but wait, I just looked the link you posted, do you run it from an Applet ?! if so maybe a security issue also becarefull on your IP vs localhost, if the server is bind to your ip you wont reach it with localhost and vice-versa

have you tried it within your LAN before.

like without port forwarding?

Thanks all for replys.
Yes im use Windows 7 OS.
Im trying change ports ( im trying download Port Scanner etc. ::slight_smile: ) but error has not changed.
Maybe any test this tutorial on Windows ? what you port use ?

I have donet hat tutorial on windows XP.

I used the default port.

I noticed that you are not using a local ip. you should try to do it with “localhost” and run both the client and the server on the same computer.

It looks like your trying to run the server twice? If the client attempts to run, and you get UDPServer.java output then it seems like your IDE setup for running two separate java projects is running the same one.

Thanks Corvinex and other for replys :slight_smile: but error im can’t/don’t fixed :-.
Corvinex maybe yau have compile this tutorial ? maybe can share ? :slight_smile:
I think in order then to understand me, why im can’t running. :slight_smile:

Kay, let’s walk through this, I just built the thing on eclipse and it worked fine.
Ouput:
owner-c6f2b00d5
192.168.100.22
null Connected
, then i get the circle moved on the screen with wasd.

How I built the project:

  1. New Project
  2. New Package, udpserver and udpclient
  3. New Class, UDPServer in udpserver package and UDPClient in udpclient package
  4. Copy code into the empty classes.
  5. run server, run client.

Works for me. Socket bind exception is indeed most common if you have already bound the socket. Check you have no other instances running.
java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
comes for me if I try to launch another server.Make sure you terminate all the runners properly.
Use the red square to terminate. 8 steps right from the red stop button is a console selector,
‘Display Selected Console’, which should show you all running instances.

This will be particularly important to handle properly when you put this stuff on to the actual server, if you forget a running program
it will keep blocking that port from new instances of the program.

A problem we had once was that windows didn’t properly clean up a previous bind. A restart did fix it, but we went for the add one to the port number each time or pick a random port number. This was on XP however. Also you need special permission to bind to ports below 1024.

Yes work, but get on Applet error maybe any can help ?

java.lang.NoClassDefFoundError: UDPClient (wrong name: UDPClient/UDPClient)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: UDPClient (wrong name: UDPClient/UDPClient)

wrong package name (can you opst your applet code?)

Thanks. :slight_smile:

Applet HTML code:

<html>
<head>
</head>
<body>
	<applet code="UDPClient.class" width="640" height="480">
        <param name="username" value="Guest">
        <param name="ip" value="localhost">
        </applet>
</body>
</html>

Error:

java.lang.NoClassDefFoundError: UDPClient (wrong name: UDPClient/UDPClient)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: UDPClient (wrong name: UDPClient/UDPClient)

If u have exported it into a .jar then u need to put it into the tag
also make sure all the files are in same directory with the page.html .

I’m test and too get error ( error don’t change ).

Yea, make sure the UDPClient.class file is going into the same directory as your applet .html file.

I reproduced your error by not having the .class files in the same folder as the .html file:

java.lang.ClassNotFoundException: UDPClient.class
	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

this is what your folder should contain
UDPClient

  • Player.class
  • PlayerList.class
  • ReceiveThread.class
  • UDPClient.class
  • UDPClient.html

and then double click UDPClient.html, and the client should run.

.