Socket Programming in c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Dear friends,
               Pls any one could help me how to get out of this problem. my problem is I have declared global structure

as 

 struct vnc_virtual_handshake {
        char vnc_cli_version[20] ;
        char auth_result[5];
        char share_desktop_flag[5];
        char client_encodings[66];
        char client_pixel_frmt[50];
        char client_frame_buf_upreq[20];
        int new_server_cfd1;
        };
 struct vnc_virtual_handshake vnc_seamless_handshake;

and I am receiving some data from server like bellow 

local_receive_len = recv(connSock, buf, PKT_BUFSIZ, 0);

and my big confusion is how to assign or copy the content of above "buf"
char array into structure variable ? but I am doing like bellow 
memcpy(vnc_seamless_handshake.vnc_cli_version, buf , local_receive_len);

similarly for remaining variables in the structure I am receiving from multiple receives and assigning 

is it correct way ?

else pls suggest

and also after filling whole structure I am sending to another function their I am accessing using bellow 

struct vnc_virtual_handshake *vnc_seamless_conn_estmnt = (struct vnc_virtual_handshake *)vnc_seamless_handshake;

and sending to remote as bellow 

send(server_fd, &(vnc_seamless_conn_estmnt->vnc_cli_version), sizeof(vnc_seamless_conn_estmnt->vnc_cli_version), 0);

send(server_fd, &(vnc_seamless_conn_estmnt->auth_result), sizeof(vnc_seamless_conn_estmnt->auth_result), 0);

send(server_fd, &(vnc_seamless_conn_estmnt->share_desktop_flag), sizeof(vnc_seamless_conn_estmnt->share_desktop_flag), 0); 


is I am doing correct way of assigning after recv function and sending correct 
please suggest 

Best Regards 
Babu
The receive protocol should match the send protocol. So you could read each field as the send does.

We can agree that it is more efficient to receive the whole structure one and memcpy the entire thing.

There are some things to look out for:
1. The structure must be packed, if it isn't you're write into the padding.
2. You need to send/recv in loops to ensure that the entire buffers are sent/received.
Topic archived. No new replies allowed.