日期:2014-05-16  浏览次数:21073 次

关于linux c中 用socket发送txt文件
如题 最近在看例子 也学习了 但总缺这少那。。。

请问谁能给我一个最简单 server/client 关于发送txt文件的源代码 最原始的就好了 谢谢。。

------解决方案--------------------
发送一副图到浏览器的
C/C++ code

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include<sys/stat.h>        /*stat用到*/

#define BUFFER_SIZE 1024

unsigned long get_file_size(char filename)  /*定义一个用来获取文件大小的函数*/
{
    struct stat buf;
    if(stat(filename, &buf)<0)
    {
        return 0;
    }
    return (unsigned long)buf.st_size;
}



int main(int argc, char *argv[])
{
    int sockfd,new_fd;
    struct sockaddr_in server_addr; /* 服务器地址信息 */
    struct sockaddr_in client_addr; /* 客户端地址信息 */
    int sin_size,portnumber;

    int ret_send,ret_recv;

    if(argc!=2)
    {
        fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
        exit(1);
    }


    if((portnumber=atoi(argv[1]))<0)
    {
        fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
        exit(1);
    }
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) /* 建立套接字 */
    {
        fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
        exit(1);
    }


    bzero(&server_addr,sizeof(struct sockaddr_in)); /*把地址信息清0*/
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    server_addr.sin_port=htons(portnumber);

    if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
    {
        fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
        exit(1);
    }

    if(listen(sockfd,5)==-1) /* 开始接收连接 ,且设置最大同时访问的客户端为5*/
    {
        fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
        exit(1);
    }


    while(1)
    {
        sin_size=sizeof(struct sockaddr_in);
        if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
        {
            fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
            exit(1);
        }


        fprintf(stdout,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
        char request[1024] = { 0 };
        ret_recv = recv( new_fd, request, sizeof( request ), 0 );/*接收http连接过程客户端返回的信息,并保存在request中*/
        if(ret_recv==-1)
        {
            fprintf(stderr,"Write Error:%s\n",strerror(errno));
            exit(1);
        }


        printf("%s",request );/*打印出http连接过程客户端回应过来的一些信息*/

        /*whatever we recv, we send 200 response */
        char file_name[20]="1.jpg";
        FILE * fp = fopen(file_name,"rb");
        if(NULL == fp )
        {
            printf("File:\t%s Not Found\n", file_name);
        }

        //char content[] = "<head><head><title>index.html</title></head><body>index.html</body>";
        char buffer[BUFFER_SIZE];
        char response[1024];
        int file_block_length = 0;
        unsigned long file_lenght= get_file_size(file_name);
        //response="HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\n\n";
        sprintf( response, "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: %l\r\n\r\n" , /*去掉了%s*/
        file_lenght);  /*把strlen(content)改成file_block_length,去掉了 content*/
        //发送buffer中的字符串到new_fd,实际是给客户端
        if(send(new_fd,response, strlen( response ),0)<0)
        {
            fprintf(stderr,"send Error:%s\n",strerror(errno));
            break;
        }

        while( (file_block_length = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
        {   /*while循环,直到发送完文件*/
            printf("file_block_length = %d\n",file_block_length);
            //发送buffer中的字符串到new_fd,实际是给客户端
            if(send(new_fd,buffer,file_block_length,0)&