linux文件传输c代码,linux下网络文件传输C代码.doc

服务器(Server)代码:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

void *thread(void *p);

int download(int client);

int uploading(int client);

int clientfd1;

int clientfd2;

int clientfd;

int main(int argc,char **argv)

{

int socketfd;

pthread_t tid1;

pthread_t tid2;

struct sockaddr_in info;

socketfd=socket(AF_INET,SOCK_STREAM,0);//创建一个用于网络通信的套接字

if(socketfd<0)

{

printf("socket error\n");

return 1;

}

info.sin_family=AF_INET;

info.sin_port=htons(6688);

info.sin_addr.s_addr=0;

bind(socketfd,(const struct sockaddr *)&info,sizeof(info));//一个套接字指明一个本地IP 和协议端口号

listen(socketfd,20);//服务器使用listen 是套接字处于被动状态(准备接受联入请求)

clientfd1=accept(socketfd,NULL,NULL);//服务器调用socket 创建一个套接字,用bind 指定一个本地IP 地址和协议端口号,然后用listen 使套接字处于被动状态,并设置连接请求队列的长度

if(clientfd<0)

{

printf("accept error\n");

return 2;

}

pthread_create(&tid1,NULL,thread,&clientfd1);//创建多线程

clientfd2=accept(socketfd,NULL,NULL);//服务器调用socket 创建一个套> 接字,用bind 指定一个本地IP 地址和协议端口号,然后用listen 使套接字处于 被动状态,并设置连接请求队列的长度

if(clientfd<0)

{

printf("accept error\n");

return 2;

}

pthread_create(&tid2,NULL,thread,&clientfd2);//创建多线程

pthread_join(tid1,NULL);//线程同步

pthread_join(tid2,NULL);//线程同步

close(socketfd);

}

void *thread(void *p)//线程函数

{

int fp,ret;

char buf[100]="";

char str[100]="";

int clientfd=*(int *)p;

read(clientfd,buf,sizeof(buf));

if(strcmp(buf,"download")==0)//判断是否从服务器下载程序

{

printf("from client:%s\n",buf);

while(1)

{

bzero(buf,sizeof(buf));

bzero(str,sizeof(str));

read(clientfd,buf,sizeof(buf));//读取从客户端输入的帐号

printf("account number:%s\n",buf);

if(strcmp(buf,"luyuan")==0)//匹配帐号

{

strcpy(str,"yes");//帐号匹配成功把yes存入字符数组中

write(clientfd,str,strlen(str));//匹配成功把yes传给客户端

break;

}

else

{

strcpy(str,"no");//匹配失败把no存入字符数组中

write(clientfd,str,strlen(str));//匹配失败把no传给客户端

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值