-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortAddress.java
executable file
·105 lines (87 loc) · 1.71 KB
/
PortAddress.java
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
101
102
103
104
105
import java.net.Socket;
import java.net.SocketException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;
import java.io.*;
public class PortAddress
{
public PortAddress ()
{
_valid = false;
}
public PortAddress (int port)
{
_valid = true;
_port = port;
try
{
InetAddress inet = InetAddress.getLocalHost();
_hostName = inet.getHostName();
}
catch (UnknownHostException e)
{
_valid = false;
};
}
public PortAddress (String host, int port)
{
_valid = true;
_port = port;
_hostName = host.toString();
}
public boolean valid ()
{
return _valid;
}
public boolean assign (PortAddress addr)
{
_valid = addr.valid();
_port = addr.portNumber();
_hostName = addr.hostName().toString();
return true;
}
public boolean equals (PortAddress addr)
{
if (_valid && addr.valid())
{
if ((_port == addr.portNumber()) && _hostName.equals(addr.hostName()))
return true;
}
return false;
}
public boolean notequals (PortAddress addr)
{
if (this.equals(addr))
return false;
else
return true;
}
public void pack (OutputBuffer buff) throws IOException
{
buff.packBoolean(_valid);
buff.packInt(_port);
buff.packString(_hostName);
}
public void unpack (InputBuffer buff) throws IOException
{
_valid = buff.unpackBoolean();
_port = buff.unpackInt();
_hostName = buff.unpackString();
}
public void print ()
{
System.out.println("PortAddress < "+_valid+", "+_port+", "+_hostName+" >\n");
}
public int portNumber ()
{
return _port;
}
public String hostName ()
{
return _hostName;
}
private boolean _valid;
private int _port;
private String _hostName;
}