因为考虑到数据传送的安全性.所以,用字节流进行socket的传输..例子如下:
客户端:
//获得流
byteOut = new ByteArrayOutputStream();
out = new DataOutputStream(byteOut);
//转为字节流
byte[] messes=message.getBytes("GBK");
//获得长度
int length=messes.length;
//把欲发送的长度转换成字节流
byte[] lengthbytes = ByteUtil.integerToBytes(length, 4);
//System.out.println("将要写过去的数据为:"+messes.length);
System.out.println("开始写socket到后台============================");
//把长度写过去
byteOut.write(lengthbytes);
//把内容写过去
byteOut.write(messes);
out.flush();
附ByteUtil方法:
public static byte[] integerToBytes(int integer, int len) {
// if (integer < 0) { throw new IllegalArgumentException("Can not cast negative to bytes : " + integer); }
ByteArrayOutputStream bo = new ByteArrayOutputStream();
for (int i = 0; i < len; i ++) {
bo.write(integer);
integer = integer >> 8;
}
return bo.toByteArray();
}
服务端:
DataInputStream in = new DataInputStream(receiver.getSocket() .getInputStream());
//读取长度
int len=ByteUtil.bytesToInteger(ByteUtil.readBytes(in,4));
//读取内容
String mess = new String(ByteUtil.readBytes(in, len)).trim();
附换转读取方法--读取字节方法:
public static byte[] readBytes(InputStream in, long length) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while (read < length) {
int cur = in.read(buffer, 0, (int)Math.min(1024, length - read));
if (cur < 0) { break; }
read += cur;
bo.write(buffer, 0, cur);
}
return bo.toByteArray();
}
public static int bytesToInteger(byte[] bytes)
{
return bytesToInteger(bytes, 0, bytes.length);
}
读取String方法:
public static byte[] readBytes(InputStream in, long length) throws IOException {
// 写完以后进行读操作
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(), "GBK"));
// 设置超时间为10秒
client.setSoTimeout(100 * 1000);
StringBuffer sb = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
writer.close();
br.close();
client.close();
return sb.toString();
}
这样写的socket程序的可容错性会更强.
http://hi.baidu.com/qinghua9/blog/item/71109422a9ae8dfbd7cae288.html
客户端:
//获得流
byteOut = new ByteArrayOutputStream();
out = new DataOutputStream(byteOut);
//转为字节流
byte[] messes=message.getBytes("GBK");
//获得长度
int length=messes.length;
//把欲发送的长度转换成字节流
byte[] lengthbytes = ByteUtil.integerToBytes(length, 4);
//System.out.println("将要写过去的数据为:"+messes.length);
System.out.println("开始写socket到后台============================");
//把长度写过去
byteOut.write(lengthbytes);
//把内容写过去
byteOut.write(messes);
out.flush();
附ByteUtil方法:
public static byte[] integerToBytes(int integer, int len) {
// if (integer < 0) { throw new IllegalArgumentException("Can not cast negative to bytes : " + integer); }
ByteArrayOutputStream bo = new ByteArrayOutputStream();
for (int i = 0; i < len; i ++) {
bo.write(integer);
integer = integer >> 8;
}
return bo.toByteArray();
}
服务端:
DataInputStream in = new DataInputStream(receiver.getSocket() .getInputStream());
//读取长度
int len=ByteUtil.bytesToInteger(ByteUtil.readBytes(in,4));
//读取内容
String mess = new String(ByteUtil.readBytes(in, len)).trim();
附换转读取方法--读取字节方法:
public static byte[] readBytes(InputStream in, long length) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while (read < length) {
int cur = in.read(buffer, 0, (int)Math.min(1024, length - read));
if (cur < 0) { break; }
read += cur;
bo.write(buffer, 0, cur);
}
return bo.toByteArray();
}
public static int bytesToInteger(byte[] bytes)
{
return bytesToInteger(bytes, 0, bytes.length);
}
读取String方法:
public static byte[] readBytes(InputStream in, long length) throws IOException {
// 写完以后进行读操作
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(), "GBK"));
// 设置超时间为10秒
client.setSoTimeout(100 * 1000);
StringBuffer sb = new StringBuffer();
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
writer.close();
br.close();
client.close();
return sb.toString();
}
这样写的socket程序的可容错性会更强.
http://hi.baidu.com/qinghua9/blog/item/71109422a9ae8dfbd7cae288.html