如何使用java socket来传输自定义的数据包?

[复制链接]
查看11 | 回复2 | 2011-5-19 12:49:37 | 显示全部楼层 |阅读模式
最好有示例代码。。要是可行,可多追加分。。邮箱:[email protected]
回复

使用道具 举报

千问 | 2011-5-19 12:49:37 | 显示全部楼层
自定义是什么意思?是指定义数据包的大小,还是数据包中内容的格式









<h4class=\"ask\">追问


是的。。就是包括包头和包体都是自己定义的。。
字段名        字节数        类型        描述
Total_Length        4        Integer        为消息的总长度
Value        2Integer        消息操作的本地值
Sequence_ID        2        Integer        序列号(从1开始计数)//包头部分;包体就是传输的实际内容
回复

使用道具 举报

千问 | 2011-5-19 12:49:37 | 显示全部楼层
随便写了个没做测试,你看看,照我的思路想想
服务端
publicclassServer{
        publicstaticvoidmain(String[]args){
                        try{
                        ServerSocketserverSocket=newServerSocket(2345);
                        Socketsocket=null;
                        while(true){
                                socket=serverSocket.accept();
                                InputStreamis=socket.getInputStream();
                                BufferedInputStreambis=newBufferedInputStream(is,16);
                                byte[]b=newbyte[16];
                                while((bis.read(b))!=-1){
                                        for(inti=0;i16;i)
                                        System.out.print((char)b);
                                        System.out.println();
                                }
                        }
                }catch(IOExceptione){
                        e.printStackTrace();
                }
        }

}
客户端
publicclassClient{
        publicstaticvoidmain(String[]args){
                try{
                        Socketsocket=newSocket(InetAddress.getByName(\"localhost\"),2345);
                        OutputStreamos=socket.getOutputStream();
                        BufferedOutputStreambos=newBufferedOutputStream(os,16);
                        byte[]b=newbyte[16];
                        Stringnews=\"helloworld!myfriend.\";
                        byte[]bb=news.getBytes();
                        intlen=bb.length;
                        intseq=0;
                        intstart=0;
                        while(len0){//16位78位没有定义
                                seq;
                                if(len7){
                                        for(intj=0;j4;j){//0-3
                                                b[j]=1;
                                        }
                                        for(inti=8;i16;i){//8-15
                                                b=bb[start];
                                                start;
                                        }
                                        len=len-8;
                                }else{
                                        intm=len8;//0-3
                                        intj=3;
                                        while(m0){
                                                b[j]=(byte)(m%2);
                                                m=m/2;
                                                j--;
                                        }
                                        while(j=0){
                                                b[j]=0;
                                                j--;
                                        }//end
                                        for(inti=8;i8len;i){//8-15
                                                b=bb[start];
                                                start;
                                        }
                                        len=0;
                                }
                                intj=5;//45
                                inttemp=seq;
                                while(j!=3){
                                        b[j]=(byte)(temp%2);
                                        temp=temp/2;
                                        j--;
                                }
                                while(j!=3){
                                        b[j]=0;
                                        j--;
                                }//end
                                bos.write(b);//发送数据包
                        }
                        socket.close();               
                }catch(UnknownHostExceptione){
                        e.printStackTrace();
                }catch(IOExceptione){e.printStackTrace();}}}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行