标准Java ServerSocket作为服务器,Netty作为客户端进行传输数据

3 篇文章 0 订阅

用Java标准的ServerSocket写一个简单的服务器,该服务器绑定本地端口80等待客户端连接,服务器收到客户端Socket连接后只简单的发一个“hello,world”后就断开连接。

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class JavaServer {
	public JavaServer() throws Exception {
		ServerSocket serverSocket = new ServerSocket(80);

		while (true) {
			Socket socket = serverSocket.accept();
			System.out.println("接受连接");

			OutputStream oStream = socket.getOutputStream();

			oStream.write("hello,world!".getBytes());
			oStream.close();
			socket.close();

			System.out.println("数据发送完毕");
		}
	}

	public static void main(String[] args) {
		try {
			new JavaServer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

现在开始用netty写一个客户端,该客户端对服务器端发起连接,连接成功后把收到的数据用标准Java的Logger打印出来:

import java.net.InetSocketAddress;
import java.util.logging.Logger;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

public class Client {
	public Client() throws Exception {
		String host = "127.0.0.1";
		int port = 80;

		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port))
					.handler(new ChannelInitializer<SocketChannel>() {
						@Override
						public void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast(new MyClientHandler());
						}
					});

			ChannelFuture f = b.connect().sync();
			f.channel().closeFuture().sync();
		} finally {
			group.shutdownGracefully().sync();
		}
	}

	private class MyClientHandler extends ChannelInboundHandlerAdapter {

		private final Logger logger = Logger.getLogger(MyClientHandler.class.getName());

		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			ByteBuf in = (ByteBuf) msg;
			logger.info(in.toString(CharsetUtil.UTF_8));
		}

		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // 4
			cause.printStackTrace();
			ctx.close();
		}
	}

	public static void main(String[] args) {
		try {
			new Client();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

收到数据:

zhangphil.server.Client$MyClientHandler channelRead
信息: hello,world!

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值