Ⅰ 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框架