Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
netty
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
netty
Commits
a2d1ccaa
Commit
a2d1ccaa
authored
Sep 02, 2017
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
264 additions
and
0 deletions
+264
-0
pom.xml
pom.xml
+40
-0
src/main/java/com/paul0523/App.java
src/main/java/com/paul0523/App.java
+13
-0
src/main/java/com/paul0523/TcpClient.java
src/main/java/com/paul0523/TcpClient.java
+61
-0
src/main/java/com/paul0523/TcpServer.java
src/main/java/com/paul0523/TcpServer.java
+112
-0
src/test/java/com/paul0523/AppTest.java
src/test/java/com/paul0523/AppTest.java
+38
-0
No files found.
pom.xml
0 → 100644
View file @
a2d1ccaa
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.paul0523
</groupId>
<artifactId>
netty
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
netty
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/paul0523/App.java
0 → 100644
View file @
a2d1ccaa
package
com
.
paul0523
;
/**
* Hello world!
*
*/
public
class
App
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello World!"
);
}
}
src/main/java/com/paul0523/TcpClient.java
0 → 100644
View file @
a2d1ccaa
package
com
.
paul0523
;
import
com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl
;
import
java.io.BufferedReader
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.net.Socket
;
import
java.nio.charset.Charset
;
public
class
TcpClient
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
TcpClient
().
start
();
}
public
void
start
()
throws
Exception
{
Socket
socket
=
new
Socket
(
"127.0.0.1"
,
8080
);
new
MyThread
(
socket
).
start
();
while
(
true
)
{
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
String
line
=
bufferedReader
.
readLine
();
OutputStream
outputStream
=
socket
.
getOutputStream
();
if
(
"bye"
.
equals
(
line
))
{
outputStream
.
write
(
line
.
getBytes
(
"utf-8"
));
break
;
}
outputStream
.
write
(
line
.
getBytes
(
"utf-8"
));
}
}
class
MyThread
extends
Thread
{
private
Socket
socket
;
public
MyThread
(
Socket
socket
)
{
this
.
socket
=
socket
;
}
@Override
public
void
run
()
{
super
.
run
();
try
{
byte
[]
bytes
=
new
byte
[
1024
];
InputStream
inputStream
=
socket
.
getInputStream
();
int
count
=
0
;
while
((
count
=
inputStream
.
read
(
bytes
))
!=
-
1
)
{
System
.
out
.
println
(
new
String
(
bytes
,
0
,
count
,
Charset
.
forName
(
"utf-8"
)));
}
socket
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
src/main/java/com/paul0523/TcpServer.java
0 → 100644
View file @
a2d1ccaa
package
com
.
paul0523
;
import
java.io.BufferedReader
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.SelectionKey
;
import
java.nio.channels.Selector
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.Set
;
public
class
TcpServer
{
private
ServerSocketChannel
serverSocketChannel
;
private
Selector
selector
;
private
SelectionKey
selectionKey
;
private
Map
<
SelectionKey
,
String
>
cache
=
new
HashMap
<>();
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
TcpServer
().
start
();
}
public
TcpServer
()
throws
Exception
{
serverSocketChannel
=
ServerSocketChannel
.
open
();
serverSocketChannel
.
bind
(
new
InetSocketAddress
(
8080
));
serverSocketChannel
.
configureBlocking
(
false
);
selector
=
Selector
.
open
();
selectionKey
=
serverSocketChannel
.
register
(
selector
,
SelectionKey
.
OP_ACCEPT
);
}
public
void
start
()
throws
Exception
{
while
(
true
)
{
selector
.
select
();
for
(
Iterator
<
SelectionKey
>
iterator
=
selector
.
selectedKeys
().
iterator
();
iterator
.
hasNext
();
)
{
SelectionKey
key
=
iterator
.
next
();
if
(
key
.
isAcceptable
())
{
handleAccept
(
key
);
}
else
if
(
key
.
isReadable
())
{
handleRead
(
key
);
}
else
if
(
key
.
isWritable
())
{
handleWrite
(
key
);
}
else
if
(
key
.
isConnectable
())
{
handleConnet
(
key
);
}
iterator
.
remove
();
}
}
}
private
void
handleConnet
(
SelectionKey
key
)
throws
Exception
{
System
.
out
.
println
(
"建立连接"
);
}
private
void
handleWrite
(
SelectionKey
key
)
throws
Exception
{
System
.
out
.
println
(
"向通道写数据。。。"
);
ByteBuffer
byteBuffer
=
(
ByteBuffer
)
key
.
attachment
();
SocketChannel
socketChannel
=
(
SocketChannel
)
key
.
channel
();
socketChannel
.
write
(
byteBuffer
);
key
.
interestOps
(
SelectionKey
.
OP_READ
);
}
private
void
handleRead
(
SelectionKey
key
)
throws
Exception
{
SocketChannel
socketChannel
=
(
SocketChannel
)
key
.
channel
();
System
.
out
.
println
(
cache
.
containsKey
(
key
));
ByteBuffer
byteBuffer
=
(
ByteBuffer
)
key
.
attachment
();
byteBuffer
.
clear
();
int
readbytes
=
socketChannel
.
read
(
byteBuffer
);
byteBuffer
.
flip
();
if
(
readbytes
>
0
)
{
byte
[]
bytes
=
new
byte
[
readbytes
];
byteBuffer
.
get
(
bytes
,
0
,
readbytes
);
System
.
out
.
println
(
new
String
(
bytes
,
0
,
readbytes
,
Charset
.
forName
(
"utf-8"
)));
}
else
if
(
readbytes
==
-
1
)
{
socketChannel
.
close
();
return
;
}
byteBuffer
.
flip
();
key
.
attach
(
byteBuffer
);
forCast
(
byteBuffer
);
key
.
interestOps
(
SelectionKey
.
OP_WRITE
);
}
private
void
forCast
(
ByteBuffer
byteBuffer
)
{
Set
<
SelectionKey
>
keys
=
selector
.
keys
();
for
(
SelectionKey
key
:
keys
)
{
if
(
key
==
selectionKey
)
{
continue
;
}
ByteBuffer
newByteBuffer
=
ByteBuffer
.
wrap
(
byteBuffer
.
array
());
newByteBuffer
.
position
(
0
);
newByteBuffer
.
limit
(
byteBuffer
.
limit
());
key
.
attach
(
newByteBuffer
);
key
.
interestOps
(
key
.
interestOps
()
|
SelectionKey
.
OP_WRITE
);
}
}
private
void
handleAccept
(
SelectionKey
key
)
throws
Exception
{
SocketChannel
socketChannel
=
((
ServerSocketChannel
)
key
.
channel
()).
accept
();
socketChannel
.
configureBlocking
(
false
);
SelectionKey
selectionKey
=
socketChannel
.
register
(
key
.
selector
(),
SelectionKey
.
OP_READ
,
ByteBuffer
.
allocate
(
1024
));
cache
.
put
(
selectionKey
,
null
);
}
}
src/test/java/com/paul0523/AppTest.java
0 → 100644
View file @
a2d1ccaa
package
com
.
paul0523
;
import
junit.framework.Test
;
import
junit.framework.TestCase
;
import
junit.framework.TestSuite
;
/**
* Unit test for simple App.
*/
public
class
AppTest
extends
TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public
AppTest
(
String
testName
)
{
super
(
testName
);
}
/**
* @return the suite of tests being tested
*/
public
static
Test
suite
()
{
return
new
TestSuite
(
AppTest
.
class
);
}
/**
* Rigourous Test :-)
*/
public
void
testApp
()
{
assertTrue
(
true
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment