📄 echoclient.java
字号:
// EchoClient.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, August 2005
/* Open a link to the server, and create an InputStream and
OutputStream.
Communication is triggered by echoMessage() being called,
which sends a message to the server, and waits for an
answer.
closeDown() closes the server link.
EchoClient uses the same readData() and sendMessage()
methods as ThreadedEchoHandler.
*/
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;
public class EchoClient extends Thread
{
private ServiceRecord servRecord;
private ClientForm clientForm;
private StreamConnection conn; // for the server
private InputStream in; // stream from server
private OutputStream out; // stream to server
private boolean isClosed = true;
// is the connection to the server closed?
public EchoClient(ServiceRecord sr, ClientForm cf)
{
clientForm = cf;
servRecord = sr;
} // end of EchoClient()
public void run()
/* The server connection is set up in this thread since
the call to Connector.open() may block for a long period,
and we don't want that to affect the ClientForm GUI.
*/
{
// get a URL for the service
String servURL = servRecord.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
// System.out.println("Service URL: " + servURL);
if (servURL != null) {
clientForm.setStatus("Found Echo Server URL");
try {
// connect to the server, and extract IO streams
conn = (StreamConnection) Connector.open(servURL);
// System.out.println("Opened a connection to server");
out = conn.openOutputStream();
in = conn.openInputStream();
clientForm.setStatus("Connected to Echo Server");
clientForm.setEnable(true); // communication allowed
isClosed = false; // i.e. the connection is open
}
catch (Exception ex)
{ System.out.println(ex);
clientForm.setStatus("Connection Failed");
}
}
else
clientForm.setStatus("No Service Found");
} // end of run()
public void closeDown()
// send "bye$$", then close the link with the service
{
if (!isClosed) {
sendMessage("bye$$"); // tell the server that the client is leaving
try {
if (conn != null) {
in.close();
out.close();
conn.close();
}
}
catch (IOException e)
{ System.out.println(e); }
isClosed = true;
}
} // end of closeDown();
// --------------------- IO methods ------------------
// readData() and sendMessage are the same as the methods
// in ThreadedEchoHandler
public String echoMessage(String msg)
/* This method is used by ClientForm to send a message (msg) to
the server, and wait for an answer. The response is returned.
If there's a problem, the client is notified using disableClient(),
and null is returned from this method.
*/
{
if (isClosed) {
disableClient("No Connection to Server");
return null;
}
if ((msg == null) || (msg.trim().equals("")))
return "Empty input message";
else {
if (sendMessage(msg)) { // message sent ok
String response = readData(); // wait for response
if (response == null) {
disableClient("Server Terminated Link");
return null;
}
else // there was a response
return response;
}
else { // unable to send message
disableClient("Connection Lost");
return null;
}
}
} // end of echoMessage()
private void disableClient(String msg)
/* Notify the client of the communications error,
and prevent any further communication. */
{ clientForm.setStatus(msg);
clientForm.setEnable(false);
} // end of disableClient()
private boolean sendMessage(String msg)
// the message format is "<length> msg" in byte form
{
System.out.println("sendMessage: " + msg);
try {
out.write(msg.length());
out.write(msg.getBytes());
return true;
}
catch (Exception e)
{ System.out.println("sendMessage(): " + e);
return false;
}
} // end of sendMessage()
private String readData()
/* Read a message in the form "<length> msg".
The length allows us to know exactly how many bytes to read
to get the complete message. Only the message part (msg) is
returned, or null if there's been a problem.
*/
{ byte[] data = null;
try {
int len = in.read(); // get the message length
if (len <= 0) {
System.out.println("Message Length Error");
return null;
}
data = new byte[len];
len = 0;
// read the message, perhaps requiring several read() calls
while (len != data.length) {
int ch = in.read(data, len, data.length - len);
if (ch == -1) {
System.out.println("Message Read Error");
return null;
}
len += ch;
}
}
catch (IOException e)
{ System.out.println("readData(): " + e);
return null;
}
// System.out.println("readData: " + new String(data));
return new String(data); // convert byte[] to String
} // end of readData()
} // end of EchoClient.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -