C中涉及套接字和客户机-服务器通信的程序出现问题
我正在用C编写一个程序,该程序涉及客户机-服务器连接以及双方之间的通信 该程序包括客户端向服务器发送一封信,服务器获取该信。然后,服务器在当前文件目录(linux中)中搜索以该字母开头的文件,并向客户端发送文件的字节数和文件文本 整个程序非常长,对于作业,讲师已经完成了许多代码,例如设置套接字和为客户端操作创建整个程序 对于服务器端,我必须为以下内容编写代码:C中涉及套接字和客户机-服务器通信的程序出现问题,c,sockets,server,directory,client,C,Sockets,Server,Directory,Client,我正在用C编写一个程序,该程序涉及客户机-服务器连接以及双方之间的通信 该程序包括客户端向服务器发送一封信,服务器获取该信。然后,服务器在当前文件目录(linux中)中搜索以该字母开头的文件,并向客户端发送文件的字节数和文件文本 整个程序非常长,对于作业,讲师已经完成了许多代码,例如设置套接字和为客户端操作创建整个程序 对于服务器端,我必须为以下内容编写代码: 从传递的内存中获取文件描述符并强制转换它 -收到客户的来信 -正在尝试打开当前目录 -在目录中迭代查找以字母开头的文件 -正在尝试打
- 从传递的内存中获取文件描述符并强制转换它
void* handleClient (void* vPtr
)
{
// I. Application validity check:
int fd = *((int *) vPtr);
//casting vPtr to an int//
free(vPtr);
// II. Handle the client:
char buffer[BUFFER_LEN+1];
read(fd, buffer, BUFFER_LEN+1);
//read the letter into a buffer//
const char* dirNamePtr = ".";
DIR* dirPtr = opendir(dirNamePtr);
// Open the current directory
if (dirPtr == NULL)
{
int toSend = htonl(CANT_READ_DIR_CODE);
write(fd,&toSend,sizeof(toSend));
printf("Cannot read directory\n");
return(NULL);
}
// If current directory cannot be opened, it sends a error message in network // endian to the client
struct dirent* entryPtr;
char path[BUFFER_LEN];
struct stat statBuffer;
//implements struct dirent to get info on the directory
//iterates through the directory
while ((entryPtr=readdir(dirPtr)) != NULL)
{
stat(entryPtr->d_name, &statBuffer);
//puts in metaddata of the current directory into statbuffer
if (!S_ISREG(statBuffer.st_mode))
continue;
//if the entry is not a file, continue
// if the first letter of the file is not the character received from the //client, send an error mesage
if(entryPtr->d_name[0]!=buffer[0]) {
int toSend2 = htonl(NO_MATCH_CODE);
write(fd,&toSend2,sizeof(toSend2));
printf("No matching file\n");
return(NULL);
}
int ab;
int numRead;
int numBytes;
char buffer[BUFFER_LEN];
//open the file and send bytes of file and file size to client
if (entryPtr->d_name[0]==buffer[0] &(S_ISREG(statBuffer.st_mode)))
{
ab=open(entryPtr->d_name,O_RDONLY,0660);
if(ab<0) {
int toSend3 = htonl(CANT_READ_FILE_CODE);
write(fd,&toSend3, sizeof(toSend3));
printf("Cannot read <filename>\n");
return(NULL);
}
numBytes=htonl(statBuffer.st_size);
write(fd, &numBytes, sizeof(numBytes));
printf("Sending %s, %d bytes\n",entryPtr >d_name,statBuffer.st_size);
while((numBytes=read(ab,buffer,BUFFER_LEN))>0)
{
printf("We read %d bytes\n", numBytes);
write(fd, buffer, numBytes);
}
//close the fiel
close(ab);
}
break;
//leave the loop
}
// III. Finished:
//
closedir(dirPtr);
return(NULL);
}
void*handleClient(void*vPtr
)
{
//一、申请有效性检查:
int fd=*((int*)vPtr);
//将vPtr强制转换为int//
免费(vPtr);
//二、接待客户:
字符缓冲区[buffer_LEN+1];
读取(fd、缓冲器、缓冲器长度+1);
//把信读入缓冲区//
常量字符*dirNamePtr=“.”;
DIR*dirPtr=opendir(dirNamePtr);
//打开当前目录
if(dirPtr==NULL)
{
int toSend=htonl(无法读取方向码);
写入(fd,&toSend,sizeof(toSend));
printf(“无法读取目录\n”);
返回(空);
}
//如果当前目录无法打开,它将在network//endian中向客户端发送一条错误消息
结构方向*entryPtr;
字符路径[BUFFER_LEN];
结构statBuffer;
//实现struct dirent以获取目录信息
//遍历目录
while((entryPtr=readdir(dirPtr))!=NULL)
{
状态(入口PTR->d_名称和状态缓冲);
//将当前目录的metaddata放入statbuffer
如果(!S_ISREG(statBuffer.st_模式))
继续;
//如果条目不是文件,请继续
//如果文件的第一个字母不是从//客户端收到的字符,请发送错误消息
if(entryPtr->d_name[0]!=buffer[0]){
int toSend2=htonl(无匹配代码);
写入(fd,&toSend2,sizeof(toSend2));
printf(“没有匹配的文件\n”);
返回(空);
}
int-ab;
国际货币联盟;
整数单位;
字符缓冲区[buffer_LEN];
//打开文件并将文件字节和文件大小发送到客户端
if(entryPtr->d_name[0]==buffer[0]&(S_ISREG(statBuffer.st_模式)))
{
ab=打开(entryPtr->d_name,O_RDONLY,0660);
if(abd_名称、statBuffer.st_尺寸);
而((numBytes=read(ab,buffer,buffer_LEN))>0)
{
printf(“我们读取了%d字节\n”,numBytes);
写入(fd、缓冲区、字节);
}
//关门大吉
关闭(ab);
}
打破
//退出循环
}
//三、完成:
//
closedir(dirPtr);
返回(空);
}
我的代码可以编译,但在尝试运行时不会将文件发送到客户端。我试过几封不同的信,但都没用。我不太清楚是什么问题使我很难纠正错误
我不是在问答案或其他什么,只是帮我看看我错在哪里。感谢您的帮助。您关于何时发送与何时不发送文件状态的逻辑似乎有误。我认为应该是这样的(很好的警告,我没有测试它,甚至没有在基本语法检查之外编译它,但你应该知道):
void*handleClient(void*vPtr)
{
//一、申请有效性检查:
int fd=*((int*)vPtr);
免费(vPtr);
//二、接待客户:
字符缓冲区[buffer_LEN+1];
读取(fd、缓冲器、缓冲器长度+1);
//把信读入缓冲区//
常量字符*dirNamePtr=“.”;
DIR*dirPtr=opendir(dirNamePtr);
//打开当前目录
if(dirPtr==NULL)
{
int toSend=htonl(无法读取方向码);
写入(fd,&toSend,sizeof(toSend));
printf(“无法读取目录\n”);
返回(空);
}
结构方向*entryPtr;
字符路径[BUFFER_LEN];
结构statBuffer;
//实现struct dirent以获取目录信息
//遍历目录
while((entryPtr=readdir(dirPtr))!=NULL)
{
状态(入口PTR->d_名称和状态缓冲);
//将当前目录的metaddata放入statbuffer
//如果这不是常规文件或第一个字符不匹配。。。
如果(!S_ISREG(statBuffer.st_模式)| entryPtr->d_name[0]!=buffer[0])
继续;
int-ab;
国际货币联盟;
整数单位;
字符缓冲区[buffer_LEN];
//打开文件并将文件字节和文件大小发送到客户端
ab=打开(entryPtr->d_name,O_RDONLY,0660);
if(abd_名称、statBuffer.st_尺寸);
而((numBytes=read(ab,buffer,buffer_LEN))>0)
{
printf(“我们读取了%d字节\n”,numBytes);
写入(fd、缓冲区、字节);
}
//关闭文件并离开
void* handleClient(void* vPtr)
{
// I. Application validity check:
int fd = *((int *) vPtr);
free(vPtr);
// II. Handle the client:
char buffer[BUFFER_LEN+1];
read(fd, buffer, BUFFER_LEN+1);
//read the letter into a buffer//
const char* dirNamePtr = ".";
DIR* dirPtr = opendir(dirNamePtr);
// Open the current directory
if (dirPtr == NULL)
{
int toSend = htonl(CANT_READ_DIR_CODE);
write(fd,&toSend,sizeof(toSend));
printf("Cannot read directory\n");
return(NULL);
}
struct dirent* entryPtr;
char path[BUFFER_LEN];
struct stat statBuffer;
//implements struct dirent to get info on the directory
//iterates through the directory
while ((entryPtr=readdir(dirPtr)) != NULL)
{
stat(entryPtr->d_name, &statBuffer);
//puts in metaddata of the current directory into statbuffer
// if this isn't a regular file OR the first char doesn't match...
if (!S_ISREG(statBuffer.st_mode) || entryPtr->d_name[0]!=buffer[0])
continue;
int ab;
int numRead;
int numBytes;
char buffer[BUFFER_LEN];
//open the file and send bytes of file and file size to client
ab = open(entryPtr->d_name,O_RDONLY,0660);
if(ab<0) {
int toSend3 = htonl(CANT_READ_FILE_CODE);
write(fd,&toSend3, sizeof(toSend3));
printf("Cannot read <filename>\n");
closedir(dirPtr);
return(NULL);
}
numBytes=htonl(statBuffer.st_size);
write(fd, &numBytes, sizeof(numBytes));
printf("Sending %s, %d bytes\n",entryPtr >d_name,statBuffer.st_size);
while((numBytes=read(ab,buffer,BUFFER_LEN))>0)
{
printf("We read %d bytes\n", numBytes);
write(fd, buffer, numBytes);
}
//close the file and leave
close(ab);
break;
}
// if this is NULL it means we dind't send anything. we break the loop
// when a file to send it discovered.
if (entryPtr == NULL)
{
printf("No matching file\n");
int toSend2 = htonl(NO_MATCH_CODE);
write(fd, &toSend2, sizeof(toSend2));
}
// III. Finished:
closedir(dirPtr);
return(NULL);
}