-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientSocketManager.java
More file actions
86 lines (76 loc) · 2.75 KB
/
ClientSocketManager.java
File metadata and controls
86 lines (76 loc) · 2.75 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
public class ClientSocketManager
{
// References to objects for Socket connection and
// Reading/writing to sockets
private Socket mySocket = null; // Reference to the Socket for managing a client socket
private PrintWriter s_out = null; // The PrintWriter used to write to the socket
private BufferedReader s_in = null; // The BufferedReader used to read from the socket
// For maintaining the address components of the socket addresses.
String destIP = null;
String myIP = null;
int destPort = -1;
int myPort = -1;
// Constructor
public ClientSocketManager()
{
mySocket = null; // need to call connect to setup the Socket.
}
// Connect socket
// Create and connect the socket using the IP/Port values given in the arguments.
// The instance varaiables destIP, destPort, myIP and myPort are also updated.
public void connect(String dIp, int dport) throws IOException
{
mySocket = new Socket(dIp, dport); // need to create new socket for new connection
destIP = dIp;
destPort = dport;
myIP = mySocket.getLocalAddress().toString();
myPort = mySocket.getLocalPort();
// Setup reader and writer
s_out = new PrintWriter(mySocket.getOutputStream(), true); // flag true for autoflush
s_in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
}
// Close the connection
// Closes the socket connection.
public void close() throws IOException
{
mySocket.shutdownInput();
mySocket.shutdownOutput();
mySocket.close();
destIP = null;
destPort = -1;
myPort = -1;
}
// Poll socket to see if data available
// Do consult the documentation on the ready() method (BufferedInput) to
// understand the method.
public boolean poll() throws IOException
{
return(s_in.ready());
}
// Read a string from connection
// If a SocketException occurs, assume the connection is closed.
public String read() throws IOException
{
String stream;
try {
stream = s_in.readLine(); // returns null if remote end closed connection
}
catch (SocketException e) { // assume connection is closed
System.out.println("readClient: SocketException");
close(); // close the connection
stream = null;
}
return(stream);
}
// Write a String to the connection
public void write(String stream) throws IOException
{
s_out.println(stream); // need line feed so that readLine() sees the line - the line feed is not delivered.
}
}