Chinaunix首页
|
论坛
|
博客
博文
博主
INFINITY
4月28日14:30-20:30机房服务器迁移,暂停博客使用
9/30日 14:00 -10/4日 08:00暂时无法发布内容!
9/30日 14:00 -10/4日 08:00暂时无法发布内容!
首页
|
博文目录
|
关于我
ihopemeo
博客访问: 4628
博文数量: 8
博客积分: 0
博客等级: 民兵
技术积分: 80
用 户 组: 普通用户
注册时间: 2014-11-19 08:24
文章分类
全部博文
(8)
Program
(1)
TIPS
(7)
未分配的博文
(0)
文章存档
2014年
(8)
2014年11月
(8)
我的朋友
最近访客
linux260
推荐博文
·
基于向量检索服务与TextEmbed...
·
k8s里的node-exporter如何cur...
·
MySQL Subquery Cache vs Ora...
·
DashVector x 通义千问大模型...
·
ORACLE vs MySQL 对组合索引...
相关博文
·
浅析数据中心中的资产定位服...
·
快速了解 | 企业代码签名证书...
·
水箱自洁消毒器数据采集解决...
·
哈工大研究员发布报告:政府...
·
温泉泵站监控运维系统解决方...
·
农田渠道闸门智能监控运维系...
·
微服务治理最全详解(8大常见...
·
服务器数据恢复—硬盘坏道和...
·
一文读懂DNS解析原理、设置步...
·
为什么会出现Nginx重定向次数...
Socket多线程局域网点对点聊天
分类:
网络与安全
2014-11-19 09:40:15
client端
#include
<
iostream
>
#include
<
string
>
#define _WIN32_WINNT 0x501
#include
<
winsock2
.
h
>
#include
<
ws2tcpip
.
h
>
#include
<
w32api
.
h
>
#include
<
stdio
.
h
>
#include
<
process
.
h
>
#include
<
windows
.
h
>
using namespace std
;
int
socketfd
;
unsigned _stdcall Receiving
(
void
*
)
{
while
(
true
)
{
ssize_t bytes_received
;
char incoming_data_buffer
[
1000
]
;
bytes_received
=
recv
(
socketfd
,
incoming_data_buffer
,
1000
,
0
)
;
if
(
bytes_received
=
=
0
)
{
cout
<
<
"host shut down."
<
<
endl
;
break
;
}
if
(
bytes_received
=
=
-
1
)
continue
;
incoming_data_buffer
[
bytes_received
]
=
'
\
0
'
;
cout
<
<
bytes_received
<
<
" "
<
<
"bytes received :"
<
<
endl
;
cout
<
<
incoming_data_buffer
<
<
endl
;
cout
<
<
"---------------Received--------------"
<
<
endl
<
<
"
"
<
<
" "
;
}
}
unsigned _stdcall Sending
(
void
*
)
{
string
str
,
code
=
">q"
;
fflush
(
stdin
)
;
while
(
true
)
{
cout
<
<
"
"
;
getline
(
cin
,
str
)
;
const
char
*
msg
=
str
.
data
(
)
;
const
char
*
c_ode
=
code
.
data
(
)
;
if
(
strcmp
(
msg
,
c_ode
)
=
=
0
)
break
;
int
len
;
ssize_t bytes_sent
;
len
=
strlen
(
msg
)
;
bytes_sent
=
send
(
socketfd
,
msg
,
len
,
0
)
;
cout
<
<
"----------------Sent----------------"
<
<
endl
;
}
}
int
main
(
)
{
WSADATA wsadata
;
if
(
WSAStartup
(
MAKEWORD
(
2
,
2
)
,
&
wsadata
)
!
=
0
)
{
cout
<
<
"WSAStartup failed to initialize!"
<
<
endl
;
return 0
;
}
int
status
;
addrinfo host_info
;
addrinfo
*
host_info_list
;
memset
(
&
host_info
,
0
,
sizeof
(
host_info
)
)
;
cout
<
<
"Setting up the struct..."
<
<
endl
;
host_info
.
ai_family
=
AF_UNSPEC
;
host_info
.
ai_socktype
=
SOCK_STREAM
;
char ip
[
16
]
,
port
[
9999
]
;
cout
<
<
"IP Address:"
;
cin
>
>
ip
;
cout
<
<
"Port:"
;
cin
>
>
port
;
status
=
getaddrinfo
(
ip
,
port
,
&
host_info
,
&
host_info_list
)
;
if
(
status!=0
)
cout
<
<
"getaddrinfo error"
<
<
gai_strerror
(
status
)
;
cout
<
<
"Creating a socket..."
<
<
endl
;
socketfd
=
socket
(
host_info_list
-
>
ai_family
,
host_info_list
-
>
ai_socktype
,
host_info_list
-
>
ai_protocol
)
;
if
(
socketfd
=
=
-
1
)
{
cout
<
<
"Socket error."
<
<
endl
;
return 0
;
}
cout
<
<
"Connecting to "
<
<
ip
<
<
endl
;
status
=
connect
(
socketfd
,
host_info_list
-
>
ai_addr
,
host_info_list
-
>
ai_addrlen
)
;
if
(
status
=
=
-
1
)
{
cout
<
<
"connect error"
<
<
endl
;
return 0
;
}
cout
<
<
"Connection established."
<
<
endl
;
unsigned
int
thID1
,
thID2
;
HANDLE hth1
,
hth2
;
hth2
=
(
HANDLE
)
_beginthreadex
(
NULL
,
0
,
Receiving
,
NULL
,
0
,
&
thID2
)
;
hth1
=
(
HANDLE
)
_beginthreadex
(
NULL
,
0
,
Sending
,
NULL
,
0
,
&
thID1
)
;
WaitForSingleObject
(
hth1
,
INFINITE
)
;
cout
<
<
"Closing threads: ID: "
<
<
thID1
<
<
" "
<
<
thID2
<
<
endl
;
CloseHandle
(
hth1
)
;
CloseHandle
(
hth2
)
;
cout
<
<
"Closing socket..."
<
<
endl
;
freeaddrinfo
(
host_info_list
)
;
closesocket
(
socketfd
)
;
return 0
;
}
服务端
#include
<
iostream
>
#include
<
cstring
>
#define _WIN32_WINNT 0x501
#include
<
winsock2
.
h
>
#include
<
ws2tcpip
.
h
>
#include
<
w32api
.
h
>
#include
<
stdio
.
h
>
#include
<
process
.
h
>
#include
<
windows
.
h
>
using namespace std
;
int
new_sd
;
unsigned _stdcall Receiving
(
void
*
)
{
while
(
true
)
{
ssize_t bytes_received
;
char incoming_data_buffer
[
1000
]
;
bytes_received
=
recv
(
new_sd
,
incoming_data_buffer
,
1000
,
0
)
;
if
(
bytes_received
=
=
0
)
{
cout
<
<
"host shut down."
<
<
endl
;
break
;
}
if
(
bytes_received
=
=
-
1
)
continue
;
incoming_data_buffer
[
bytes_received
]
=
'
\
0
'
;
/
/
important
.
it will appear
"\"
at the
end
of letter without it
.
cout
<
<
bytes_received
<
<
" "
<
<
"bytes received :"
<
<
endl
;
cout
<
<
incoming_data_buffer
<
<
endl
;
cout
<
<
"-------------------Received-------------------"
<
<
endl
<
<
"
"
;
}
}
unsigned _stdcall Sending
(
void
*
)
{
string
str
,
code
=
">q"
;
fflush
(
stdin
)
;
while
(
true
)
{
cout
<
<
"
"
;
getline
(
cin
,
str
)
;
const
char
*
msg
=
str
.
data
(
)
;
const
char
*
c_ode
=
code
.
data
(
)
;
if
(
strcmp
(
msg
,
c_ode
)
=
=
0
)
break
;
int
len
;
ssize_t bytes_sent
;
len
=
strlen
(
msg
)
;
bytes_sent
=
send
(
new_sd
,
msg
,
len
,
0
)
;
cout
<
<
"---------------------Sent--------------------"
<
<
endl
;
}
}
int
main
(
)
{
WSADATA wsadata
;
if
(
WSAStartup
(
MAKEWORD
(
2
,
2
)
,
&
wsadata
)
!
=
0
)
{
cout
<
<
"WSAStartup failed to initialize!"
<
<
endl
;
return 0
;
}
int
status
;
addrinfo host_info
;
addrinfo
*
host_info_list
;
memset
(
&
host_info
,
0
,
sizeof
(
host_info
)
)
;
cout
<
<
"Setting up the struct..."
<
<
endl
;
host_info
.
ai_family
=
AF_UNSPEC
;
host_info
.
ai_socktype
=
SOCK_STREAM
;
host_info
.
ai_flags
=
AI_PASSIVE
;
char
*
ip
=
NULL
;
char name
[
32
]
=
""
;
PHOSTENT hostinfo
;
if
(
gethostname
(
name
,
sizeof
(
name
)
)
=
=
0
)
{
/
/
如果成功 将本地主机名存放入name缓冲区
if
(
(
hostinfo
=
gethostbyname
(
name
)
)
!
=
NULL
)
{
ip
=
inet_ntoa
(
*
(
struct in_addr
*
)
*
hostinfo
-
>
h_addr_list
)
;
/
/
获取主机IP
}
}
cout
<
<
"Server IP: "
<
<
ip
<
<
endl
;
status
=
getaddrinfo
(
ip
,
"5555"
,
&
host_info
,
&
host_info_list
)
;
if
(
status!=0
)
cout
<
<
"getaddrinfo error"
<
<
gai_strerror
(
status
)
;
cout
<
<
"Creating a socket..."
<
<
endl
;
int
socketfd
;
socketfd
=
socket
(
host_info_list
-
>
ai_family
,
host_info_list
-
>
ai_socktype
,
host_info_list
-
>
ai_protocol
)
;
if
(
socketfd
=
=
-
1
)
cout
<
<
"socket error"
;
cout
<
<
"Binding socket..."
<
<
endl
;
status
=
setsockopt
(
socketfd
,
SOL_SOCKET
,
SO_REUSEADDR
,
"1"
,
sizeof
(
int
)
)
;
status
=
bind
(
socketfd
,
host_info_list
-
>
ai_addr
,
host_info_list
-
>
ai_addrlen
)
;
if
(
status
=
=
-
1
)
cout
<
<
"bind error"
<
<
endl
;
cout
<
<
"Listening for connections..."
<
<
endl
;
status
=
listen
(
socketfd
,
5
)
;
if
(
status
=
=
-
1
)
cout
<
<
"listen error"
<
<
endl
;
struct sockaddr_storage their_addr
;
socklen_t addr_size
=
sizeof
(
their_addr
)
;
new_sd
=
accept
(
socketfd
,
(
struct sockaddr
*
)
&
their_addr
,
&
addr_size
)
;
if
(
new_sd
=
=
-
1
)
{
cout
<
<
"listen error"
<
<
endl
;
}
else
{
cout
<
<
"Connections accepted.Using new socketfd : "
<
<
new_sd
<
<
endl
;
}
unsigned
int
thID1
,
thID2
;
HANDLE hth1
,
hth2
;
hth1
=
(
HANDLE
)
_beginthreadex
(
NULL
,
0
,
Receiving
,
NULL
,
0
,
&
thID1
)
;
hth2
=
(
HANDLE
)
_beginthreadex
(
NULL
,
0
,
Sending
,
NULL
,
0
,
&
thID2
)
;
WaitForSingleObject
(
hth2
,
INFINITE
)
;
cout
<
<
"Closing threads: ID: "
<
<
thID1
<
<
" "
<
<
thID2
<
<
endl
;
CloseHandle
(
hth1
)
;
CloseHandle
(
hth2
)
;
cout
<
<
"Stopping Server..."
<
<
endl
;
freeaddrinfo
(
host_info_list
)
;
closesocket
(
new_sd
)
;
closesocket
(
socketfd
)
;
return 0
;
}
先贴代码,有空再解释。
阅读(136) | 评论(0) | 转发(0) |
0
上一篇:
拆分素数和
下一篇:没有了
给主人留下些什么吧!~~
评论热议
请登录后评论。
登录
注册