|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SocketClient.java | - | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* Created on 2004/06/04
|
|
3 |
*/
|
|
4 |
package org.asyrinx.brownie.net.socket;
|
|
5 |
|
|
6 |
import java.io.IOException;
|
|
7 |
import java.io.PrintWriter;
|
|
8 |
import java.net.Socket;
|
|
9 |
|
|
10 |
/**
|
|
11 |
* @author akima
|
|
12 |
*/
|
|
13 |
public class SocketClient { |
|
14 |
/**
|
|
15 |
*
|
|
16 |
*/
|
|
17 | 0 |
public SocketClient() {
|
18 | 0 |
super();
|
19 |
} |
|
20 |
|
|
21 | 0 |
public SocketClient(String host, int port) { |
22 | 0 |
super();
|
23 | 0 |
this.host = host;
|
24 | 0 |
this.port = port;
|
25 |
} |
|
26 |
|
|
27 |
private Socket socket;
|
|
28 |
|
|
29 |
private String host = null; |
|
30 |
|
|
31 |
private int port = 5555; |
|
32 |
|
|
33 | 0 |
public void execute(String message) throws IOException { |
34 | 0 |
connect(); |
35 | 0 |
try {
|
36 | 0 |
send(message); |
37 |
} finally {
|
|
38 | 0 |
disconnect(); |
39 |
} |
|
40 |
} |
|
41 |
|
|
42 | 0 |
public void connect() throws IOException { |
43 | 0 |
socket = new Socket(host, port);
|
44 |
} |
|
45 |
|
|
46 | 0 |
public void disconnect() throws IOException { |
47 | 0 |
socket.close(); |
48 |
} |
|
49 |
|
|
50 | 0 |
public boolean isConnected() { |
51 | 0 |
return socket != null; |
52 |
} |
|
53 |
|
|
54 | 0 |
public void send(String message) throws IOException { |
55 | 0 |
final PrintWriter out = new PrintWriter(socket.getOutputStream(), true); |
56 | 0 |
try {
|
57 | 0 |
out.println(message); |
58 |
} finally {
|
|
59 | 0 |
out.close(); |
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* @return Returns the host.
|
|
65 |
*/
|
|
66 | 0 |
public String getHost() {
|
67 | 0 |
return host;
|
68 |
} |
|
69 |
|
|
70 |
/**
|
|
71 |
* @param host
|
|
72 |
* The host to set.
|
|
73 |
*/
|
|
74 | 0 |
public void setHost(String host) { |
75 | 0 |
this.host = host;
|
76 |
} |
|
77 |
|
|
78 |
/**
|
|
79 |
* @return Returns the port.
|
|
80 |
*/
|
|
81 | 0 |
public int getPort() { |
82 | 0 |
return port;
|
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* @param port
|
|
87 |
* The port to set.
|
|
88 |
*/
|
|
89 | 0 |
public void setPort(int port) { |
90 | 0 |
this.port = port;
|
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* @return Returns the socket.
|
|
95 |
*/
|
|
96 | 0 |
public Socket getSocket() {
|
97 | 0 |
return socket;
|
98 |
} |
|
99 |
} |
|