gpt4 book ai didi

c - sizeof()在c中返回奇怪的值

转载 作者:太空宇宙 更新时间:2023-11-04 05:03:29 25 4
gpt4 key购买 nike

<分区>

大家好,我正在为一个任务实现一个多协议(protocol)服务器,问题是当我将一个函数(它与 TCP 配合得很好)与 UDP 一起使用时,它会为 sizeof (rslt) 返回一些奇怪的值这是打印 sizeof (rslt) 的结果,

227247297301947350235748709778135745577468557658424998002441906536119128023452627558110097185387624219639637941804882228932644995956639137025857651046253325633445227449505007892388016152332207357456599282029784441594833774964182706535976953843609432439401651880375483842720203925484233013596689866744018141665458868649317570117104675934020967854264479860821412689862026122703619698561786472189815291976898928623462131088247227757807721072403795459334826914847683531968239236940500199813269288827276477929188384103679545017307548348628331645669098568909370657065971531337341841662603278594221673762642070120745706004269203411380905823024264516528460218266891034505741227610635108566850102547194244234491595802548473820585414612221747352614218601769292070105886498675038800726670654977989907519633820118543794748179420330708894380512935922929197901192958685020453701483477243638441444607644584045103566854732472043437182834837031151276986466924005056107273112615576154305580818456831896986707479921666804850676978009175139482011600872247781696198151360337444421986267162518809749962279541373290140958667251725787077177421917491032187282871406009682186676021616280669304703976683626337533413464759379774970193907347344224349646126910484652263652582861429537622470060950001898433056178384737790185034727054100655408078501650100419653460914613312003325114514976904977903673785434865519242218932927846724749932914680623078230459473390721631571879357028192620799780670249628404890444625081372459032351139442349193023231648953178892393215225869560487387734447607698987643759581917130647967805017677771860565983455868285091963816554539133509004989767441167607027792254042536381477844753039244848391452325130886154506629373015159028410740477270477681641090045720626668817450051271286643920235139888460288674438576178355066172484843620406990305664863352348519178027532105785588869464103655179641271672762329909252285829060459009910768529658550406700160376994566456247010717483703953366527358263048640804889600709363152032432252410085951706712799052430344895928694442582957645431633369053034305667463823090663464760738164090556152525774466164744037686011101663800170078366921209056500303914474056087892235358298164024357215391588641194707115978140407226154118906542449763834465166058632126443504894420499741513006310822583552919768761548462977412311647236868595822220663005491511561850746178480104681354464439867884163742890098946294243688211351484163203709554184403155848611709506733882593468169901350306884677995915932010321114285505973740406847370286120635158347518247510277661323300043873047950392164942431645418302947950413506210734540437852746639625375347610012995678139901099918378380261823821336209645745980214130228811271994044550978448678801380815142903470540468943168894917972070887965091844883478597646129241181927386702586436228869778709864490945388199788157229336144495868928370130428522581684988316175567123887288601709737949842018996477785851910159268866322819839489792236073965243444078966709362490743754122694159124524052020792484412812868943800034992477766811147183724209135415987638202158028537905514471899977100572198298304042878318729460950115591276417376811003545986574793531075323131684626381339291509853454358163265714861331384777107194532363959316468225193409347344254519577720009422713289644543760402612121969692919169780911452088487833412590590051242129109845157720008193666133253502916786223810520703777964003329058076952385412988850950651536066213022496822713600805867150527938872166308595393085376986603206932959009695449149501563093375374780821185997456057835811105292775253063772002382719467857393447371079222454636084693485484877479484598463692899799144769464635549655732415843871907114705042415521903704488250326193374402414538809696743414494509229818516037189690520555940494775487076957344200010781415703176415728240860895233439634632912797526990278731739984780391745714390037327203315505427496375837124960562284419088384.000000

服务器

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <errno.h>

#define MAXLINE 100


struct equation {
float a;
char c;
float b;
};

struct result {
long double res;
};

int MAX (int a,int b){
if (a>b) return a;
return b;
}


int readn(int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0; /* and call read() again */
else
return (-1);
} else if (nread == 0)
break; /* EOF */

nleft -= nread;
ptr += nread;
}
return (n - nleft); /* return >= 0 */
}


int writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
printf ("\nI'm In %Lf\n",n);
ptr = vptr;
nleft = n;
printf ("%Lf NNNNN %Lf SIZE %Lf \n",nleft,n,sizeof(*vptr));
while (nleft > 0) {
printf ("%Lf\n",nleft);
fflush (stdout);
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return (-1); /* error */
}

nleft -= nwritten;
ptr += nwritten;
}
printf ("\n 5alawees \n");
// printf ("\n%s\n",vptr);
// ffulsh (stdout);
return (n);
}

void HandleClient(int comm_fd);
void Die (const char * msg)
{
perror(msg);
exit(1);
}

