[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [openss7] Getting through



Brian,
	Thanks for the advice.  I took much of it, including the implicit
adivice of changing over my program to use readmsg and sendmsg.  I am
still having problems with recvmsg retreiving data from the buffer which
was already read.  
	Example.  I am including 2 programs.  The first program (read.c) is
going to wait for a connection from another program (write.c) and
display all the data that write sends to it.  The second program
(write.c) will startup, connect to read, and sleep for 2 seconds, then
and send 1 packet to read.  It will then sleep for 2 seconds, and write
another packet.  On and on until write reaches the count of 10.  
	The observed behavior is that read starts up, waits for a connection. 
After write connects and goes to sleep(for the initial 2 seconds), read
blocks on recvmsg waiting for data(Everything is ok so far).  Write then
wakes up and sends one packet and goes to sleep for another 2 seconds. 
At this time, read will continually call recvmsg and get the same data
that was originally gotten from write with the first packet 100
times(the for loop in read).  Read then blows through the rest of the
code and shuts down.  Soon after, write wakes up, and tries to send
another packet, it immediately get a broken pipe error(rightly so being
that read has closed the connection).  
	So my problem is, recvmsg keeps getting the same data from the buffer. 
I made sure my buffer was zeroed out after I print out it's contents,
and try to read again.  
	I attached the two programs, and the output from them both.  

Thanks,
Chuck
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>

int main(void)
{
	int listen_sock;
	int connect_sock;
	int ct = 0;
	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	int sockaddr_len = 0;
	char buff[1024];
	int on = 1;
	int buffsize = 0;
	int bytes_to_read;
	int bytes_total;
	int bytes_read_this;
	struct msghdr message;
	struct iovec  iov;


	memset(&message, 0, sizeof(struct msghdr));
	memset(&iov, 0, sizeof(struct iovec));
	memset(&server_addr, 0, sizeof(struct sockaddr_in));
	memset(&client_addr, 0, sizeof(struct sockaddr_in));
	memset(buff, 0, 1024);

	/** Create a socket **/
	if((listen_sock = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0)
	{
		perror("Creating Socket");
		exit(1);
	}
	fprintf(stderr, "Created socket with fd of %d\n", listen_sock);

	/** Fill address in **/
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(33822);
	server_addr.sin_addr.s_addr = inet_addr("192.168.1.33");

	iov.iov_base = buff;
	iov.iov_len = 1024;
	message.msg_iov = &iov;
	message.msg_iovlen = 1;
	message.msg_name = &client_addr;
	/** Bind address to the socket **/
	if(bind(listen_sock, (struct sockaddr* ) &server_addr, sizeof(struct sockaddr_in)) < 0)
	{
		perror("Binding to socket");
		exit(2);
	}

	/** Now listen and accept a connection **/
	if(listen(listen_sock, 1) < 0)
	{
		perror("Listening");
		exit(3);
	}
	fprintf(stderr, "After listen\n");

	if((connect_sock = accept(listen_sock, (struct sockaddr* ) &client_addr, &sockaddr_len)) < 0)
	{
		perror("Accepting\n");
		exit(4);
	}

	/** Now read from the socket **/
	for(ct = 0; ct < 100; ct++)
	{
		
		if(recvmsg(connect_sock, &message, MSG_WAITALL | MSG_NOSIGNAL) < 0)
		{
			perror("Reading");
			exit(1);
		}

		fprintf(stderr, "Client Addres is %s\n", message.msg_name);
		fprintf(stderr, "%d) Data received is  %s\n", ct, message.msg_iov->iov_base);

		ioctl(connect_sock, FIONREAD, &buffsize);
		fprintf(stderr, "Data still in recv buffer is %d\n", buffsize);
		memset(buff, 0, 1024);
		fprintf(stderr, "buffsize %d\n", buffsize);
	}

	/** Close the fd **/
	close(listen_sock);
	close(connect_sock);
	exit(0);
}
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/uio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/uio.h>

int main(void)
{
	int listen_sock;
	int ct = 0;
	struct sockaddr_in server_addr;
	struct sockaddr_in my_addr;
	char buff[12] = "Hello World";
	struct msghdr message;
	struct iovec  iov;

	iov.iov_base = buff;
	iov.iov_len = 12;

	memset(&my_addr, 0, sizeof(struct sockaddr_in));
	memset(&server_addr, 0, sizeof(struct sockaddr_in));
	signal(SIGPIPE, SIG_IGN);

	/** Create a socket **/
	if((listen_sock = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0)
	{
		perror("Creating Socket");
		exit(1);
	}
	fprintf(stderr, "Created socket with fd of %d\n", listen_sock);

	/** Fill in addr **/
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(33822);
	server_addr.sin_addr.s_addr = inet_addr("192.168.1.33");
	
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(1024);
	my_addr.sin_addr.s_addr = inet_addr("192.168.1.34");

	message.msg_name = &server_addr;
	message.msg_namelen = sizeof(struct sockaddr_in);
	message.msg_iov = &iov;
	message.msg_iovlen = 1;
	message.msg_control = NULL;
	message.msg_controllen = 0;

	if(bind(listen_sock, (struct sockaddr* ) &my_addr, sizeof(struct sockaddr_in)) < 0)
	{
		perror("Binding");
		exit(1);
	}
	fprintf(stderr, "Bound to my_addr to socket %d\n", listen_sock);

	/** Connect to the Server **/
	if(connect(listen_sock, (struct sockaddr* ) &server_addr, sizeof(struct sockaddr_in)) < 0)
	{
		perror("Connecting");
		exit(2);
	}
	fprintf(stderr, "Connected to socket %d\n", listen_sock);

	/** Write to the socket **/
	for(ct = 0; ct < 10; ct++)
	{
		/*if(send(listen_sock, buff, sizeof(buff), MSG_NOSIGNAL) < 0)*/
		if(sendmsg(listen_sock, &message, MSG_NOSIGNAL) < 0)
		{
			perror("Problem Sending");
			exit(1);
		}
		fprintf(stderr, "Sent %s to bert for the %d time\n", buff, ct);
		sleep(2);
	}

	/** close **/
	close(listen_sock);
	exit(0);
}


--- Write Begins ---
Created socket with fd of 3
Bound to my_addr to socket 3
Connected to socket 3
Sent Hello World to bert for the 0 time
Problem Sending: Broken pipe

--- Read Begins ---
Created socket with fd of 3
After listen
Client Addres is 
0) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
1) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
2) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
3) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
4) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
5) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
6) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
7) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
8) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
9) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
10) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
11) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
12) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
13) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
14) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
15) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
16) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
17) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
18) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
19) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
20) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
21) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
22) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
23) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
24) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
25) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
26) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
27) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
28) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
29) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
30) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
31) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
32) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
33) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
34) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
35) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
36) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
37) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
38) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
39) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
40) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
41) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
42) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
43) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
44) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
45) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
46) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
47) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
48) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
49) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
50) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
51) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
52) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
53) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
54) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
55) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
56) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
57) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
58) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
59) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
60) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
61) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
62) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
63) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
64) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
65) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
66) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
67) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
68) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
69) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
70) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
71) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
72) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
73) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
74) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
75) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
76) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
77) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
78) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
79) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
80) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
81) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
82) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
83) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
84) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
85) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
86) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
87) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
88) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
89) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
90) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
91) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
92) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
93) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
94) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
95) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
96) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
97) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
98) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0
Client Addres is 
99) Data received is  Hello World
Data still in recv buffer is 0
buffsize 0