- From: "Oon Heok Kee" <oonheokkee78 AT hotmail.com>
- To: discussion AT sctp.de
- Subject: [SCTP-Discussion] What does ext_sendmsg() return??
- Date: Wed Sep 4 08:02:01 2002
- List-id: SCTP Discussions <discussion.sctp.de>
Hi,
I would like to ask, what's the value that ext_sendmsg() return when it's
called? If I didn't understand wrongly, it returns the number of bytes that
it's sending, isn't it? Am I right to assume that if it returns a negative
value, some error has occurred?
I have attached my code as below, because at the portion where it calls
ext_sendmsg(), it always returns an error saying "send() failed: Cannot send
after transport endpoint shutdown".
Is it because ext_sendmsg() is trying to send an empty user data
buffer?
Thank you!!
Oon, Heok Kee
************************************************************
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <sys/types.h>
#include <sys/time.h>
#include <ext_socket.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sctp.h>
#define MAXPENDING 5 /* Maximum outstanding connection requests */
#define RCVBUFSIZE 1024 /* Size of receive buffer */
#define OUTSTREAM 3 /* Number of out-bound streams to initialise */
#define INSTREAM 3 /*Number of in-bound streams to initialise */
void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}
void HandleSCTPClient(int clntSocket)
{
char echoBuffer[RCVBUFSIZE];
int recvMsgSize, count = 0; /* Size of received message */
struct msghdr msg[1];
struct cmsghdr *cmsg;
struct sctp_sndrcvinfo *sri;
struct iovec iov[1];
size_t cmsglen = sizeof(struct cmsghdr) + sizeof(struct
sctp_sndrcvinfo) + sizeof(echoBuffer);
char cbuf[cmsglen];
memset(msg, 0, sizeof(*msg));
msg->msg_control = cbuf;
msg->msg_controllen = cmsglen;
msg->msg_flags = 0;
msg->msg_namelen = 0;
msg->msg_name = NULL;
printf("Receiving message from client...\n\n");
(&iov[0])->iov_base = echoBuffer;
(&iov[0])->iov_len = sizeof(echoBuffer);
msg->msg_iov = (struct iovec*)&iov;
msg->msg_iovlen = 1;
cmsg = (struct cmsghdr *)cbuf;
sri = (struct sctp_sndrcvinfo *)(cmsg + 1);
/*
cmsg->cmsg_len = cmsglen;
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_SNDRCV;
sri->sinfo_assoc_id = 0;
sri->sinfo_stream = 1;
sri->sinfo_flags = 0;
sri->sinfo_ppid = 0x12345678;
sri->sinfo_context = (uint32_t)NULL;
sri->sinfo_timetolive = (unsigned int)-1;
*/
printf("Message received: ");
do{
recvMsgSize = 0;
/*msg->msg_flags = 0;
msg->msg_control = cbuf;
msg->msg_controllen = cmsglen;
*/
if ((recvMsgSize = ext_recvmsg(clntSocket, msg, 0)) < 0)
{
DieWithError("recvmsg() failed");
} else {
echoBuffer[recvMsgSize] = '\0';
printf("%s", echoBuffer);
}
//just took out the below part (3)
/* (&iov[0])->iov_base = echoBuffer;
(&iov[0])->iov_len = strlen(echoBuffer);
msg->msg_iov = (struct iovec*)&iov;
msg->msg_iovlen = 1;
*/
msg->msg_flags = 0;
msg->msg_control = cbuf;
msg->msg_controllen = sizeof(struct cmsghdr) + sizeof(struct
sctp_sndrcvinfo);
msg->msg_name = NULL;
msg->msg_namelen = 0;
//just copied this block from above(1)
cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct
sctp_sndrcvinfo);
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_SNDRCV;
sri->sinfo_assoc_id = 0;
sri->sinfo_stream = 1;
sri->sinfo_flags = 0;
sri->sinfo_ppid = 0x12345678;
sri->sinfo_context = (uint32_t)NULL;
sri->sinfo_timetolive = (unsigned int)-1;
if (count=ext_sendmsg(clntSocket, msg, 0) < 0)// &&
(recvMsgSize < 0))
DieWithError("send() failed");
printf("count = %d\n", count);
} while (recvMsgSize > 0);
printf("\n\nClosing connection... ");
ext_close(clntSocket); /* Close client socket */
printf("Connection closed.\n\n");
}
int main(int argc, char *argv[])
{
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned int clntLen; /* Length of client address */
unsigned short echoServPort; /* Server port */
struct sctp_initmsg sim;
struct sctp_event_subscribe events;
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}
echoServPort = atoi(argv[1]); /* First arg: local port */
printf("\nCreating socket... ");
/* Create socket for incoming connections */
if ((servSock = ext_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) < 0)
DieWithError("socket() failed");
printf("Socket created.\n\n");
/* Construct local address structure */
/* Zero out structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;/* Internet addr. family */
/* Any incoming interface */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
echoServAddr.sin_port = htons(echoServPort); /* Local port */
/* To initialise the number of in and out streams needed with the
client */
(&sim)->sinit_num_ostreams = OUTSTREAM;
(&sim)->sinit_max_instreams = INSTREAM;
(&sim)->sinit_max_attempts = 3;
(&sim)->sinit_max_init_timeo = 30;
if (ext_setsockopt(servSock, IPPROTO_SCTP, SCTP_INITMSG, &sim,
sizeof(sim)) < 0)
{
DieWithError("setsockopt() failed");
exit(1);
}
memset((char *)&events, 0, sizeof(events));
events.sctp_data_io_event = 1;
if (ext_setsockopt(servSock, IPPROTO_SCTP, SCTP_SET_EVENTS, &events,
sizeof(events)) < 0)
DieWithError("setsockopt() failed");
printf("Binding to address... ");
/* Bind to the local address */
if (ext_bind(servSock, (struct sockaddr *) &echoServAddr,
sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");
printf("Address bounded.\n\n");
/* Mark the socket so it will listen for incoming conn. */
printf("Waiting for connection... ");
if (ext_listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");
for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);
/* Wait for a client to connect */
if ((clntSock = ext_accept(servSock, (struct sockaddr *)
&echoClntAddr, &clntLen)) < 0)
DieWithError("accept() failed");
printf("Connected.\n\n");
/* clntSock is connected to a client! */
printf("Handling client %s\n\n",
inet_ntoa(echoClntAddr.sin_addr));
HandleSCTPClient(clntSock);
}
}
**************************************************************************
_________________________________________________________________
Join the worlds largest e-mail service with MSN Hotmail.
http://www.hotmail.com
- [SCTP-Discussion] What does ext_sendmsg() return??, Oon Heok Kee, 09/04/2002
Archive powered by MHonArc 2.6.19+.