gpt4 book ai didi

c - 发送在循环中仅工作一次

转载 作者:行者123 更新时间:2023-11-30 19:10:04 24 4
gpt4 key购买 nike

嗨,我正在尝试使用 TCP 连接我从客户端获取文件名并将其发送到服务器。服务器检查文件是否存在,否则发送适当的消息。如果文件存在,数据将发送到客户端我只收到一次文件内容。

服务器程序

#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>

int main()
{
int s,b,opt,port,ns,len,recb,sntd;
char server_addr[100],*gets_func;
struct sockaddr_in server,client;
char buff[50];
printf("Enter the port number ");
scanf("%d",&port);

// The socket is now being created
s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1)
{
perror("SOCKET NOT CREATED \n");
close(s);
exit(0);
}
printf("SOCKET SUCCESSFULL ");
printf("\n Do you wish to input address \n 1.YES 2.NO : ");
scanf("%d",&opt);

if(opt==1)
{

printf("Input the address of ther server :");
scanf("%s",server_addr);
printf("%s",server_addr);
server.sin_addr.s_addr=inet_addr(server_addr);
}
else
{
server.sin_addr.s_addr=inet_addr("127.0.0.3");

}
server.sin_family=AF_INET;
server.sin_port=htons(port);
// Bind the server to the given address

b=bind(s,(struct sockaddr*)&server,sizeof(server));
if(b==-1)
{
perror("\nBind not successfull");
exit(0);
}
printf("\nBIND SUCCESSFULL ");
b=listen(s,1);
if(b==-1)
{
perror("Error listening :");
exit(0);
}
printf("\n Socket listening");
len=sizeof(client);
ns=accept(s,(struct sockaddr*)&client,&len);
if(ns==-1)
{
close(s);
exit(0);
}
printf("\nSocket accepted");
while(1)
{

recb=recv(ns,buff,sizeof(buff),0);
if(recb==-1)
{
perror("\nerror receving message : ");
exit(0);
}

if(strcmp(buff,"exit")==0)
break;
printf("\nThe File Path is %s ", buff);



// Accessing the file
char contents[1000],ch;

FILE *fp;
int i=0;
if(access(buff,F_OK)!=-1)
{
fp=fopen(buff,"r");
printf("\n");
while((ch=fgetc(fp))!=EOF)
{
//printf("%c - %d \n",ch,acii);
contents[i]=ch;
i++;
}
fclose(fp);
contents[i]='\0';
puts(contents);

}
else
{
char temp[]="FILE DOES'NT EXITS";
strcpy(contents,temp);
i=strlen(contents);
i++;
}
contents[i]='\0';





sntd=send(ns,contents,sizeof(contents),0);
if(sntd==-1)
{
perror("\nerror sending");
exit(0);
}
//printf("\nSent succesfully");
//printf("\n");
}
close(s);
}

客户端程序

#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>

int main()
{

int s,b,opt,port,r,len,sntd,recb;
char server_addr[100],*gets_func;
struct sockaddr_in server,client;
char buff[50];
printf("Enter the port number for client");
scanf("%d",&port);

// The socket is now being created
s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1)
{
perror("SOCKET NOT CREATED \n");
exit(0);
}
printf("SOCKET SUCCESSFULL ");
printf("\n Do you wish to input address for client \n 1.YES 2.NO : ");
scanf("%d",&opt);

if(opt==1)
{
printf("Input the address of ther server :");
scanf("%s",server_addr);
printf("%s",server_addr);
server.sin_addr.s_addr=inet_addr(server_addr);
}
else
{
server.sin_addr.s_addr=inet_addr("127.0.0.3");

}
server.sin_family=AF_INET;
server.sin_port=htons(port);
// Bind the server to the given address

r=connect(s,(struct sockaddr*)&server,sizeof(server));
if(r==-1)
{
perror("Failed connecting");
exit(0);
}
printf("\nConnected succesfully");
while(1)
{
printf("\n Type File name:");
scanf("%s",buff);


sntd=send(s,buff,sizeof(buff),0);
if(sntd==-1)
{
perror("\n Message not send error :");
exit(0);
}



if(strcmp(buff,"exit")==0)
break;

memset(buff,0,50);
recb=recv(s,buff,sizeof(buff),0);
if(recb==-1)
{
perror("\n error receving message : ");
exit(0);
}
buff[recb]='\0';
printf("\n %s", buff);
printf("\n");
}
close(s);
}

最佳答案

TCP 是字节流协议(protocol),而不是消息协议(protocol)。如果您想发送和接收消息,则必须编写“发送消息”和“接收消息”函数并调用它们。您不能只调用 recv 并期望它知道您的消息是什么,因为它不知道。

关于c - 发送在循环中仅工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41953658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com