当前位置: 技术问答>linux和unix
关于socket的一个问题?
来源: 互联网 发布时间:2015-02-17
本文导语: //server.c typedef struct { char name[20]; char national[40]; } STUDENT; main() { STUDENT jack = { "willianmus Keve uu", "England china jpan frica europ", }; int sock_fd, new_fd; usDataBytes = strlen(jack.name) + strlen(jack.national)...
//server.c
typedef struct
{
char name[20];
char national[40];
} STUDENT;
main()
{
STUDENT jack =
{
"willianmus Keve uu",
"England china jpan frica europ",
};
int sock_fd, new_fd;
usDataBytes = strlen(jack.name) + strlen(jack.national);
.
//建立socket过程略
.
if(!fork())
{
if(send(new_fd, &jack, usDataBytes, 0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
}
//client.c
typedef struct
{
char name[20];
char national[40];
} STUDENT;
int main(int argc, char* argv[])
{
//建立connect过程略
bzero(&rcv_message, sizeof(rcv_message));
if( (numbytes = recv(sock_fd, &rcv_message, sizeof(rcv_message), 0)) == -1 )
{
perror("recv");
exit(1);
}
printf("Courrent Received %d Bytesn", numbytes);
//buf[numbytes] = 'n';
printf("Name is %sn", rcv_message.name);
printf("National is %sn", rcv_message.national);
close(sock_fd);
return 0;
}
客户端最后显示的结果是:
Current Received 50 Bytes
Name is willianmus Keve uu
National is England china jpan frica euro //这里缺少一个字母p
问:这个程序为什么会少输出一个字母
typedef struct
{
char name[20];
char national[40];
} STUDENT;
main()
{
STUDENT jack =
{
"willianmus Keve uu",
"England china jpan frica europ",
};
int sock_fd, new_fd;
usDataBytes = strlen(jack.name) + strlen(jack.national);
.
//建立socket过程略
.
if(!fork())
{
if(send(new_fd, &jack, usDataBytes, 0) == -1)
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd);
}
}
}
//client.c
typedef struct
{
char name[20];
char national[40];
} STUDENT;
int main(int argc, char* argv[])
{
//建立connect过程略
bzero(&rcv_message, sizeof(rcv_message));
if( (numbytes = recv(sock_fd, &rcv_message, sizeof(rcv_message), 0)) == -1 )
{
perror("recv");
exit(1);
}
printf("Courrent Received %d Bytesn", numbytes);
//buf[numbytes] = 'n';
printf("Name is %sn", rcv_message.name);
printf("National is %sn", rcv_message.national);
close(sock_fd);
return 0;
}
客户端最后显示的结果是:
Current Received 50 Bytes
Name is willianmus Keve uu
National is England china jpan frica euro //这里缺少一个字母p
问:这个程序为什么会少输出一个字母
|
用SIZE去内存区大小吧.
不要用usDataBytes = strlen(jack.name) + strlen(jack.national);这么弱的语句
不要用usDataBytes = strlen(jack.name) + strlen(jack.national);这么弱的语句