1)ping cn5071
PING cn5071 (12.0.157.39) 56(84) bytes of data.
64 bytes from cn5071 (12.0.157.39): icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from cn5071 (12.0.157.39): icmp_seq=2 ttl=64 time=0.073 ms
64 bytes from cn5071 (12.0.157.39): icmp_seq=3 ttl=64 time=0.084 ms
^C
--- cn5071 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2014ms
rtt min/avg/max/mdev = 0.073/0.081/0.086/0.005 ms
2)客户端程序
cpp
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h> //exit
#include <string.h> //bzero
#include <strings.h> //strncasecmp 忽略大小写比较
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_PORT (5001)
#define SERV_IP "12.0.157.39" //只能用ifconfig查到的
#define BUFSIZE (128)
#define QUIT_STR "quit"
int main(void)
{
int sock_fd;
struct sockaddr_in cin;
char wr_buf[BUFSIZE];
//1.创建
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd == -1)
{
perror("socket");
exit(1);
}
//2.连接服务器,还是一样先配置sockaddr_in
bzero(&cin,sizeof(cin));
cin.sin_family = AF_INET;
cin.sin_port = htons(SERV_PORT);
cin.sin_addr.s_addr = inet_addr(SERV_IP);
if( inet_pton(AF_INET,SERV_IP,(void*)&cin.sin_addr) != 1 )
{
perror("inet_pton");
exit(1);
}
if( connect(sock_fd,(struct sockaddr*)&cin,sizeof(cin)) == -1 )
{
perror("connect");
exit(1);
}
//3.写数据
while(1)
{
bzero(wr_buf,BUFSIZE);//写之前先清零
if( fgets(wr_buf,BUFSIZE-1,stdin) == NULL)
{
continue;//重新获取
}
write(sock_fd,wr_buf,strlen(wr_buf));//写数据--相当于发送给服务器
if( strncasecmp(wr_buf,QUIT_STR,strlen(QUIT_STR)) == 0) //输入了quit
{
break;
}
}
close(sock_fd);
return 0;
}
2)服务器程序
cpp
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h> //exit
#include <string.h> //bzero
#include <strings.h> //strncasecmp 忽略大小写比较
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_PORT (5001)
#define SERV_IP "12.0.157.39"
#define BUFSIZE (128)
#define QUIT_STR "quit"
int main(void)
{
int sock_fd,newfd;
struct sockaddr_in sin;
char rd_buf[BUFSIZE];
int read_num;
//1.创建
if( (sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1 )
{
perror("socket");
exit(1);
}
//2.绑定 需要先配置sockaddr_in
bzero(&sin,sizeof(sin));//清零再填充
sin.sin_family = AF_INET;//IPV4
sin.sin_port = htons(SERV_PORT);//端口号转换网络字节序
//两种IP地址转换
//sin.sin_addr.s_addr = inet_addr(SERV_IP);
if( inet_pton(AF_INET,SERV_IP,(void*)&sin.sin_addr) != 1 )
{
perror("inet_pton");
exit(1);
}
if(bind(sock_fd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
{
perror("bind");
exit(1);
}
//3.监听
if( listen(sock_fd,5) == -1 )
{
perror("listen");
exit(1);
}
//4.阻塞等待客户端连接 连接成功后需要newfd
if( (newfd = accept(sock_fd,NULL,NULL)) == -1 )
{
perror("accept");
exit(1);
}
while(1)
{
bzero(rd_buf,BUFSIZE); //读之前先请零
do
{
read_num = read(newfd,rd_buf,BUFSIZE-1);
}while(read_num < 0 && errno == EINTR);
if(read_num < 0)
{
perror("read");
}
if(read_num == 0) //客户端关闭
{
printf("Client is closed!\n");
break;
}
printf("Server received:%s\n",rd_buf);
if( strncasecmp(rd_buf,QUIT_STR,strlen(rd_buf)) == 0)//客户端输入quit
{
printf("Client is exiting!\n");
break;
}
}
close(newfd);
close(sock_fd);
return 0;
}
4)
编译程序
g++ server.cpp -o o1.server
g++ client.cpp -o o2.client
5)运行效果
client端
./o2.client
123
456
quit
server端
yhrun -p thcp1 -w cn5071 o1.server
Server received:123
Server received:456
Server received:quit
Client is closed!
6)在天河上是支持计算节点与服务器之间的socket通信的