想使用socket实现C向S传1,然后S向C传2,

立志做一个佳娃~ 2017-08-07 04:05:59
想使用socket实现C向S传1,然后S向C传2,然后C向S传3,然后S向C传4的效果,,,

这个效果百度上也没有,听说要使用自定义协议来搞定,特来请教高手,指点一下!
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaTree2017 2017-08-07
  • 打赏
  • 举报
回复
客户端代码
public class client {



    public static void main(String[] args) throws IOException {
        Socket client = new Socket("localhost",111);
        //创建socket连接

        InputStream inputStream = client.getInputStream();
        OutputStream outputStream = client.getOutputStream();
        byte[] integer = new byte[4];
        int i = 1;
        while(true){
            //发送数据 也可以发送字符串String s = 你想说的话;然后:s.getBytes("utf-8")获得byte数组,然后用write方法写
            outputStream.write( new byte[] {
                    (byte) ((i >> 24) & 0xFF),
                    (byte) ((i >> 16) & 0xFF),
                    (byte) ((i >> 8) & 0xFF),
                    (byte) (i & 0xFF)
            });
            outputStream.flush();

            System.out.println("向服务器传输:" + i);
            //和服务器一样的
            inputStream.read(integer);
            i = (integer[0] & 0xff) << 24 |
                    (integer[1] & 0xff) << 16 |
                    (integer[2] & 0xff) << 8 |
                    (integer[3] & 0xff);
            i++;
            System.out.println("获得服务器数据:" + i);
        }
    }
}
JavaTree2017 2017-08-07
  • 打赏
  • 举报
回复
服务端代码:
public class Main {

    public static void main(String[] args) throws IOException {
	// write your code here
        ServerSocket serverSocket = new ServerSocket(111);

        while(true){
            Socket client = serverSocket.accept();
            
            //创建新线程处理客户端请求
            new Thread(()->{
                try {
                    InputStream inputStream = client.getInputStream();
                    OutputStream outputStream = client.getOutputStream();
                    //获取输入输出流
                    byte[] integer = new byte[4];
                    int i = 0;
                    while(i<10){
                        inputStream.read(integer);
                        //阻塞方式读取数据(如果是对话,那么上面的byte数组的大小可以适当增加,比如加到 2048,视情况而定
                        //String s = new String(integer,"utf-8");   这种方式可以获取字符串字节
                        i = (integer[0] & 0xff) << 24 |
                                (integer[1] & 0xff) << 16 |
                                (integer[2] & 0xff) << 8 |
                                (integer[3] & 0xff);
                        System.out.println("获得客户端信息:" + i);
                        i++;
                        //outputStream.write(s.getBytes("utf-8"));写入输出流 
                        outputStream.write( new byte[] {
                                (byte) ((i >> 24) & 0xFF),
                                (byte) ((i >> 16) & 0xFF),
                                (byte) ((i >> 8) & 0xFF),
                                (byte) (i & 0xFF)
                        });
                        //清空缓冲区
                        outputStream.flush();
                        System.out.println("向客户端发送:" + i);
                    }
                }catch(Exception e){

                }
            }).start();
        }
    }
}
public class Main {

    public static void main(String[] args) throws IOException {
	// write your code here
        ServerSocket serverSocket = new ServerSocket(111);

        while(true){
            Socket client = serverSocket.accept();
            
            //创建新线程处理客户端请求
            new Thread(()->{
                try {
                    InputStream inputStream = client.getInputStream();
                    OutputStream outputStream = client.getOutputStream();
                    //获取输入输出流
                    byte[] integer = new byte[4];
                    int i = 0;
                    while(i<10){
                        inputStream.read(integer);
                        //阻塞方式读取数据(如果是对话,那么上面的byte数组的大小可以适当增加,比如加到 2048,视情况而定
                        //String s = new String(integer,"utf-8");   这种方式可以获取字符串字节
                        i = (integer[0] & 0xff) << 24 |
                                (integer[1] & 0xff) << 16 |
                                (integer[2] & 0xff) << 8 |
                                (integer[3] & 0xff);
                        System.out.println("获得客户端信息:" + i);
                        i++;
                        //outputStream.write(s.getBytes("utf-8"));写入输出流 
                        outputStream.write( new byte[] {
                                (byte) ((i >> 24) & 0xFF),
                                (byte) ((i >> 16) & 0xFF),
                                (byte) ((i >> 8) & 0xFF),
                                (byte) (i & 0xFF)
                        });
                        //清空缓冲区
                        outputStream.flush();
                        System.out.println("向客户端发送:" + i);
                    }
                }catch(Exception e){

                }
            }).start();
        }
    }
}
JavaTree2017 2017-08-07
  • 打赏
  • 举报
回复
引用 2 楼 ghyghost 的回复:
[quote=引用 1 楼 shengming0302 的回复:] 问题不是很清楚 是说获得数,然后加一返回给客户端/服务器的意思? 如果这样的话,那就用socket获取输入流然后read,得到的byte转成int然后+1再传到输出流去就行了
如果是对话的效果呢??没有+1的效果,,双方说的话String长度不一样,![/quote]服务器一句客户端一句那种,还是类似聊天的形式 任何一方都可以随时说话
  • 打赏
  • 举报
回复
引用 1 楼 shengming0302 的回复:
问题不是很清楚 是说获得数,然后加一返回给客户端/服务器的意思? 如果这样的话,那就用socket获取输入流然后read,得到的byte转成int然后+1再传到输出流去就行了
如果是对话的效果呢??没有+1的效果,,双方说的话String长度不一样,!
JavaTree2017 2017-08-07
  • 打赏
  • 举报
回复
问题不是很清楚 是说获得数,然后加一返回给客户端/服务器的意思? 如果这样的话,那就用socket获取输入流然后read,得到的byte转成int然后+1再传到输出流去就行了

62,615

社区成员

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

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