Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
misc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
misc
Commits
29a5c941
Commit
29a5c941
authored
May 05, 2016
by
Elvis Pranskevichus
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from gallir/master
A simpler and perhaps faster server
parents
eb6e8940
ab01099e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
40 deletions
+29
-40
servers/goecho.go
servers/goecho.go
+29
-40
No files found.
servers/goecho.go
View file @
29a5c941
...
...
@@ -3,52 +3,41 @@
package
main
import
(
"ne
t"
"strconv
"
"fmt
"
"fm
t"
"net
"
"strconv
"
)
const
PORT
=
25000
func
main
()
{
server
,
err
:=
net
.
Listen
(
"tcp"
,
":"
+
strconv
.
Itoa
(
PORT
))
if
server
==
nil
{
panic
(
"couldn't start listening: "
+
err
.
Error
())
}
conns
:=
clientConns
(
server
)
for
{
go
handleConn
(
<-
conns
)
}
}
func
clientConns
(
listener
net
.
Listener
)
chan
net
.
Conn
{
ch
:=
make
(
chan
net
.
Conn
)
i
:=
0
go
func
()
{
for
{
client
,
err
:=
listener
.
Accept
()
if
client
==
nil
{
fmt
.
Printf
(
"couldn't accept: "
+
err
.
Error
())
continue
}
i
++
fmt
.
Printf
(
"%d: %v <-> %v
\n
"
,
i
,
client
.
LocalAddr
(),
client
.
RemoteAddr
())
ch
<-
client
}
}()
return
ch
server
,
err
:=
net
.
Listen
(
"tcp"
,
":"
+
strconv
.
Itoa
(
PORT
))
if
server
==
nil
{
panic
(
"couldn't start listening: "
+
err
.
Error
())
}
i
:=
0
for
{
client
,
err
:=
server
.
Accept
()
if
client
==
nil
{
fmt
.
Printf
(
"couldn't accept: "
+
err
.
Error
())
continue
}
i
++
fmt
.
Printf
(
"%d: %v <-> %v
\n
"
,
i
,
client
.
LocalAddr
(),
client
.
RemoteAddr
())
go
handleConn
(
client
)
}
}
func
handleConn
(
client
net
.
Conn
)
{
buf
:=
make
([]
byte
,
102400
)
for
{
reqLen
,
err
:=
client
.
Read
(
buf
)
if
err
!=
nil
{
break
}
if
reqLen
>
0
{
client
.
Write
(
buf
[
:
reqLen
])
}
}
defer
client
.
Close
()
buf
:=
make
([]
byte
,
102400
)
for
{
reqLen
,
err
:=
client
.
Read
(
buf
)
if
err
!=
nil
{
break
}
if
reqLen
>
0
{
client
.
Write
(
buf
[
:
reqLen
])
}
}
}
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