系统:fedora core 5
服务器端程序:
/*
server
*/
#include
<
stdio.h
>
#include
<
sys
/
types.h
>
#include
<
netinet
/
in
.h
>
#include
<
sys
/
socket.h
>
#include
<
netdb.h
>
#include
<
unistd.h
>
#include
<
string
.h
>
#include
<
arpa
/
inet.h
>
#define
PORT 8848
#define
BACKLOG 1
main()
{
int
listenfd,connectfd;
struct
sockaddr_in server;
struct
sockaddr_in client;
int
sin_size;
if
(( listenfd
=
socket(AF_INET, SOCK_STREAM,
0
))
==
-
1
)
{
perror(
"
Creating socket failed.
"
);
return
0
;
}
int
opt
=
SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
&
opt,
sizeof
(opt));
bzero(
&
server,
sizeof
(server));
server.sin_family
=
AF_INET;
server.sin_port
=
htons(PORT);
server.sin_addr.s_addr
=
htonl(INADDR_ANY);
printf(
"
binding...
"
);
if
(bind(listenfd, (
struct
sockaddr
*
)
&
server,
sizeof
(
struct
sockaddr))
==
-
1
) {
perror(
"
Bind error
"
);
return
0
;
}
printf(
"
listen...
"
);
if
(listen(listenfd,BACKLOG)
==
-
1
){
perror(
"
listen() error
"
);
return
0
;
}
printf(
"
accept....
"
);
sin_size
=
sizeof
(
struct
sockaddr_in);
if
((connectfd
=
accept(listenfd, (
struct
sockaddr
*
)
&
client,(socklen_t
*
)
&
sin_size))
==
-
1
) {
perror(
"
accept() error
"
);
return
0
;
}
printf(
"
you got a connection from %s
"
,inet_ntoa(client.sin_addr) );
send(connectfd,
"
welcome to my server.
"
,
22
,
0
);
close(connectfd);
close(listenfd);
}
客户端程序:
/*
client
*/
#include
<
stdio.h
>
#include
<
sys
/
types.h
>
#include
<
netinet
/
in
.h
>
#include
<
sys
/
socket.h
>
#include
<
netdb.h
>
#include
<
unistd.h
>
#include
<
string
.h
>
#include
<
arpa
/
inet.h
>
#include
<
errno.h
>
#define
PORT 8848
#define
MAXDATASIZE 100
int
main(
int
argc,
char
*
argv[])
{
int
fd, numbytes;
char
buf[MAXDATASIZE];
struct
hostent
*
he;
int
errno;
struct
sockaddr_in server;
if
(argc
!=
2
) {
printf(
"
usage: %s <ip address>
"
,argv[
0
]);
return
0
;
}
printf(
"
gethostbynem....
"
);
if
((he
=
gethostbyname(argv[
1
]))
==
NULL){
printf(
"
gethostbyname() error
"
);
return
0
;
}
if
((fd
=
socket(AF_INET,SOCK_STREAM,
0
))
==
-
1
){
printf(
"
socket() error
"
);
return
0
;
}
bzero(
&
server,
sizeof
(server));
server.sin_family
=
AF_INET;
server.sin_port
=
htons(PORT);
server.sin_addr
=
*
((in_addr
*
)he
->
h_addr);
printf(
"
connecting...
"
);
if
(connect(fd, (
struct
sockaddr
*
)
&
server,
sizeof
(
struct
sockaddr))
==
-
1
){
//
printf("connect() error ");
perror(
"
error
"
);
return
0
;
}
if
(( numbytes
=
recv(fd,buf,MAXDATASIZE,
0
))
==
-
1
){
printf(
"
recv() error
"
);
return
0
;
}
buf[numbytes]
=
'
操作:
[root@localhost tcpsocket]# ls
help tcpsocketC.c tcpsocketS.c
[root@localhost tcpsocket]# g++ -g -o myserver tcpsocketS.c -lc -lnsl
[root@localhost tcpsocket]# g++ -g -o myclient tcpsocketC.c -lc -lnsl
[root@localhost tcpsocket]# ls
help myclient myserver tcpsocketC.c tcpsocketS.c
[root@localhost tcpsocket]# ./myserver
binding...
listen...
accept....
[root@localhost tcpsocket]# netstat -a | grep 8848
tcp 0 0 *:8848 *:* LISTEN
[root@localhost tcpsocket]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:13:D4:3E:9F:89
inet addr:125.221.160.241 Bcast:125.221.160.255 Mask:255.255.255.0
inet6 addr: fe80::213:d4ff:fe3e:9f89/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:39108 errors:0 dropped:0 overruns:0 frame:0
TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2359238 (2.2 MiB) TX bytes:4040 (3.9 KiB)
Interrupt:17 Base address:0xa000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2005 errors:0 dropped:0 overruns:0 frame:0
TX packets:2005 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2078171 (1.9 MiB) TX bytes:2078171 (1.9 MiB)
[root@localhost tcpsocket]# ./myclient 127.0.0.1