int passiveUDP (short port){
struct sockaddr_in servaddr;
int listen_fd;
if ((listen_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
Die("Falied to create socket");
};

//printf ("%d" ,listen_fd);
memset( &servaddr,0, sizeof(servaddr));

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

if (bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0)
{
Die("Failed to bind socket to address");
}
return listen_fd;
}

int passiveTCP (short port){
struct sockaddr_in servaddr;
int listen_fd;
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
Die("Falied to create socket");
};

memset( &servaddr,0, sizeof(servaddr));

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

if (bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0)
{
Die("Failed to bind socket to address");
}

if (listen(listen_fd, 10) < 0)
{
Die("Failed to listen on server socket");
}
return listen_fd;
}

int main(int argc, char * argv[])
{
if (argc != 2) {
fprintf(stderr, "USAGE: ./HelloITServer <port>\n");
exit(1);
}
char str[100];
int listen_fd, comm_fd;
int usock = passiveUDP (atoi (argv[1])); /* UDP socket */

int tsock = passiveTCP (atoi (argv[1])); /* TCP master socket */
int nfds;
fd_set rfds; /* readable file descriptors */
struct sockaddr_in fsin; /* the request from address */

nfds = MAX(tsock, usock) + 1;
FD_ZERO(&rfds);
while (1) {
FD_SET(tsock, &rfds);
FD_SET(usock, &rfds);
printf ("HELLO");
if(select(nfds, &rfds, NULL, NULL, NULL) < 0){
printf("select error: %d \n",errno);
exit (1);
}
if(FD_ISSET(tsock, &rfds))
{
/* TCP slave socket */
printf ("Hello TCP");
int ssock;
//int alen = sizeof(fsin);
ssock = accept(tsock, (struct sockaddr *) NULL, NULL);
if(ssock < 0)
Die("accept failed: jkjkjkjkjkj \n");
HandleClient (ssock);
close (ssock);
}
if(FD_ISSET(usock, &rfds))
{
printf ("Hello UDP");
HandleClient (usock);
}
}

}
void HandleClient(int comm_fd)
{
struct equation eq;
struct result rslt;
bzero (&eq,sizeof (eq));
bzero (&rslt, sizeof (rslt));
if ((readn (comm_fd, &eq, sizeof(eq))) == 0){
Die("Failed to receive from client");
}
printf ("\n%Lf %Lf\n",sizeof (struct result),sizeof (rslt));
printf ("reciveed %f %c %f",eq.a,eq.c,eq.b);
switch (eq.c) {
case '+':
rslt.res = eq.a+eq.b;
break;
case '-':
rslt.res = eq.a-eq.b;
break;
case '*':
rslt.res = eq.a*eq.b;
break;
case '/':
rslt.res = eq.a/eq.b;
break;
case '%':
rslt.res = (int)eq.a% (int)eq.b;
break;
default:
break;
}
printf ("\n%Lf\n",sizeof(rslt));
printf ("\n%Lf\n",sizeof(rslt));
printf ("\n%Lf\n",sizeof(rslt));

writen (comm_fd, &rslt, sizeof (rslt));
//close (comm_fd);
}

客户端(以防万一你们需要它)

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<unistd.h>
#include <float.h>
#include <errno.h>

struct equation {
float a;
char c;
float b;
};

struct result {
long double res;
};

int readn(int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0; /* and call read() again */
else
return (-1);
} else if (nread == 0)
break; /* EOF */

nleft -= nread;
ptr += nread;
}
return (n - nleft); /* return >= 0 */
}

int writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;

ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return (-1); /* error */
}

nleft -= nwritten;
ptr += nwritten;
}
return (n);
}

int main(int argc,char *argv[])
{
int sockfd,n;
char sendline[100];
char recvline[100];
struct sockaddr_in servaddr;
if (argc != 3) {
fprintf(stderr, "USAGE: ./HelloClient <server_ip> <port>\n");
exit(1);
}

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

servaddr.sin_family=AF_INET;
servaddr.sin_port= htons(atoi(argv[2]));

inet_pton(AF_INET,argv[1],&(servaddr.sin_addr));

connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
struct result rslt;
struct equation eq;
//while(1)
//{
printf ("Accepted values by the program, from %f to %f\n", FLT_MIN, FLT_MAX);
printf ("\n possible operations are addition, substraction, division, multiplication and modulo with operators : +,-,*,/,% respectivly\n");
printf ("\nPlease enter the equation in this form only : \"5.0 + 2.0\" with a single space\n");
scanf ("%f %c %f", &eq.a,&eq.c,&eq.b);
if (eq.c!='+' && eq.c!='-' && eq.c!='*' && eq.c!='/' && eq.c!='%'){
printf ("\n possible operations are addition, substraction, division, multiplication and modulo with operators : +,-,*,/,% respectivly\n");
exit (1);
}
if (eq.c== '%'){
if (!(eq.a == (float) ((int) eq.a) && eq.b == (float) ((int) eq.b))){
printf ("Only integer values are accepted with the % operation, please rerun the program\n");
exit (1);
}
}
//bzero( &eq, sizeof(eq));
bzero( &rslt, sizeof(rslt) );
//fgets(sendline,100,stdin); /*stdin = 0 , for standard input */
writen (sockfd, &eq, sizeof(eq));

readn (sockfd, &rslt, sizeof(rslt));
printf("%Lf\n thank you for using this marvelous calculator!\n",rslt.res);
bzero( &eq, sizeof(eq));
exit (1);
//}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com