以下函数,client端是用127.0.0.1回送地址,固定与本机通信。而service端应该依旧能接收到非本机通信,
如果需要固定只接收本机通信,不接收其它的,那在 clientfd = accept(socketfd,(struct sockaddr *)&client_addr,&addrlen);后,需要对client的ip进行判断!
/*
* 基于TCP的 service
* 新建文件名为:./rec_buf暂存接收内容
*/
#include
#include
#include
#include //strcpy.strlen
#include //htons sockaddr_in
#include //for open() read().....
//for open()
#include
#include
#include
#include //for exit()
#define SERVPORT 5555 //服务器监听端口
#define BACKLOG 10 //请求队列长度
#define BUFFSIZE 1024
int main(void)
{
int ret = 0;
int addrlen = 0;
int socketfd = 0; //socket 描述符
int clientfd = 0; //数据传输描述符
int filefd = 0;
struct sockaddr_in host_addr; //本机IP地址和端口号
struct sockaddr_in client_addr; //客户端IP地址和端口信息
char buf[BUFFSIZE];
//创建套接字
socketfd = socket(AF_INET, SOCK_STREAM,0);
if(socketfd == -1)
{
perror("socketfd:");
exit(-1);
}
//绑定套接字
memset(&host_addr,0,sizeof(host_addr));
host_addr.sin_family = AF_INET; //TCP/IP协议
host_addr.sin_port = htons(SERVPORT); //主机字节顺序转换为网络字节顺序
host_addr.sin_addr.s_addr = INADDR_ANY; //填入本机地址
ret = bind(socketfd,(const struct sockaddr *)&host_addr,sizeof(host_addr));
if(ret == -1)
{
perror("blind:");
close(socketfd);
return -1;
}
//监听网络端口
ret = listen(socketfd,BACKLOG);
if(ret == -1)
{
perror("listen:");
close(socketfd);
return -1;
}
while(1)
{
clientfd = accept(socketfd,(struct sockaddr *)&client_addr,&addrlen);
if(clientfd == -1)
{
perror("accept:");
close(socketfd);
return -1;
}
//open transmission file
filefd = open("./rec_buf", O_WRONLY | O_CREAT |O_TRUNC);
//O_TRUNC 每次打开文件时,清除里面内容,切将内容截断为0;O_APPEND 每次写时都添加到文件尾端
if (filefd < 0)
{
perror( "open the file:");
close(clientfd);
close(socketfd);
return -1;
}
//tarnsmit file
do
{
memset(&buf,0,sizeof(buf));
ret = read(clientfd, buf, BUFFSIZE);
if (ret < 0)
{
perror("connfd read error:");
close(filefd);
close(clientfd);
close(socketfd);
return -1;
}
else if(ret == 0) //if read the end of file,out of loop
break;
if (write(filefd, buf, ret) < 0)
{
perror("writing to filefd error:");
close(filefd);
close(clientfd);
close(socketfd);
return -1;
}
}while(ret);
close(filefd);
close(clientfd);
}
//关闭端口
close(socketfd);
return 0;
}
====================================================================================
/*
* client 客服端 使用格式:clinet 文件名
* 用 127.0.0.1 回送地址,只与本机通信
*/
#include
#include
#include
#include
#include
#include //for open() read().....
#include
#include
#include
#define SERVPORT 5555 //服务器监听窗口
#define BACKLOG 10 //请求队列的长度数
#define BUFFSIZE 1024
int main(int argc,char *argv[])
{
int ret = 0;
int flag = 0;
int socketfd = 0;
int filefd = 0;
struct sockaddr_in serv_addr; //客服端IP地址和端口信息
int addrlen = sizeof(serv_addr);
char buf[BUFFSIZE];
socketfd = socket(AF_INET,SOCK_STREAM,0);
if(socketfd == -1)
{
perror("socketfd:");
return -1;
}
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
inet_aton("127.0.0.1",(struct sockaddr *)&serv_addr.sin_addr.s_addr);
flag = 0; //connect超时机制标志
do
{
ret = connect(socketfd,(struct sockaddr *)&serv_addr,addrlen);
if(ret == -1)
{
perror("connect:");
flag++;
}
if(flag > 3)
{
close(socketfd);
return -1;
}
sleep (1);
}while(ret < 0);
//transfer file
filefd = open(argv[1], O_RDONLY);
if (filefd < 0)
{
perror("open file:");
close(socketfd);
return -1;
}
do
{
memset(&buf,0,sizeof(buf));
ret = read(filefd, buf, BUFFSIZE);
if (ret < 0)
{
perror("read error:");
close(socketfd);
close(filefd);
return -1;
}
if (write(socketfd, buf, ret) < 0)
{
perror("write error");
close(socketfd);
close(filefd);
return -1;
}
}while(ret);
close(socketfd);
close(filefd);
return 0;
}
阅读(1596) | 评论(0) | 转发(0) |