blob: a092bfb7be8657465485c6440d2446db521dcd83 (
plain)
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
|
package com.cycling74.net;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.cycling74.max.Atom;
import com.cycling74.max.MaxRuntimeException;
/**
* Class wrapper to send atoms via TCP/IP. The host on the other side
* must use TcpReceive to read the sended atoms.
*
* This class is a work in progress and have been lightly tested.
*/
public class TcpSender {
InetAddress inetAddress;
String address = null;
int port = -1;
public TcpSender() {
}
public TcpSender(String address, int port) {
setAddress(address);
this.port = port;
}
public void send(Atom args[]) {
send(Atom.toOneString(args));
}
public void send(int i) {
send(Integer.toString(i));
}
public void send(float f) {
send(Float.toString(f));
}
public void send(String msg) {
if ( address == null )
throw new MaxRuntimeException("TcpSender has no active hosts");
if ( port == -1 )
throw new MaxRuntimeException("TcpSender has no active port");
try {
Socket sender = new Socket(inetAddress, port);
sender.getOutputStream().write(msg.getBytes());
sender.close();
} catch (IOException e) {
throw new MaxRuntimeException(e);
}
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
try {
inetAddress = InetAddress.getByName(address);
} catch (UnknownHostException e) {
throw new MaxRuntimeException(e);
}
this.address = address;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
|