OpenCores
URL https://opencores.org/ocsvn/udp_ip_stack/udp_ip_stack/trunk

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [sw/] [UDPCxn.java] - Blame information for rev 34

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 pjf
package com.pjf;
2
 
3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.DatagramSocket;
6
import java.net.InetAddress;
7
import java.net.SocketException;
8
import java.net.UnknownHostException;
9
 
10
 
11
public class UDPCxn {
12
        private DatagramSocket skt;
13
        private InetAddress dstIP;
14
 
15
        public UDPCxn(long dstIPadr) throws SocketException, UnknownHostException {
16
                skt = new DatagramSocket();
17
                        byte[] target = new byte[4];
18
                        target[0] = (byte) ((dstIPadr >> 24) & 0xff);
19
                        target[1] = (byte) ((dstIPadr >> 16) & 0xff);
20
                        target[2] = (byte) ((dstIPadr >> 8) & 0xff);
21
                        target[3] = (byte) (dstIPadr & 0xff);
22
                        dstIP = InetAddress.getByAddress(target);
23
        }
24
 
25
        public UDPCxn(String dstIPadr) throws SocketException, UnknownHostException {
26
                skt = new DatagramSocket();
27
                String[] parts = dstIPadr.split("[.]");
28
                if (parts.length != 4) {
29
                        throw new UnknownHostException("ip addr must have 4 parts");
30
                }
31
                        byte[] target = new byte[4];
32
                        for (int i = 0; i<4; i++) {
33
                                target[i] = (byte) Integer.parseInt(parts[i]);
34
                        }
35
                        dstIP = InetAddress.getByAddress(target);
36
        }
37
 
38
        public void send(byte[] data, int port) throws IOException {
39
                DatagramPacket pkt = new DatagramPacket(data, data.length, dstIP, port);
40
                System.out.println("Sending packet");
41
                skt.send(pkt);
42
        }
43
 
44
        public void fixSend(String str, int port, boolean print) throws IOException {
45
                String s1 = str.replace('~','\001');
46
                byte[] data = s1.getBytes();
47
                DatagramPacket pkt = new DatagramPacket(data, data.length, dstIP, port);
48
                if (print) {
49
                        System.out.println("Sending packet: " + str + " on port " + port);
50
                }
51
                skt.send(pkt);
52
        }
53
 
54
 
55
        public byte[] rcv() throws IOException {
56
                byte[] buf = new byte[1024];
57
                    DatagramPacket pkt = new DatagramPacket(buf, buf.length);
58
//                  System.out.println("waiting to receive ...");
59
                    skt.receive(pkt);
60
                    int len = pkt.getLength();
61
                    byte[] rd = pkt.getData();
62
                byte[] data = new byte[len];
63
                    for (int i=0; i<len; i++) {
64
                        data[i] = rd[i];
65
                    }
66
                    return data;
67
        }
68
 
69
        public void close() {
70
                    skt.close();
71
        }
72
        }

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.