Line | |
---|
1 | package org.expeditee.network;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.net.DatagramPacket;
|
---|
5 | import java.net.DatagramSocket;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.MessageBay;
|
---|
8 |
|
---|
9 | public class MessageReciever extends DefaultServer {
|
---|
10 | public final static int OFFSET = 2;
|
---|
11 |
|
---|
12 | public MessageReciever(int port) throws IOException {
|
---|
13 | super("MessageReciever", port + OFFSET);
|
---|
14 | }
|
---|
15 |
|
---|
16 | public void listenForMessages() throws IOException {
|
---|
17 | byte[] buf = new byte[FrameServer.MAX_PACKET_LENGTH];
|
---|
18 |
|
---|
19 | // receive request
|
---|
20 | DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
---|
21 | socket.receive(packet);
|
---|
22 |
|
---|
23 | String packetContents = new String(packet.getData(), 0, packet
|
---|
24 | .getLength());
|
---|
25 | String sendersName = FrameShare.getInstance().getPeerName(
|
---|
26 | packet.getPort() + 1, packet.getAddress());
|
---|
27 | /**
|
---|
28 | * If the sender of the message is not on our peer list then use their
|
---|
29 | * IP addy.
|
---|
30 | */
|
---|
31 | if (sendersName == null)
|
---|
32 | sendersName = packet.getAddress().toString();
|
---|
33 |
|
---|
34 | packetContents = sendersName + " says: " + packetContents;
|
---|
35 | MessageBay.displayMessage(packetContents);
|
---|
36 | }
|
---|
37 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.