You are not logged in.

  • Login
Bitte besucht unser neues Forum unter https://forum.nitrado.net | Please visit our new Forum at https://forum.nitrado.net

Dear visitor, welcome to Nitrado.net Prepaid Gameserver Community-Support - Archiv. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

1

Friday, October 9th 2015, 9:33am

Java UDP kommt nicht klar

Hallo,

Ich versuche über UDP ein Client Server System zu machen.
Das ganze sieht folgendermaßen aus:

Server
|
v
Android oder PC Client.

Der Android Client klappt problemlos.
Der PC Client hat den selben sende Code aber es kommen nur von 100 Packets ca 3 an.

Sende code:

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Sender {

	public static void send(int i) throws Exception {
		DatagramSocket dsocket = new DatagramSocket();
		System.out.println("asd"); //Debug

		String host = "aaa.bbb.ccc.ddd";
		int port = PORT;
		String s = "" + i;
		byte[] message = s.getBytes();
		System.out.println(s); //Debug
		// Get the internet address of the specified host
		InetAddress address = InetAddress.getByName(host);
		System.out.println(address.toString()); //Debug
		// Initialize a datagram packet with data and address
		DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
		System.out.println(packet.toString()); //Debug
		// Create a datagram socket, send the packet through it, close it.
		
		dsocket.send(packet);
		dsocket.close();

	}
}


Mag mir da mal einer helfen?

iSEVEN

User / Kunde

  • "iSEVEN" is male

Posts: 1,431

  • Send private message

2

Friday, October 9th 2015, 9:45am

1. Wenn wir dir helfen sollen brauchen wir den kompletten Code
2. Welche Debug Nachrichten werden ausgegeben?
3. Wird die Exception angefangen?
4. Irgendeine Fehlermeldung?

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

3

Friday, October 9th 2015, 9:48am

Kein Fehler.
Das was ausgegeben werden soll. Der Int, die IP und die addr von dem DatagramPacket.

Spoiler Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
JButton h = new JButton("100%");
		h.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Sender.send(7);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		h.setBounds(250, 315, 80, 30);
		p.add(h);
		frame.add(p);

Empfänger

Spoiler Spoiler

Source code

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
private void scan() {
		
		try {
		      int port = 02015;

		      // Create a socket to listen on the port.
		      DatagramSocket dsocket = new DatagramSocket(port);

		      // Create a buffer to read datagrams into. If a
		      // packet is larger than this buffer, the
		      // excess will simply be discarded!
		      byte[] buffer = new byte[2048];

		      // Create a packet to receive data into the buffer
		      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

		      // Now loop forever, waiting to receive packets and printing them.
		      while (true) {
		        // Wait to receive a datagram
		        dsocket.receive(packet);

		        // Convert the contents to a string, and display them
		        String msg = new String(buffer, 0, packet.getLength());
		        System.out.println(packet.getAddress().getHostName() + ": "
		            + msg);
		        new Executor(Integer.parseInt(msg));

		        // Reset the length of the packet before reusing it.
		        packet.setLength(buffer.length);
		      }
		    } catch (Exception e) {
		      System.err.println(e);
		    }
		
	}

iSEVEN

User / Kunde

  • "iSEVEN" is male

Posts: 1,431

  • Send private message

4

Friday, October 9th 2015, 10:30am

Ja und was wird ausgegeben? Lass dir nicht immer alles aus der Nase ziehen...

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

5

Friday, October 9th 2015, 10:33am

asd
5
/aaa.bbb.ccc.ddd
java.net.DatagramPacket@63650cda

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

6

Sunday, October 11th 2015, 11:03pm

//Posh

magicced01

User / Kunde

Posts: 30

  • Send private message

7

Sunday, October 11th 2015, 11:22pm

Wieso verwendest du kein Tcp bei Udp kann datenverlust auftreten

ketrwu

Moderator

  • "ketrwu" is male

Posts: 1,430

Thanks: 2

  • Send private message

8

Sunday, October 11th 2015, 11:29pm

Quoted

I'd tell you a joke about UDP, but you probably wouldn't get it.

Probier's so:
https://systembash.com/a-simple-java-udp…and-udp-client/

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

9

Monday, October 12th 2015, 8:27am

Ich ba lange gebraucht um den zu verstehen... :D

Dass es verloren gehen kann weis ich. Aber für den Zweck ists ok. Is ja sogar selbes Netzwerk. Was mich halt stört ist:

Auf dem PC Android Emulator mit der App = Alle da
Auf dem PC das Java Programm = 2 von 1000

Das kann doch nicht nur an UDP liegen...

iSEVEN

User / Kunde

  • "iSEVEN" is male

Posts: 1,431

  • Send private message

10

Monday, October 12th 2015, 11:50am

Ansonsten probier es doch mit Netty (http://netty.io)

jonas32

User / Kunde

  • "jonas32" is male
  • "jonas32" started this thread

Posts: 350

Location: 127.0.0.1

Occupation: Azubi Fachinformatiker für Systemintegration

  • Send private message

11

Monday, October 12th 2015, 3:44pm

Wollte ich vermeiden da es keine permanente Verbindung sein soll und es mehrere Geräte sind.

Bitte besucht unser neues Forum unter https://forum.nitrado.net| Please visit our new Forum at https://forum.nitrado.net