gpt4 book ai didi

c - 使用套接字将空文件发送回客户端的简单 FTP

转载 作者:行者123 更新时间:2023-11-30 21:09:06 25 4
gpt4 key购买 nike

我正在尝试为客户端和服务器之间的简单 FTP 实现以下代码。问题是,当服务器将文件发送到客户端时,文件是空的。我不确定问题是什么。我假设问题出在服务器发送文件时。下面是代码。

/*Server Code*/

#ifndef unix
#define WIN32
#include <windows.h>
#include <winsock.h>
#else
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#endif

#include <stdio.h>
#include <string.h>

#define PROTOPORT 5193 /* default protocol port number */
#define QLEN 6 /* size of request queue */

int visits = 0; /* counts client connections */

main(argc, argv)
int argc;
char *argv[];
{
struct hostent *ptrh; /* pointer to a host table entry */
struct protoent *ptrp; /* pointer to a protocol table entry */
struct sockaddr_in sad; /* structure to hold server.s address */
struct sockaddr_in cad; /* structure to hold client.s address */
int sd, sd2; /* socket descriptors */
int port; /* protocol port number */
int alen; /* length of address */
char buf_recv[1000],buf_send[1000]; /* buffer for string the server sends */
char file_buffer[10000],f_buffer[1000];
int n;
FILE *fp;

#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &wsaData);
#endif
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */

if (argc > 1) { /* if argument specified */
port = atoi(argv[1]);

} else {
port = PROTOPORT; /* use default port number */
}

if (port > 0) /* test for illegal value */
sad.sin_port = htons((u_short)port);

else { /* print error message and exit */
fprintf(stderr,"bad port number &#37;s\n",argv[1]);
exit(1);
}

/* Map TCP transport protocol name to protocol number */
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit(1);
}

/* Create a socket */
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "socket creation failed\n");
exit(1);
}

/* Bind a local address to the socket */
if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"bind failed\n");
exit(1);
}

/* Specify size of request queue */
if (listen(sd, QLEN) < 0) {
fprintf(stderr,"listen failed\n");
exit(1);
}

alen = sizeof(cad);

if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
fprintf(stderr, "accept failed\n");
exit(1);
}

sprintf(buf_send,"Please enter the file name: ");
send(sd2,buf_send,strlen(buf_send),0);

n=recv(sd2,buf_recv,1000,0);
buf_recv[n]='\0';
printf("%s\n",buf_recv);
fflush(stdout);

if((fp = fopen(buf_recv,"r"))==NULL)
{
sprintf(buf_send,"File could not be found!!!");
exit(0);
} else
sprintf(buf_send,"File found!!!\n");

send(sd2,buf_send,strlen(buf_send),0);

n=recv(sd2,buf_recv,1000,0);
printf("%s",buf_recv);
fflush(stdout);

while(!feof(fp)) {
fgets(f_buffer,1000,fp);
if (feof(fp))
break;
strcat(file_buffer,f_buffer);
}

fclose(fp);

send(sd2,file_buffer,strlen(file_buffer),0);
closesocket(sd2);
exit(0);
}

客户端代码:

/*Client Code*/

#ifndef unix
#define WIN32
#include <windows.h>
#include <winsock.h>
#else
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

#include <stdio.h>
#include <string.h>

#define PROTOPORT 5193 /* default protocol port number */

extern int errno;

char localhost[] = "localhost"; /* default host name */

main(argc, argv)
int argc;
char *argv[];
{
struct hostent *ptrh; /* pointer to a host table entry */
struct protoent *ptrp; /* pointer to a protocol table entry */
struct sockaddr_in sad; /* structure to hold an IP address */
int sd; /* socket descriptor */
int port; /* protocol port number */
char *host; /* pointer to host name */
int n; /* number of characters read */
char buf_recv[1000],buf_send[100]; /* buffer for data from the server */
char *filename;
char file_buffer[10000];
FILE *fp;

#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &wsaData);
#endif

memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */

if (argc > 2) { /* if protocol port specified */
port = atoi(argv[2]); /* convert to binary */

} else {
port = PROTOPORT; /* use default port number */
}

if (port > 0) /* test for legal value */
sad.sin_port = htons((u_short)port);

else { /* print error message and exit */
fprintf(stderr,"bad port number %s\n",argv[2]);
exit(1);
}

/* Check host argument and assign host name. */
if (argc > 1) {
host = argv[1]; /* if host argument specified */

} else {
host = localhost;
}

/* Convert host name to equivalent IP address and copy to sad. */
ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL ) {
fprintf(stderr,"invalid host: %s\n", host);
exit(1);
}

memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);

/* Map TCP transport protocol name to protocol number. */
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit(1);
}

/* Create a socket. */
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "socket creation failed\n");
exit(1);
}

/* Connect the socket to the specified server. */
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"connect failed\n");
exit(1);
}

n = recv(sd, buf_recv, sizeof(buf_recv), 0);

buf_recv[n]='\0';

printf("%s",buf_recv);
scanf("%s",buf_send);
send(sd,buf_send,strlen(buf_send),0);
n = recv(sd, buf_recv, sizeof(buf_recv), 0);
buf_recv[n]='\0';
printf("%s",buf_recv);
fflush(stdout);

sprintf(buf_send,"Client acknowledges, Sending file now.\n");
send(sd,buf_send, strlen(buf_send),0);

n=recv(sd, file_buffer, sizeof(file_buffer), 0);
file_buffer[n]='\0';
fflush(stdout);

fp = fopen("transferredFile.TXT","w");
fputs(file_buffer,fp);
fclose(fp);
closesocket(sd);

exit(0);
}

问题是来自服务器发送还是客户端接收?谢谢!

最佳答案

服务器:'strcat(file_buffer,f_buffer);' - 连接到未初始化的数组。

服务器和客户端:假设数据不包含空值,即。系统无法传输二进制数据,只能传输文本。

服务器和客户端 - 假设所有文件数据都适合一行。

服务器和客户端 - 假设 TCP 可以传输大于一字节的消息。

可能还有与 TCP 流和 C 空终止字符串相关的其他错误。

关于c - 使用套接字将空文件发送回客户端的简单 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35671762/

25 4 0