Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass the entire string over the read and write function in TCP socket programming in c program

Tags:

c

sockets

here is sample program to get the month or year or time request from multiple client to server in server program, here i am not able to send "Fri Jan 11 11:59:51 2008" the full string from buffer in server program to client.only Fri alone read in the client.

SERVER program

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include <unistd.h>
#include <fcntl.h>
#include<sys/stat.h>
#include<time.h>

int main(int argc,char **argv) {
    int len,ch=0;
    int sockfd,newfd;
    struct sockaddr_in servaddr,cliaddr;
    char str[1000];

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
        printf("cannot create socket");

    bzero(&servaddr,sizeof(servaddr));

    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=INADDR_ANY;      
    servaddr.sin_port=htons(7220);

    if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
        printf("Bind error");

    listen(sockfd,5);

    len=sizeof(cliaddr);
    time_t current_time = time(NULL);
    struct tm* local_time = localtime(&current_time); 
    char *buff=asctime(local_time);
    int leng=sizeof(buff);

    //strcpy(buff,buff1);

    printf("%d",leng);
    printf("%s",buff);

    int n=0;

    while(1) {
        newfd=accept(sockfd,(struct sockaddr*)&cliaddr,&len);
        printf("request from client%d",n++);
        n=write(newfd,buff,sizeof(buff));
        //n=write(newfd,buff1+1,sizeof(buff1));
        printf("message sent to client%d",n);
    }

    close(newfd);
    close(sockfd);
}

CLIENT program

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

int main(int argc,char **argv) {
    int len;
    int sockfd,n;
    struct sockaddr_in servaddr,cliaddr;
    char str[7][100],st[3][100];
    int d,m,choice=0;
    char buff[1024],buff1[1024];

    sockfd=socket(AF_INET,SOCK_STREAM,0);
    bzero(&servaddr,sizeof(servaddr));

    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=inet_addr(argv[1]);
    servaddr.sin_port=htons(7220);

    connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));

    bzero(&buff,sizeof(buff));
    bzero(&buff1,sizeof(buff1));

    n=read(sockfd,buff,sizeof(buff));

    printf("hai");  

    puts(buff);

    //puts(buff1);
    /*strcpy(str[1],strtok(buff," "));
    strcpy(str[2],strtok(NULL," "));
    strcpy(str[0],strtok(NULL," "));
    strcpy(str[4],strtok(NULL," "));
    strcpy(str[3],strtok(NULL," "));        
    while(choice!=6) {
        printf("\n\nMENU\n");
        printf("1.DATE\n");
        printf("2.DAY\n");
        printf("3.MONTH\n");
        printf("4.YEAR\n");
        printf("5.TIME\n");
        printf("6.EXIT\n");
        printf("ENTER CHOICE\n");
        scanf("%d",&choice);
        switch(choice) {
          case 1: printf("date %s",str[0]);
            break;
          case 2: printf("day %s",str[1]);
            break;
          case 3: printf("month %s",str[2]);
            break;
          case 4: printf("year %s",str[3]);
            break;
          case 5: printf("time %s",str[4]);
            break;
        }
    }*/

    close(sockfd);
    return 0;
}
like image 962
Sam Avatar asked Feb 15 '23 14:02

Sam


1 Answers

The problem is in the server code:

n=write(newfd,buff,sizeof(buff));

should be

n=write(newfd,buff,strlen(buff) + 1);

sizeof(buff) will not return the length of buff, but the size of its type. buff is a pointer, so sizeof(buff) will be equal to 4 (this is your case I guess) or 8, depending on your system.

If sizeof(buff) == 4, you will send the first 4 characters of buff, which will be 'Fri' (notice the space, which is a character too).

strlen() will return you the length of the string, and that's what you want.

EDIT: alk's comment is right : You can have some trouble with read. See ZanLynx's comment on the question.

like image 95
nouney Avatar answered Mar 08 '23 23:03

nouney