Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tcpserver
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
substrate
tcpserver
Commits
f16bdb28
Commit
f16bdb28
authored
Mar 23, 2021
by
xgrowingpains
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
c1c26c64
Pipeline
#4501
failed with stages
in 0 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
0 deletions
+79
-0
.gitignore
+1
-0
Cargo.lock
+5
-0
Cargo.toml
+9
-0
src/main.rs
+64
-0
No files found.
.gitignore
0 → 100644
View file @
f16bdb28
/target
Cargo.lock
0 → 100644
View file @
f16bdb28
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "tcpserver"
version = "0.1.0"
Cargo.toml
0 → 100644
View file @
f16bdb28
[package]
name
=
"tcpserver"
version
=
"0.1.0"
authors
=
["root"]
edition
=
"2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
src/main.rs
0 → 100644
View file @
f16bdb28
use
std
::
io
::{
Error
,
Read
,
Write
};
use
std
::
net
::{
TcpListener
,
TcpStream
};
use
std
::
thread
;
use
std
::
time
;
use
std
::
str
;
fn
read_byte
(
_url
:
&
String
)
->
Result
<
(),
&
'static
str
>
{
Err
(
"input error."
)
}
//循环
fn
handle_client
(
mut
stream
:
TcpStream
)
->
Result
<
(),
Error
>
{
let
mut
buf
=
[
0
;
512
];
//100
loop
{
//eprintln!("buf ==========={:?}", buf);
//输入内容字节数
let
bytes_read
=
stream
.read
(
&
mut
buf
)
?
;
//字节数为0则结束
match
read_byte
(
bytes_read
)
{
Ok
(
k
)
=>
{
stream
.read
(
&
mut
buf
)
?
;
},
Err
(
e
)
=>
{
println!
(
"Failed to open the file."
);
}
};
// if bytes_read == 0 {
// return Ok(());
// }
//字节数
println!
(
"format {} arguments"
,
bytes_read
);
//写回到stream中
stream
.write
(
&
buf
[
..
bytes_read
])
?
;
//转字符串打印
println!
(
"{}"
,
str
::
from_utf8
(
&
buf
)
.expect
(
"Could not write buffer as string"
));
//休眠1秒钟
thread
::
sleep
(
time
::
Duration
::
from_secs
(
1
as
u64
));
}
//Ok(())
}
fn
main
()
->
std
::
io
::
Result
<
()
>
{
//创建监听器
let
listener
=
TcpListener
::
bind
(
"127.0.0.1:8080"
)
?
;
// let mut thread_vec: Vec<thread::JoinHandle<()>> = Vec::new();
//打印监听器
eprintln!
(
"listener ==={:?}"
,
listener
);
for
stream
in
listener
.incoming
()
{
//如果值不存在则提示错误
let
stream
=
stream
.expect
(
"failed!"
);
//spawn 函数创建新进程,
let
handle
=
thread
::
spawn
(
||
{
//调用handle_client,有错误抛出异常unwrap_or_else类似match
handle_client
(
stream
)
.unwrap_or_else
(|
error
|
eprintln!
(
"{:?}"
,
error
));
//Err(error);
});
eprintln!
(
"handle ==========={:?}"
,
handle
);
}
Ok
(())
}
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