Sponsored Content
Full Discussion: Socket Programming
Top Forums Programming Socket Programming Post 302104944 by arunviswanath on Monday 29th of January 2007 07:08:48 PM
Old 01-29-2007
Socket Programming

Hi ,
I'm facing the following problem in socket programming.

My structure is

struct {
int i;
char *str;
}samp;

I'm creating the pointer to this structure and assigning the value to the structure members and send via the socket to the another process.

The receiver process when trying to print the data of this struct members , it is printing only the int data and the str data is missing.

I not able to find out where the problem actually lies.

sample code:

samp s1;
s1 = malloc(sizeof(samp);
s1-> i = 100;
s1->str = "Data";

send(sockfd, s1, sizeof(samp), 0);
// I don't know whether the above statement of data send is correct.

In the receiver end.

samp s2;
s2 = malloc(sizeof(samp));

recv(recvfd, s2, sizeof(samp), 0);

Also please add some note about how to pass the "pointer to a structure" date across the process using sockets.

Thanks in Advance,
Arun Viswanath
 

10 More Discussions You Might Find Interesting

1. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies

2. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

3. Programming

Need Help Regarding Socket Programming

Can anyone plz me. I need a sample code for the following description. Its urgent. It is C/Socket program with the following descriptions: NAME coreadServer - Concurrent Readers Server. coreadClient - Concurrent Readers Client. SYNOPSIS coreadServer <OutputFile> coreadClient <n>... (1 Reply)
Discussion started by: priya.vmr
1 Replies

4. IP Networking

socket programming

my system is a stand alone system... i want to try doing socket porgramming..ihave heard that this is usually done during testing... how can i do that....? (6 Replies)
Discussion started by: damn_bkb
6 Replies

5. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

6. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

7. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

8. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

9. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

10. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies
CMSG_DATA(3)						   BSD Library Functions Manual 					      CMSG_DATA(3)

NAME
CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE -- socket control message routines SYNOPSIS
#include <sys/socket.h> void * CMSG_DATA(struct cmsghdr *); struct cmsghdr * CMSG_FIRSTHDR(struct msghdr *); size_t CMSG_LEN(size_t); struct cmsghdr * CMSG_NXTHDR(struct msghdr *, struct cmsghdr *); size_t CMSG_SPACE(size_t); DESCRIPTION
The control message API is used to construct ancillary data objects for use in control messages sent and received across sockets. Control messages are passed around by the recvmsg(2) and sendmsg(2) system calls. The cmsghdr structure, described in recvmsg(2), is used to specify a chain of control messages. These routines should be used instead of directly accessing the control message header members and data buffers as they ensure that necessary alignment constraints are met. The following routines are provided: CMSG_DATA(cmsg) This routine accesses the data portion of the control message header cmsg. It ensures proper alignment constraints on the beginning of ancillary data are met. CMSG_FIRSTHDR(mhdr) This routine accesses the first control message attached to the message msg. If no control messages are attached to the message, this routine returns NULL. CMSG_LEN(len) This routine determines the size in bytes of a control message, which includes the control message header. len specifies the length of the data held by the control message. This value is what is normally stored in the cmsg_len of each control message. This rou- tine accounts for any alignment constraints on the beginning of ancillary data. This macro might not evaluate to a compile-time con- stant. CMSG_NXTHDR(mhdr, cmsg) This routine returns the location of the control message following cmsg in the message mhdr. If cmsg is the last control message in the chain, this routine returns NULL. CMSG_SPACE(len) This routine determines the size in bytes needed to hold a control message and its contents of length len, which includes the control message header. This value is what is normally stored in msg_msgcontrollen. This routine accounts for any alignment constraints on the beginning of ancillary data as well as any needed to pad the next control message. This macro might not evaluate to a compile- time constant. EXAMPLES
The following example constructs a control message containing a file descriptor and passes it over a socket: struct msghdr msg; struct cmsghdr *cmsg; /* We use a union to make sure hdr is aligned */ union { struct cmsghdr hdr; unsigned char buf[CMSG_SPACE(sizeof(int))]; } *cmsgbuf; /* * We allocate in the heap instead of the stack to avoid C99 * variable stack allocation, which breaks gcc -fstack-protector. */ if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL) err(1, "malloc"); (void)memset(&msg, 0, sizeof(msg)); msg.msg_control = cmsgbuf->buf; msg.msg_controllen = sizeof(cmsgbuf->buf); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; *(int *)CMSG_DATA(cmsg) = fd; if (sendmsg(s, &msg, 0) == -1) err(1, "sendmsg"); free(cmsgbuf); And an example that receives and decomposes the control message: struct msghdr msg; struct cmsghdr *cmsg; union { struct cmsghdr hdr; unsigned char buf[CMSG_SPACE(sizeof(int))]; } *cmsgbuf; if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL) err(1, "malloc"); (void)memset(&msg, 0, sizeof(msg)); msg.msg_control = cmsgbuf->buf; msg.msg_controllen = sizeof(cmsgbuf->buf); if (recvmsg(s, &msg, 0) == -1) err(1, "recvmsg"); if ((msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) errx(1, "control message truncated"); for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { fd = *(int *)CMSG_DATA(cmsg); /* Do something with the descriptor. */ } } free(cmsgbuf); SEE ALSO
recvmsg(2), sendmsg(2), socket(2) HISTORY
The control message API first appeared in 4.2BSD. BSD
June 20, 2008 BSD
All times are GMT -4. The time now is 04:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy