Linux socket程式設計學習初步(3)--客戶端向伺服器請求檔案

pengfoo發表於2012-03-14

伺服器端:

#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);
	}
}

給出了客戶端由伺服器獲取一個檔案的例子。
獲取的檔案將直接顯示在終端介面上。

相關文章