Ⅰ socket框架netty的使用,以及nio的实现原理,为什么是异步非阻塞
socket框架netty的使用,以及nio的实现原理,为什么是异步非阻塞
按Alt+F可进入File菜单, 如图:
File菜单的子菜单共有9项,分别叙述如下:
1. Load:装入一个文件, 可用类似DOS的通配符(如*.C)来进行列表选择。也可装入其它扩展名的文件, 只要给出文件名(或只给路径)即可。该项的热键为F3, 即只要按F3即可进入该项, 而不需要先进入File菜单再选此项。
2. Pick:将最近装入编辑窗口的8个文件列成一个表让用户选择, 选择后将该程序装入编辑区, 并将光标置在上次修改过的地方。其热健为Alt-F3。
3. New:新建文件, 缺省文件名为NONAME.C, 存盘时可改名。
4. Save:将编辑区中的文件存盘, 若文件名是NONAME.C时, 将询问是否更改文件名, 其热键为F2。
5. Write to:可由用户给出文件名将编辑区中的文件存盘, 若该文件已存在, 则询问要不要 覆盖。
6. Directory:显示目录及目录中的文件, 并可由用户选择。
7. Change dir:显示当前默认目录, 用户可以改变默认目录。
8. Os shell:暂时退出Turbo C 2.0到DOS提示符下, 此时可以运行DOS 命令, 若想回到 Turbo C 2.0中, 只要在DOS状态下键入EXIT即可。
9. Quit:退出Turbo C 2.0, 返回到DOS操作系统中, 其热键为Alt+X。
Ⅱ android端使用netty发送一个请求给服务端如何判断是否响应超时
用http协议发送get或者post请求,把需要发送的json字符串带上。最好用post方式
Ⅲ netty4 能不能运行在android上
是哪些
在修改androidpn,感觉xmpp协议太重了,而且据说很耗流量很耗电。
mina2两年没更新了!spring mvc 竟然还用的是2.5
= - =想迁移到netty4,把xmpp改成轻量的protobuf,但是不知道android上能否流畅使用netty4
netty4的jar包,服务端不要紧可以使用all-in-one,android端怎么使用?最小依赖是哪些
坐等大神解惑,感激不尽。
Ⅳ 怎么写个socket与netty通信
netty v3.9.4
websocket连接建立前,客户端需要与服务器进行握手(http协议) 确认websocket连接,也就是说在处理websocket请求前,必需要处理一些http请求。
websocket到现在为止,已经有多个版本,netty有相应的对应类,这部分处理一般不需要人工干预。
如果运行正常的话,会在页面的文本框中显示1-20记数。
可以通过firefox或chrome的开发人员工具,显看浏览器与服务器的交互。
主要是HttpServerChannelHandler2,加了些注释和少量debug代码。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.sl.demo.httpserver1;
import java.util.List;
import java.util.Map;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import org.jboss.netty.handler.codec.http.websocketx.;
public class HttpServerChannelHandler2 extends SimpleChannelHandler{
public static boolean debug = true;
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
Channel ch = e.getChannel();
Object msg = e.getMessage();
if(debug){
System.out.println("---------------");
System.out.println("message: "+msg.getClass());
}
//虽然是websocket,但在建立websocket连接前,先进行http握手,所以,这时也要处理http请求
//在http握手完成后,才是websocket下的通信
if(msg instanceof HttpRequest){
processHttpRequest(ch, (HttpRequest)msg);
}else if(msg instanceof WebSocketFrame){
processWebsocketRequest(ch,(WebSocketFrame)msg);
}else{
//未处理的请求类型
}
}
//这个方法:
//1.完成websocket前的http握手
//2.屏蔽掉非websocket握手请求
void processHttpRequest(Channel channel,HttpRequest request){
HttpHeaders headers = request.headers();
if(debug){
List<Map.Entry<String,String>> ls = headers.entries();
for(Map.Entry<String,String> i: ls){
System.out.println("header "+i.getKey()+":"+i.getValue());
}
}
//屏蔽掉非websocket握手请求
//只接受http GET和headers['Upgrade']为'websocket'的http请求
if(!HttpMethod.GET.equals(request.getMethod())
|| !"websocket".equalsIgnoreCase(headers.get("Upgrade"))){
DefaultHttpResponse resp = new DefaultHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.BAD_REQUEST);
channel.write(resp);
channel.close();
return;
}
wsShakerFactory = new (
"ws://"+request.headers().get(HttpHeaders.Names.HOST),
null,false );
WebSocketServerHandshaker wsShakerHandler = wsShakerFactory.newHandshaker(request);
if(null==wsShakerHandler){
//无法处理的websocket版本
wsShakerFactory.(channel);
}else{
//向客户端发送websocket握手,完成握手
//客户端收到的状态是101 sitching protocol
wsShakerHandler.handshake(channel, request);
}
}
//websocket通信
void processWebsocketRequest(Channel channel, WebSocketFrame request){
if(request instanceof CloseWebSocketFrame){
channel.close();
}else if(request instanceof PingWebSocketFrame){
channel.write(new PongWebSocketFrame(request.getBinaryData()));
}else if(request instanceof TextWebSocketFrame){
//这个地方 可以根据需求,加上一些业务逻辑
TextWebSocketFrame txtReq = (TextWebSocketFrame) request;
if(debug){ System.out.println("txtReq:"+txtReq.getText());}
//向ws客户端发送多个响应
for(int i=1; i<=20; i++){
channel.write(new TextWebSocketFrame(""+i));
try{Thread.sleep(300);}catch(Exception ex){}
}
}else{
//WebSocketFrame还有一些
}
}
}
其他类跟网上的差不多: http://blog.csdn.net/shagoo/article/details/8028813
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.sl.demo.httpserver1;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class HttpServer implements Runnable{
int port = 80;
public HttpServer(int port){
this.port = port;
}
@Override
public void run() {
ServerBootstrap b = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
b.setPipelineFactory(new ());
b.setOption("child.tcpNoDelay", true);
b.setOption("child.keepAlive", true);
b.bind(new InetSocketAddress(port));
}
public static void main(String[] args){
new HttpServer(80).run();
}
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.sl.demo.httpserver1;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket00FrameDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
public class implements ChannelPipelineFactory {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline cp = Channels.pipeline();
cp.addLast("decoder", new HttpRequestDecoder());
// cp.addLast("decoder", new WebSocket00FrameDecoder());
cp.addLast("encoder", new HttpResponseEncoder());
// cp.addLast("downhandler", new HttpServerDownstreamHandler());
// cp.addLast("uphandler", new HttpServerUpstreamHandler());
cp.addLast("handler", new HttpServerChannelHandler2());
return cp;
}
}
测试页面:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<html>
<head>
<script >
function connect1(){
alert('connect1');
var ta = document.getElementById('responseText');
var socket = new WebSocket('ws://127.0.0.1/websocket');
if (window.WebSocket) {
}else{
alert('Your browser does not support Web Socket.');
return;
}
socket.onopen = function(event) {
ta.value = "Web Socket opened!";
}
socket.onmessage = function(event) {
ta.value = event.data;
};
socket.onclose = function(event) {
ta.value = "Web Socket closed";
};
}
function connect() {
alert('connect');
var socket;
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}
if (window.WebSocket) {
socket = new WebSocket("ws://127.0.0.1/websocket");
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = event.data;
};
socket.onopen = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket opened!";
socket.send('hello');
};
socket.onclose = function(event) {
var ta = document.getElementById('responseText');
ta.value = "Web Socket closed";
};
socket.onerror = function(event){
};
} else {
alert("Your browser does not support Web Socket.");
}
}
</script>
</head>
<body>
<input type="button" onclick="connect1()" value="ws connect1"/> <br/><br/>
<input type="button" onclick="connect()" value="ws connect"/> <br/><br/>
<input id="responseText" type="text" value="123456"/>
</body>
</html>
Ⅳ Android的Socket是全双工的吗
是的,可以同时读写的,这个“同时”读写 有点虚,计算机执行的时候是有先后的,你可以开启2个线程 进行socket读写,一个线程读,一个线程写,不知道你的业务上是如何处理的,如果高并发可以考虑 MIMA NETTY框架,具体还是要看代码的实现方式,socket只是套接字,全双工是指同一个socket连接 同时完成收和发的功能
Ⅵ android上的socket通信的开源框架有哪些
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,它在socket的基础上根据各种常用的应用协议又进一步封装,提供更便利的接口。如果需要快速搭建一个C/S服务框架,那Netty过来用是没错。 反过来你的情况是需要学习这个课程,你应该掌握基本的socket编程及其通信原理,所以学习时直接用socket编程比较好。也许哪一天,你灵感来了,编出一个比Netty更好的框架,一个更牛的软件。
Ⅶ android netty怎么抓包
import static org.jboss.netty.channel.Channels.pipeline;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.stream.ChunkedWriteHandler;
/**
* 后台管理服务
*
* @author javagg
*
*/
public class AdminServer {
public static void main(String[] args) {
start(8080);
}
public static void start(int port) {
// 配置服务器-使用java线程池作为解释线程
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
// 设置 pipeline factory.
bootstrap.setPipelineFactory(new ServerPipelineFactory());
// 绑定端口
bootstrap.bind(new InetSocketAddress(port));
System.out.println(“admin start on ”+port);
}
private static class ServerPipelineFactory implements
ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast(“decoder”, new HttpRequestDecoder());
pipeline.addLast(“encoder”, new HttpResponseEncoder());
//http处理handler
pipeline.addLast(“handler”, new AdminServerHandler());
return pipeline;
}
}
}
2. [代码]AdminServerHandler.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.util.HashMap;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.buffer.DynamicChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.util.CharsetUtil;
/**
* @author javagg
*/
public class AdminServerHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
HttpRequest request = (HttpRequest) e.getMessage();
String uri = request.getUri();
System.out.println(“uri:”+uri);
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
ChannelBuffer buffer=new DynamicChannelBuffer(2048);
buffer.writeBytes(“hello, oschina”.getBytes(“UTF-8″));
response.setContent(buffer);
response.setHeader(“Content-Type”, ”text/html; charset=UTF-8″);
response.setHeader(“Content-Length”, response.getContent().writerIndex());
Channel ch = e.getChannel();
// Write the initial line and the header.
ch.write(response);
ch.disconnect();
ch.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
Channel ch = e.getChannel();
Throwable cause = e.getCause();
if (cause instanceof TooLongFrameException) {
sendError(ctx, BAD_REQUEST);
return;
}
cause.printStackTrace();
if (ch.isConnected()) {
sendError(ctx, INTERNAL_SERVER_ERROR);
}
}
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
response.setHeader(CONTENT_TYPE, ”text/plain; charset=UTF-8″);
response.setContent(ChannelBuffers.copiedBuffer(“Failure: ” + status.toString() + ”\r\n”, CharsetUtil.UTF_8));
// Close the connection as soon as the error message is sent.
ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
}
}
Ⅷ android 怎么使用netty
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。也就是说,Netty是一个基于NIO的客户,服务器端编程框架,它在socket的基础上根据各种常用的应用协议又进一步封装,提供更便利的接口。如果需要快速搭建一个C/S服务框架,那Netty过来用是没错。反过来你的情况是需要学习这个课程,你应该掌握基本的socket编程及其通信原理,所以学习时直接用socket编程比较好。也许哪一天,你灵感来了,编出一个比Netty更好的框架,一个更牛的软件。
Ⅸ Android的Socket是全双工的吗
可以同时读写的,这个“同时”读写 有点虚,计算机执行的时候是有先后的,可以开启2个线程 进行socket读写,
一个线程读,一个线程写,不知道个人的业务上是如何处理的,如果高并发可以考虑 MIMA NETTY框架