* 1.IP地址和端口连接服务器,获取服务器的数据,设置接听让外部处理数据
* 2.认证(类似于我们平常的登录),取得唯一认证值,方便于服务器发送信息;
* 3.发送数据,注意:发送的数据要特定协议,就是包含发送的信息等;
* 4.断开连接
/*
* @项目名: MyChat
* @包名: com.itheima.client
* @文件名: ClientConnector
* @创建者: 客户端的父类
* @创建时间: 2015/5/24 15:49
* @描述:
* 1.IP地址和端口连接服务器,获取服务器的数据,设置接听让外部处理数据
* 2.认证(类似于我们平常的登录),取得唯一认证值,方便于服务器发送信息;
* 3.发送数据,注意:发送的数据要特定协议,就是包含发送的信息等;
* 4.断开连接
*
*/
public class ClientConnector
{
private Socket client;
private String dstName;
private int dstPort;
private ConnectListener mConnectListener;
public ClientConnector(String dstName,
int dstPort)
{
this.dstName = dstName;
this.dstPort = dstPort;
}
/**
* 连接
*/
public void connect()
throws
IOException
{
if (client == null)
{
client = new Socket(dstName,
dstPort);
}
//获得输入流的数据
InputStream is = client.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1)
{
final String data = new String(buffer,
0,
len);
//获得数据,将数据推出去
if (mConnectListener != null)
{
mConnectListener.onReceiveData(data);
}
}
}
/**
* 认证
*/
public void auth(String authName)
throws
IOException
{
if (client != null)
{
//2. 数据通讯
OutputStream os = client.getOutputStream();
// #A
os.write(("#" + authName).getBytes());
}
}
/**
* 发送数据
*/
public void send(String receiver,
String data)
throws
IOException
{
if (client != null)
{
//2. 数据通讯
OutputStream os = client.getOutputStream();
// A # content
os.write((receiver + "#" + data).getBytes());
}
}
/**
* 断开连接
*/
public void disconnect()
throws
IOException
{
if (client != null)
{
client.close();
client = null;
}
}
public void setConnectListener(ConnectListener listener)
{
mConnectListener = listener;
}
public interface ConnectListener
{
void onReceiveData(String data);
}
}
使用该连接------------------
public class Activity_3 extends ActionBarActivity
implements ClientConnector.ConnectListener
{
String dstName = "10.0.2.2";//服务器的ip
int dstPort = 9999;//服务端端口
private EditText mEtContent;
private EditText mEtAuth;
private EditText mEtReciever;
private ClientConnector mConnector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtAuth = (EditText) findViewById(R.id.et_auth);
mEtContent = (EditText) findViewById(R.id.et_content);
mEtReciever = (EditText) findViewById(R.id.et_receiver);
mConnector = new ClientConnector(dstName,
dstPort);
mConnector.setConnectListener(this);
}
public void clickConnet(View view)
{
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
mConnector.connect();
} catch (IOException e)
{
e.printStackTrace();
}
}
}).start();
}
public void clickAuth(View view)
{
final String auth = mEtAuth.getText()
.toString()
.trim();
if (TextUtils.isEmpty(auth))
{
Toast.makeText(this,
"消息为空",
Toast.LENGTH_SHORT)
.show();
return;
}
try
{
mConnector.auth(auth);
} catch (IOException e)
{
e.printStackTrace();
}
}
public void clickDisconnet(View view)
{
try
{
mConnector.disconnect();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void clickSend(View view)
{
final String receiver = mEtReciever.getText()
.toString()
.trim();
final String content = mEtContent.getText()
.toString()
.trim();
if (TextUtils.isEmpty(content) || TextUtils.isEmpty(receiver))
{
Toast.makeText(this,
"消息为空",
Toast.LENGTH_SHORT)
.show();
return;
}
try
{
mConnector.send(receiver,
content);
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void onReceiveData(final String data)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(Activity_3.this,
data,
Toast.LENGTH_LONG)
.show();
}
});
}
}