Android中Socket通讯类
public class SocketClient
{
public boolean mClientFlag = false;
private static Socket client;
public SocketClient()
{
Log.d("WineStock", "WineStock SocketClient()");
client = new Socket();
}
public SocketClient(String HostName, int iPort)
{
client=new Socket();
SocketAddress socketAddress = new InetSocketAddress(HostName, iPort);
try
{
Log.d("WineStock", "WineStock SocketClient connect");
client.connect(socketAddress, 3000);
}
catch (IOException e) {
// TODO Auto-generated catch block
Log.d("WineStock", "WineStock SocketClient IOException");
} catch (IllegalArgumentException e1) {
// TODO: handle exception
Log.d("WineStock", "WineStock SocketClient IllegalArgumentException ");
}
return ;
}
public boolean SocketConnect(String HostName, int iPort)
{
SocketAddress socketAddress = new InetSocketAddress(HostName, iPort);
try
{
Log.d("WineStock", "WineStock SocketConnect connect ");
client.connect(socketAddress, 3000);
}
catch (IOException e) {
// TODO Auto-generated catch block
Log.d("WineStock", "WineStock SocketConnect IOException ");
} catch (IllegalArgumentException e1) {
// TODO: handle exception
Log.d("WineStock", "WineStock SocketConnect IllegalArgumentException ");
}
return true;
}
public void SocketClose()
{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("WineStock", "socket close error"+e.getMessage());
}
}
// 建立连接
public static boolean SClient(SocketSendModel SendModel) throws UnknownHostException, IOException
{
boolean bRet = false;
try
{
if(client.isConnected())
{
//发送请求
sendMessage(SendModel);
bRet = true;
}
}
catch (UnknownHostException e)
{
Log.d("WineStock", "socket SClienterror"+e.getMessage());
}
catch (IOException e1)
{
Log.d("WineStock", "socket SClienterror"+e.getMessage());
}
return bRet;
}
// 发送请求www.zzzyk.com
private static void sendMessage(SocketSendModel model) throws IOException
{
//boolean bRet = false;
try
{
PrintWriter out = new PrintWriter(client.getOutputStream());
out.print(objToJsonString(model).replace("\n", " "));
out.flush();
//bRet = true;
}
catch (IOException e)
{
}
return;
}
&nb
补充:移动开发 , Android ,