kautilya hari
kautilya hari

Reputation: 213

send working only once in the loop

Hi i am trying to work on tcp connection I take the file name from client and send it to server. The server checks if file exits or else it sends the appropriate message. if file exits the data is sent to client i receive the file content only once.

Server program

#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);
}

Client Program

#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);
}

Upvotes: 0

Views: 306

Answers (1)

David Schwartz
David Schwartz

Reputation: 182769

TCP is a byte-stream protocol, not a message protocol. If you want to send and receive messages, you will have to write a "send a message" and "receive a message" function and call those. You can't just call recv and expect it to know what your messages are because it has no idea.

Upvotes: 1

Related Questions