第1章1.5节一个简单的时间获取服务器程序
下面的demo既有server端又有client端,两者互相通信
1. server端
-
cong@msi:/work/test/tcpip$ cat 2timeserv/timeserv.c
-
#include "utils.h"
-
-
#define TIME_SERV_ADDR "192.168.4.98"
-
//#define TIME_SERV_ADDR "127.0.0.1"
-
-
#define LISTENQ 10
-
-
int main ( int argc, char *argv[] )
-
{
-
int listenfd, connfd;
-
struct sockaddr_in servaddr;
-
char buff[1024];
-
time_t ticks;
-
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(listenfd< 0)
-
return -1;
-
dbmsg("socket created");
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999); //绑定端口13999,htons将主机字节转为网络字节序
-
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //区别:client端必须是一个固定的ip,server端是INADDR_ANY
-
//inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
-
dbmsg("next bind&listen");
-
bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
listen(listenfd, LISTENQ);
-
dbmsg("bind&listen success");
-
while(1)
-
{
-
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
-
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
-
dbmsg("accept success");
-
ticks = time(NULL);
-
snprintf(buff, sizeof(buff), "%.24s\n", ctime(&ticks));
-
write(connfd, buff, strlen(buff));
-
close(connfd);
-
}
-
-
return EXIT_SUCCESS;
-
}
2. client端
-
#include "utils.h"
-
-
//#define TIME_SERV_ADDR "192.168.4.98"
-
#define TIME_SERV_ADDR "127.0.0.1"
-
-
int main ( int argc, char *argv[] )
-
{
-
dbmsg();
-
int sockfd, n;
-
struct sockaddr_in servaddr;
-
char recvline[1024];
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(sockfd < 0)
-
return -1;
-
dbmsg("socket created");
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
//这儿修改端口为13999,不修改在主机上运行就会访问系统的daytime而不访问自己写的server了
-
servaddr.sin_port = htons(13999); //绑定端口13999,htons将主机字节转为网络字节序
-
inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr); //将字符串(presentation)转为数值(numeric)
-
dbmsg("next connect");
-
connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
dbmsg("connect success");
-
while((n=read(sockfd, recvline, MAXLINE)) > 0)
-
{
-
recvline[n] = '\0';
-
dbmsg("read over");
-
fputs(recvline, stdout);
-
}
-
-
return EXIT_SUCCESS;
-
}
3.查看本要端口占用情况
a.netstat默认,是不显示listen状态的socket
-
cong@msi:/work/test/tcpip$ netstat
-
tcp 0 1 192.168.4.98:37985 58.215.118.34:http FIN_WAIT1
-
unix 3 [ ] STREAM CONNECTED 19484
b. -l 显示处于listen状态的socket
-a 显示所有状态的socket,包括listen connect等
c. -n 将host与port name显示为数字
-
cong@msi:/work/test/tcpip$ netstat -lp
-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
-
tcp 0 0 *:daytime *:* LISTEN -
-
-
cong@msi:/work/test/tcpip$ netstat -lnp
-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
-
tcp 0 0 0.0.0.0:13 0.0.0.0:* LISTEN - //将daytime服务变为数字13
d. -p显示使用socket的程序
-
cong@msi:/work/test/tcpip$ netstat -lp
-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
-
tcp 0 0 *:daytime *:* LISTEN -
-
-
cong@msi:/work/test/tcpip$ netstat -lnp
-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
-
tcp 0 0 0.0.0.0:13 0.0.0.0:* LISTEN -
附源码:
client端,下载后改名为1echo.tar.gz
1echo.rar
server端,下载后改名为2timeserv.tar.gz
2timeserv.rar
阅读(1061) | 评论(0) | 转发(0) |