发送在循环中只工作一次

send working only once in the loop

你好,我正在尝试使用 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]='[=10=]';
        puts(contents);

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





    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]='[=11=]';
    printf("\n %s", buff);
    printf("\n");
}
close(s);
}

TCP 是 byte-stream 协议,而不是消息协议。如果你想发送和接收消息,你将不得不编写一个 "send a message" 和 "receive a message" 函数并调用它们。您不能只调用 recv 并期望它知道您的消息是什么,因为它不知道。