A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨鹏鹏 中级黑马   /  2012-8-18 20:03  /  1242 人查看  /  2 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

public class TCPServer {

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
                // TODO Auto-generated method stub
          ServerSocket socket=new  ServerSocket(6000);
          Socket d= socket.accept();
          //输入流
          InputStream in=d.getInputStream();
          byte[] b=new byte[1024];
          int length=0;
          while(-1!=(length=in.read(b, 0, b.length))){
                  String s=new String(b,0,length);
                  System.out.println(s);
          }
           //输出流
          OutputStream ou=d.getOutputStream();
          ou.write("哈哈".getBytes());
          in.close();
          ou.close();
        }

}
package com.itheima.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class UDPClient {
        public static void main(String[] args) throws UnknownHostException, Exception {
      Socket so =new Socket("localHost",6000);
      //输出流
      OutputStream out=so.getOutputStream();
      out.write("喂:你好?".getBytes());
       //输入流
      InputStream in=so.getInputStream();
      byte[] b=new byte[1024];
      int length=0;
      while(-1!=(length=in.read(b, 0, b.length))){
              String say=new String(b,0,length);
              System.out.println(say);
      }
      out.close();
      in.close();
        }
   

}
控制台 只有 “喂 你好“,没有回复的 “哈哈” 怎么回事?

2 个回复

倒序浏览
没人回答吗
回复 使用道具 举报
这个有人问过了,你参照下:
tcp网络通信问题
http://bbs.itheima.com/forum.php ... 5&fromuid=36155
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马