Linux socket编程学习初步(3)--客户端向服务器请求文件

服务器端:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>//read,write
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netdb.h>
#include <sys/fcntl.h>//open
#include <strings.h>

#define SERVER_PORT 12345
#define BUF_SIZE 4096 /* block transfer size */
#define QUEUE_SIZE 10 /* how many client can connect at the same time */

void fatal(char *string)
{
    printf("%s\n",string);
    exit(1);
}

int main(/*int argc,char * argv[]*/)
{
	int s,b,l,fd,sa,bytes,on=1;
	char buf[BUF_SIZE]; /*buf for outgoing files*/
	struct sockaddr_in channel; /* hold IP address*/

    /* Build address structure to bind to socket. */
    bzero(&channel,sizeof(channel));
    channel.sin_family=AF_INET;
    channel.sin_addr.s_addr=htonl(INADDR_ANY);
    channel.sin_port=htons(SERVER_PORT);

    /* Passive open.Wait for connection. */
    s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  /* Creat socket */
    if(s<0) fatal("socket failed");
    setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
	/* use the socket for many time (up word) */
    b=bind(s,(struct sockaddr *)&channel,sizeof(channel));
    if(b<0) fatal("blind failed");

    l=listen(s,QUEUE_SIZE);
    if(l<0) fatal("listen failed");

 /* Socket is now set up and bound. Wait for connection and process it. */

    while(1)
    {
        sa=accept(s,0,0);
        if(sa<0) fatal("accept failed");

        recv(sa,buf,BUF_SIZE,0);

     /* Get and return the file. */
        fd=open(buf,O_RDONLY);
        if(fd<0) fatal("open failed");

        while(1)
        {
            bytes=read(fd,buf,BUF_SIZE);
            if(bytes<=0) break;
            write(sa,buf,bytes);
        }

        close(fd);
        close(sa);
    }
    return 0;
}

客户端:

/*
 *This page contains a client program that can request a flie from the server program.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>//read,write
#include <stdio.h>

#define SERVER_PORT 12345
#define BUF_SIZE 4096
#define PATH "/home/fupeng/Desktop/test.txt"
#define DEST_IP "127.0.0.1"

void fatal(char *s)
{
	printf("%s\n",s);
	exit(1);
}

int main(/*int argc,char * *argv*/)

{
    int c,s,bytes;
    char buf[BUF_SIZE];   /*buf for incoming files*/
    struct sockaddr_in channel;   /* hold IP address*/

//    if (argc!=3)fatal("Usage:client IPaddress file-name");

    s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(s<0)fatal("socket");

    bzero(&channel,sizeof(channel));
    channel.sin_family=AF_INET;
    channel.sin_port=htons(SERVER_PORT);
    if(inet_aton(DEST_IP,&channel.sin_addr)==0)
    {

 /* use the ASCII ip for the  server's sin_addr. */

        fprintf(stderr,"Inet_aton erro\n");
        exit(1);
    }

    c=connect(s,(struct sockaddr *)&channel,sizeof(channel));
    if(c<0)fatal("connetct failed");
//    send(s,argv[2],strlen(argv[2]),0);
    send(s,PATH,strlen(PATH),0);//BY FUPENG

   /* Go get the file and write it to standard output.*/
	while(1)
	{
		bytes=read(s,buf,BUF_SIZE);
		if(bytes<=0) exit(0);
 		write(1,buf,bytes);
	}
}

给出了客户端由服务器获取一个文件的例子。
获取的文件将直接显示在终端界面上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值