c - 网络编程,从客户端发送文件到服务器

标签 c networking

我对网络非常陌生,我正在使用回显服务器和回显客户端。在我的实验室中,我有两台计算机连接在网络上,因此我可以在通过“ifconfig eth0 10.1.1.n(n 是端口号)网络掩码 255.255.255.0”配置它们后对它们执行 ping 操作。

echo_server.c

 /* A simple echo server using TCP */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

#define SERVER_TCP_PORT 3000    /* well-known port */
// #define BUFLEN       256 /* buffer length */

int echod(int);
void reaper(int);

int main(int argc, char **argv)
{
    int     sd, new_sd, client_len, port, confd = 0,b,tot;
    struct  sockaddr_in server, client;
    char buff[1025];

    switch(argc){
    case 1:
        port = SERVER_TCP_PORT;
        break;
    case 2:
        port = atoi(argv[1]);
        break;
    default:
        fprintf(stderr, "Usage: %d [port]\n", argv[0]);
        exit(1);
    }

    /* Create a stream socket   */  
    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "Can't creat a socket\n");
        exit(1);
    }

    /* Bind an address to the socket    */
    bzero((char *)&server, sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
        fprintf(stderr, "Can't bind name to socket\n");
        exit(1);
    }



    while(1) {
      confd = accept(sd, (struct sockaddr*)NULL, NULL);
        if (confd==-1) {
            perror("Accept");
            continue;
        }
        //Should create a text file.
        FILE* fp = fopen( "test.txt", "wb");
        tot=0;
        if(fp != NULL){
            while( (b = recv(confd, buff, 1024,0))> 0 ) {
                tot+=b;
                fwrite(buff, 1, b, fp);
            }

            printf("Received byte: %d\n",tot);
            if (b<0)
               perror("Error recieving the file");

            fclose(fp);
        } else {
            perror("File doesnt exist");
        }
        close(confd);
    }
}

echo_client.c

/* A simple echo client using TCP */
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <strings.h>


#define SERVER_TCP_PORT 3000    /* well-known port */
#define BUFLEN      256 /* buffer length */

int main(int argc, char **argv)
{
    int     n, i, bytes_to_read, b;
    int     sd, port;
    struct  hostent     *hp;
    struct  sockaddr_in server;
    char    *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN], sendbuffer[100];

    switch(argc){
    case 2:
        host = argv[1];
        port = SERVER_TCP_PORT;
        break;
    case 3:
        host = argv[1];
        port = atoi(argv[2]);
        break;
    default:
        fprintf(stderr, "Usage: %s host [port]\n", argv[0]);
        exit(1);
    }

    /* Create a stream socket   */  
    if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "Can't creat a socket\n");
        exit(1);
    }

    bzero((char *)&server, sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    if (hp = gethostbyname(host)) 
      bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
    else if ( inet_aton(host, (struct in_addr *) &server.sin_addr) ){
      fprintf(stderr, "Can't get server's address\n");
      exit(1);
    }

    /* Connecting to the server */
    if (connect(sd, (struct sockaddr *)&server, sizeof(server)) == -1){
      fprintf(stderr, "Can't connect \n");
      exit(1);
    }

    FILE *fp = fopen("test.txt", "rb");

    if(fp == NULL){
        perror("File doesnt exist");
        return 2;
    }

    while( (b = fread(sendbuffer, 1, sizeof(sendbuffer), fp))>0 ){
        send(sd, sendbuffer, b, 0);
    }

    fclose(fp);

    close(sd);
    return(0);
}

我试图在脚本运行时将文本文件从客户端发送到服务器。为了运行该脚本,我指定了一个端口号。

当我执行服务器脚本时,我得到接受:无效参数。它似乎在循环中,所以我得到了一百多个这样的输出。那时我必须退出终端。

有关执行脚本时如何将文件从客户端发送到服务器的任何要点或提示?

最佳答案

在调用 accept(2) 之前您需要调用listen(2)将套接字标记为服务器套接字,用于接受传入的连接请求。

关于c - 网络编程,从客户端发送文件到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066525/

相关文章:

c - 需要一个在 Contiki 中使用 mmem 的例子

linux - 我可以使用静态 IP 检测已配置连接设备的网络和网络掩码吗

linux - 在 Linux 上捕获网络流量

visual-studio-2010 - Visual Studio 远程调试 Amazon EC2 机器

networking - 网络中的IP地址和端口号有什么区别?

c++ - const 的未定义行为

python - 在 bash 中使用 NULL 字节(缓冲区溢出)

c - 在非阻塞模式下创建多个 TCP 连接时内核日志中的 net_ratelimit 消息

c - 获取修改变量的子函数

c - 如何在 C 中为函数设置超时?