阅读:2484次
评论:2条
更新时间:2012-10-17
服务端开启一个端口,收到请求后立即初始化一个线程进行处理!
线程为一个内部类,不断从管道中读取字节流然后打印,并且将字节流内容加倍后返回!
客户端循环不断向服务端发送数据并接收返回数据进行打印!
至今运行看效果!
此示例为最简单的示例程序,不完善地方多指教!
服务端:
客户端:
服务端打印:
客户端打印:
线程为一个内部类,不断从管道中读取字节流然后打印,并且将字节流内容加倍后返回!
客户端循环不断向服务端发送数据并接收返回数据进行打印!
至今运行看效果!
此示例为最简单的示例程序,不完善地方多指教!
服务端:
package com.socket; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketAddress; import java.util.Arrays; /** * @说明 监听端口服务 * @author cuisuqiang * @version 1.0 * @since */ public class SocketService { public static void main(String[] args) { try { // 创建非邦定式连接对象 ServerSocket ss = new ServerSocket(); // 连接地址对象 SocketAddress address = new InetSocketAddress("192.168.9.69", 8001); // 将连接对象邦定到地址 ss.bind(address); System.out.println("邦定端口成功:" + address.toString()); boolean bRunning = true; while (bRunning) { // 接收请求 Socket s = ss.accept(); // 将请求指定一个线程去执行 new Thread(new SocketListener(s)).start(); } } catch (Exception e) { e.printStackTrace(); } } } /** * @说明 监听端口数据并打印 * @author cuisuqiang * @version 1.0 * @since */ class SocketListener implements Runnable { Socket socket = null; public SocketListener(Socket socket) { this.socket = socket; } @Override public void run() { System.err.println("开始处理:" + socket.toString()); try { InputStream ips = socket.getInputStream(); OutputStream ops = socket.getOutputStream(); while (true) { byte[] bts = readStream(ips); if (null != bts) { System.err.println("接收:" + Arrays.toString(bts)); byte[] btreturn = new byte[bts.length * 2]; System.arraycopy(bts, 0, btreturn, 0, bts.length); System.arraycopy(bts, 0, btreturn, bts.length - 1, bts.length); ops.write(btreturn); ops.flush(); } } } catch (Exception e) { e.printStackTrace(); } System.err.println("执行结束:" + socket.toString()); } /** * 读取流 5 秒超时 1 秒一次 */ public byte[] readStream(InputStream inStream) throws Exception { int count = 0; int index = 1; while (count == 0) { count = inStream.available(); index++; Thread.sleep(1 * 1000); if (index > 5 || count > 0) { break; } } if (count > 0) { byte[] b = new byte[count]; inStream.read(b); return b; } return null; } }
客户端:
package com.socket; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Arrays; /** * @说明 Socket客户端,连接成功后发送一组数据然后关闭连接 * @author cuisuqiang * @version 1.0 * @since */ public class SocketClient { public static void main(String[] args) { try { Socket socket = new Socket("192.168.9.69", 8001); socket.setKeepAlive(true); InputStream ips = socket.getInputStream(); OutputStream ops = socket.getOutputStream(); while(true){ byte[] remess = new byte[20]; for (int i = 0; i < 20; i++) { remess[i] = (byte) i; } ops.write(remess); byte[] bts = readStream(ips); if (null != bts) { System.err.println("返回:" + Arrays.toString(bts)); } Thread.sleep(2 * 1000); } } catch (Exception e) { e.printStackTrace(); } } /** * 读取流 5 秒超时 1 秒一次 */ public static byte[] readStream(InputStream inStream) throws Exception { int count = 0; int index = 1; while (count == 0) { count = inStream.available(); index++; Thread.sleep(1 * 1000); if (index > 5 || count > 0) { break; } } if (count > 0) { byte[] b = new byte[count]; inStream.read(b); return b; } return null; } }
服务端打印:
邦定端口成功:/192.168.9.69:8001 开始处理:Socket[addr=/192.168.9.69,port=5865,localport=8001] 接收:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 接收:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 接收:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
客户端打印:
返回:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0] 返回:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0] 返回:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0]
服务端为什么要指定IP
// 创建非邦定式连接对象 ServerSocket ss = new ServerSocket(); // 连接地址对象 SocketAddress address = new InetSocketAddress("192.168.9.69", 8001); // 将连接对象邦定到地址 ss.bind(address); System.out.println("邦定端口成功:" + address.toString());
也许大家看到的网上示例不是这么写的,而是直接邦定一个端口!
可是一台机器不可能只有一个网卡的,有的还可能设置了兼容IP!
为了不歧义,这里直接邦定某IP的某端口,当然这个IP运行环境的IP地址!
2 楼 cuisuqiang 2013-04-25 16:41
从他那边入门的,不过上面的代码是我自己写的
1 楼 douyacai_1987feng 2013-04-25 16:08