怎么对socket输出的信息以byte方式输出,接收以后再转为原类型

victory31 2005-10-28 09:08:52
就是客户端以byte的形式将各种类型的数据输出,服务器端接收以后再转换回相应的类型。
这个问题苦闷许久忘高手告知,本人系菜鸟。
下面附上服务器端客户端及转换的代码,忘达人帮我改一下,谢谢!



//: JabberServer.java
import java.io.*;
import java.net.*;
import sun.io.*;

public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8088;

public static void main(String[] args)
throws IOException {
//InetAddress addr = InetAddress.getByName(null);
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println("Connection accepted: "+ socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedInputStream inn = new BufferedInputStream(socket.getInputStream());
byte[] b =new byte[4];

// Output is automatically flushed
// by PrintWriter:
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
BufferedOutputStream outt = new BufferedOutputStream(socket.getOutputStream());
while (true) {
int ii = inn.read(b,0,1);
int strin;
strin = TypeConvers.byteToInt(b);
System.out.println(b);
System.out.println("ReceiveLength:"+ii);
System.out.println(strin);
/*
char[] bb = new char[1];

int readnum = in.read(bb);
byte[] aa = TypeConvers.charToByte(bb[0]);
System.out.println("Num:"+readnum + bb[0] );
System.out.print(bb);
int strin = TypeConvers.byteToChar(aa);
System.out.println(strin);
*/
/*String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
*/
String encoding="gb2312";
//byte b[]={(byte)'\u00c4',(byte)'\u00E3'};
char [] c=ByteToCharConverter.getConverter(encoding).convertAll(b);
for(int i=0;i<c.length;i++){
// System.out.println(Integer.toHexString(c[i]));
}


out.println(strin);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~


//: JabberClient.java

import java.net.*;
import java.io.*;
import java.util.Date;

public class JabberClient {
public static void main(String[] args)
throws IOException {
// Passing null to getByName() produces the
// special "Local Loopback" IP address, for
// testing on one machine w/o a network:
InetAddress addr = InetAddress.getByName(null);
// Alternatively, you can use
// the address or name:
// InetAddress addr =
// InetAddress.getByName("127.0.0.1");
// InetAddress addr =
// InetAddress.getByName("localhost");
System.out.println("addr = " + addr);

Socket socket = new Socket(addr, JabberServer.PORT);
// Guard everything in a try-finally to make
// sure that the socket is closed:


try {
System.out.println("socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
Date time = new Date();
Date time1;
//DateFormat mediumDateFormat = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM);
//sbeginTime = mediumDateFormat.format(time) ;
for(int i = 0; i < 10; i ++) {
byte[] ss = TypeConvers.intToByte(1);
//发送数据
out.println(ss);
int ii = TypeConvers.byteToInt(ss);
System.out.println(ii);
System.out.println(ss);
String str = in.readLine();
System.out.println(str);
}
time1 = new Date();
System.out.println(time1.getTime() - time.getTime());
out.println("END");
} finally {
System.out.println("closing...");
socket.close();
}
}


} ///:~




public class TypeConvers {
//int、char、double与byte相互转换的程序
//整数到字节数组的转换
public static byte[] intToByte(int number) {
int temp = number;
byte[] b=new byte[4];
for (int i=b.length-1;i>-1;i--){
b[i] = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}

//字节数组到整数的转换
public static int byteToInt(byte[] b) {
int s = 0;
for (int i = 0; i < 3; i++) {
if (b[i] >= 0)
s = s + b[i];
else


s = s + 256 + b[i];
s = s * 256;
}
if (b[3] >= 0) //最后一个之所以不乘,是因为可能会溢出
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}

//字符到字节转换
public static byte[] charToByte(char ch){
int temp=(int)ch;
byte[] b=new byte[2];
for (int i=b.length-1;i>-1;i--){
b[i] = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}

//字节到字符转换


public static char byteToChar(byte[] b){
int s=0;
if(b[0]>0)
s+=b[0];
else
s+=256+b[0];
s*=256;
if(b[1]>0)
s+=b[1];
else
s+=256+b[1];
char ch=(char)s;
return ch;
}

//浮点到字节转换
public static byte[] doubleToByte(double d){
byte[] b=new byte[8];
long l=Double.doubleToLongBits(d);
for(int i=0;i<b.length;i++){
b[i]=new Long(l).byteValue();
l=l>>8;


}
return b;
}

//字节到浮点转换
public static double byteToDouble(byte[] b){
long l;

l=b[0];
l&=0xff;
l|=((long)b[1]<<8);
l&=0xffff;
l|=((long)b[2]<<16);
l&=0xffffff;
l|=((long)b[3]<<24);
l&=0xffffffffl;
l|=((long)b[4]<<32);
l&=0xffffffffffl;

l|=((long)b[5]<<40);
l&=0xffffffffffffl;
l|=((long)b[6]<<48);


l|=((long)b[7]<<56);
return Double.longBitsToDouble(l);
}

}
...全文
334 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
takecare 2005-11-11
  • 打赏
  • 举报
回复
encode/decode成对即可
wxfJordan 2005-11-11
  • 打赏
  • 举报
回复
你在做有关Jabber的项目吗? 我也是. 我也在深索中, 如果你先解决请互相告知.
ngqzmjmj 2005-11-11
  • 打赏
  • 举报
回复
不知道楼主要做什么,如果要实现楼主的想法客户短发什么类型的信息,服务器断就接受什么类型的信息,那这样的信息就应该是实现知道的 比如客户断发送 "你好"5一个字符型的一个byte型的
那么服务器断只要按发送的顺序读就可以了,那么完全可以用DataInputStream,DataOutputStream 来实现
老無所依 2005-11-11
  • 打赏
  • 举报
回复
代码收下了, 向楼主学习,我也在探索中
doway 2005-10-29
  • 打赏
  • 举报
回复
楼主知道不知道 HTTP 协议是怎么发送数据的?是以纯文本发送的。
victory31 2005-10-28
  • 打赏
  • 举报
回复
希望各位高手不吝赐教啊,先谢谢了!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