4

这是从服务器程序中的多个客户端向服务器获取月份或年份或时间请求的示例程序,这里我无法将“Fri Jan 11 11:59:51 2008”从服务器程序中的缓冲区发送到客户端.only Fri 单独在客户端阅读。

服务器程序

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

客户计划

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

1 回答 1

8

问题出在服务器代码中:

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

应该

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

sizeof(buff)不会返回 的长度buff,而是其类型的大小。buff是一个指针,因此sizeof(buff)将等于 4(我猜这是您的情况)或 8,具体取决于您的系统。

如果sizeof(buff) == 4,您将发送 的前 4 个字符buff,即 ' Fri'(注意空格,它也是一个字符)。

strlen()将返回字符串的长度,这就是你想要的。

编辑: alk的评论是正确的:你可能会遇到一些麻烦read。请参阅ZanLynx对该问题的评论。

于 2013-08-12T12:42:19.860 回答