首页 > 代码库 > java中tcp小样例
java中tcp小样例
服务端:
ServerSocket service = new ServerSocket(7777); Socket socket = service.accept(); InputStream input = socket.getInputStream(); OutputStream out = socket.getOutputStream(); BufferedInputStream bufinput = new BufferedInputStream(input); BufferedOutputStream bufout = new BufferedOutputStream(out); byte [] buf = new byte[1024]; int len = 0; StringBuffer sb = new StringBuffer(); String message = new String(buf,0,bufinput.read(buf)); System.out.println(message); out.write("服务端:\n小客你好啊!".getBytes());
client:
Socket client = new Socket("127.0.0.1",7777); OutputStream out = client.getOutputStream(); InputStream input = client.getInputStream(); BufferedOutputStream bufout = new BufferedOutputStream(out); BufferedInputStream bufinput = new BufferedInputStream(input); bufout.write("客户端:\n你好".getBytes()); bufout.flush(); byte [] buf = new byte [1024]; String message = new String(buf,0,bufinput.read(buf)); System.out.println(message);
java中tcp小样例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。