sockets in c tcp
im tryin to send a 2d array in simple socket program..but this only sends the first row and never anything beyond that/....what is the problem here... ?
开发者_Python百科server part ...
struct sockaddr_in clienta,servera;
int s ;
s = socket(AF_INET,SOCK_STREAM,0);
servera.sin_family = AF_INET;
servera.sin_port = htons(3386);
servera.sin_addr.s_addr = htonl(INADDR_ANY);
bind(s,(struct sockaddr *)&servera,sizeof(servera));
listen(s,1);
int news;
int len = sizeof(clienta);
printf("waiting for connection");
news = accept(s,(struct sockaddr *)&clienta , &len);
printf("\n received connection");
int c[3][3];
recv(news, &c,sizeof(c),0);
int d = sizeof(c);
int i=0,j=0;
for(;i<=2;i++)
for(;j<=2;j++)
printf("\n %i,%i,%i",i,j,c[i][j]);
printf("\n %i",c[1][0]);
here is the client program
struct sockaddr_in servera;
int s ;
s = socket(AF_INET,SOCK_STREAM,0);
servera.sin_family = AF_INET;
servera.sin_port = htons(3386);
servera.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(s,(struct sockaddr *)&servera, sizeof(servera));
int b[3][3];
int i=0,j=0;
for(;i<3;i++)
for(;j<3;j++)
b[i][j]=4;
printf("connected");
send(s,&b,sizeof(b),0);
int l = sizeof(b);
You know what these:
for(;i<=2;i++)
for(;j<=2;j++)
printf("\n %i,%i,%i",i,j,c[i][j]);
and
for(;i<=2;i++)
for(;j<=2;j++)
printf("\n %i,%i,%i",i,j,c[i][j]);
do? j
is initialized just once (:
So, once j
gets 3
on i = 0
, when i = 1
and i = 2
j
is still 3
and the second for
is not executed, that's why you send only the first row.
精彩评论