Home > Archive > Unix Programming > May 2006 > Problem in writing to a socket
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem in writing to a socket
|
|
| shibdas@gmail.com 2006-05-28, 4:02 am |
| Hi,
I am doing a network simulation where each node is represented by a
process and I have a central moderator which controls the flow of the
message from one node(process) to another node(process). For this the
central moderator establishes connection with the node processes and
keeps the corresponding file descriptors on an array. The setup is
working fine when there is round about 20 nodes but when the number of
nodes increases to 100 or so the moderator is not able to write data to
the fds present in the array of acoket fds. The "write" function
returns the number of bytes that should be written but at the node I am
not getting any data from the "read call". Also, before the write
statement system prints some strange charaters... which may mean there
is some error..
Please help
thanks
| |
| Maxim Yegorushkin 2006-05-28, 4:02 am |
|
shibdas@gmail.com wrote:
> Hi,
> I am doing a network simulation where each node is represented by a
> process and I have a central moderator which controls the flow of the
> message from one node(process) to another node(process). For this the
> central moderator establishes connection with the node processes and
> keeps the corresponding file descriptors on an array. The setup is
> working fine when there is round about 20 nodes but when the number of
> nodes increases to 100 or so the moderator is not able to write data to
> the fds present in the array of acoket fds. The "write" function
> returns the number of bytes that should be written but at the node I am
> not getting any data from the "read call". Also, before the write
> statement system prints some strange charaters... which may mean there
> is some error..
Yes, there must be an error. Please find it in your source code.
| |
|
| On 27 May 2006 23:18:32 -0700
shibdas@gmail.com wrote:
> I am doing a network simulation where each node is represented by
> a process and I have a central moderator which controls the flow of
> the message from one node(process) to another node(process). For this
> the central moderator establishes connection with the node processes
> and keeps the corresponding file descriptors on an array. The setup is
> working fine when there is round about 20 nodes but when the number of
> nodes increases to 100 or so the moderator is not able to write data
> to the fds present in the array of acoket fds. The "write" function
> returns the number of bytes that should be written but at the node I
> am not getting any data from the "read call". Also, before the write
> statement system prints some strange charaters... which may mean there
> is some error..
ulimit might be helpful in changing the number of open fd. try running
it as root, which should have a higher (unlimited) fd count.
--
Regards, Ed :: http://www.openbsdhacker.com
proud linux person
:%s/Open Source/Free Software/g :: Free DNS available
| |
| Maxim Yegorushkin 2006-05-28, 8:04 am |
|
ed wrote:
> On 27 May 2006 23:18:32 -0700
> shibdas@gmail.com wrote:
>
>
> ulimit might be helpful in changing the number of open fd. try running
> it as root, which should have a higher (unlimited) fd count.
The simptom of this problem would be the inability to *open* more than
1024 socket/fd. The opened fd's would operate normally.
| |
| shibdas@gmail.com 2006-05-28, 8:04 am |
| I have run this code as root and also the number of open fds are little
more than 100
The moderatoir code is as follows:-
" server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(MOD_SOCKET);
result = bind(server_sockfd, (struct sockaddr *)&server_address,
sizeof(server_address));
listen(server_sockfd, 5);
client_len = sizeof(client_address);
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);
fprintf(stderr, "\n Waiting for the client to connect...");
while(1)
{
testfds = readfds;
tmout.tv_sec = 100;
tmout.tv_usec = 0;
result = select(FD_SETSIZE, &testfds, (fd_set *)0,
(fd_set *)0, &tmout);
//printf("Select returned");
if(result < 0)
{
printf("\n Unexpected Behaviour Shutting down the server");
return -1;
}
if(result == 0) // No response from the clients, Time to finish the
simulation
{
int tot_aff;
//fprintf(stderr, "\n Finishing the simulation");
// Write the results
// Check whether there is any request for new connection
for(fd = 0; fd < FD_SETSIZE; fd++)
{
if(FD_ISSET(fd, &testfds))
{
if(fd == server_sockfd) // a new connection has arrived, accept it
{
//fprintf(stderr, "\n Accepting new Connection... ");
client_sockfd = accept(server_sockfd, (struct sockaddr
*)&client_address,
&client_len);
FD_SET(client_sockfd, &readfds);
}
else // This is a data sent by the client, handle it
{
ioctl(fd, FIONREAD, &nread);
if(nread == 0)
{
// Client is closing the connection
close(fd);
FD_CLR(fd, &readfds);
}
else // client has sent a data packet to the moderator
{
// process the
message
for(i = 0; i < nPoints; i++)
{
if(distance(src, i) <= (rad * rad) && src != i)
{
fprintf(stderr, "\n Tx to = %d", i);
cntRx++;
// problem occurs here...
// output is like tx to 23<garbage>
reswrite = write(sockfds[i], &msg, sizeof(msg));
printf("\n Result %d", reswrite);
}
}
"
The client code is as follows:-
"
sockfd = socket(AF_INET, SOCK_STREAM, 0);
hostinfo = gethostbyname("127.0.0.1");
address.sin_family = AF_INET;
address.sin_port = htons(MOD_SOCKET);
address.sin_addr.s_addr = inet_addr("127.0.0.1");
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("could not start client process");
return -1;
}
// send the id to the server
msg.type = MSG_CTL_ID;
msg.content = my_id;
write(sockfd, &msg, sizeof(msg));
// Start sending the packet if it is the source
if(my_id == src_id)
{
prev_msg_id = 1;
msg.type = MSG_DATA;
msg.content = my_id;
msg.opt_content1 = dest_id;
msg.msg_id = prev_msg_id;
// write the message to the socket
write(sockfd, &msg, sizeof(msg));
}
// We are done!!!, listen for any packet from the server
while(1)
{
result = read(sockfd, &msg, sizeof(msg));
if(result == -1)
{
// server must have been stopped
close(sockfd);
return 0;
}
else if(result == 0)
{
close(sockfd);
return 0;
}
else
{
// It is not receving data from the moderator
fprintf(stderr, "\n Received Data id = %d", my_id);
"
Can you please point out the problem with this code...
Thanks
| |
| Barry Margolin 2006-05-28, 8:04 am |
| In article <1148797112.106203.6760@i39g2000cwa.googlegroups.com>,
shibdas@gmail.com wrote:
> Hi,
> I am doing a network simulation where each node is represented by a
> process and I have a central moderator which controls the flow of the
> message from one node(process) to another node(process). For this the
> central moderator establishes connection with the node processes and
> keeps the corresponding file descriptors on an array. The setup is
> working fine when there is round about 20 nodes but when the number of
> nodes increases to 100 or so the moderator is not able to write data to
> the fds present in the array of acoket fds. The "write" function
> returns the number of bytes that should be written but at the node I am
> not getting any data from the "read call". Also, before the write
> statement system prints some strange charaters... which may mean there
> is some error..
Sounds like somehow the fd for the socket is getting set to the fd's for
stdout or stderr, so it's writing to the terminal instead of the socket.
It's time to start up your debugger to see why this is happening.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Rainer Temme 2006-05-28, 7:06 pm |
| - Your code doesn't compile ... please support us with
compilable code.
- I don't see the point where the server calls read() to read
from the clients.
- Without the declaration of the variables it is quite
often impossible to find errors.
- Add this line to the server to the startup of your server ...
printf("sizeof fd_set: %d\n",sizeof(readfds));
If the output is 16, then on your system the fd_set only holds space for
128 fds. (In this case you could use poll() instead of select()).
Rainer
| |
|
| shibdas@gmail.com wrote:
> I have run this code as root and also the number of open fds are little
> more than 100
>
> if(fd == server_sockfd) // a new connection has arrived, accept it
> {
> //fprintf(stderr, "\n Accepting new Connection... ");
> client_sockfd = accept(server_sockfd, (struct sockaddr
> *)&client_address,
> &client_len);
> FD_SET(client_sockfd, &readfds);
You need to do some bookkeeping in the sockfds[] array here.
> }
> else // This is a data sent by the client, handle it
> {
> ioctl(fd, FIONREAD, &nread);
Strange way to test readability, IMHO.
read() returns zero on eof. fd is
reported as readable by select(), so read should not block.
Setting fd's to non-blocking is also advised.
>
> if(nread == 0)
> {
> // Client is closing the connection
> close(fd);
> FD_CLR(fd, &readfds);
> }
You need to do some bookkeeping in the sockfds[] array here.
> else // client has sent a data packet to the moderator
> {
> // process the
> message
> for(i = 0; i < nPoints; i++)
> {
> if(distance(src, i) <= (rad * rad) && src != i)
> {
>
> fprintf(stderr, "\n Tx to = %d", i);
> cntRx++;
>
> // problem occurs here...
>
> // output is like tx to 23<garbage>
My guess is that you write() msg to stdout or stderr.
> reswrite = write(sockfds[i], &msg, sizeof(msg));
You assume all entries in the sockfds[] array are valid file
descriptors. They might not be.
HTH,
AvK
| |
| shibdas@gmail.com 2006-05-28, 7:06 pm |
| I thought the whole code is too big to post here. All the entries of
the sockfds array are valid as I maintaing the bookkeeping it in
another way. As I am quite new to this newsgroup can anyone please tell
me how to attach any file so that I can send the two programs
| |
|
| shibdas@gmail.com wrote:
> I thought the whole code is too big to post here. All the entries of
> the sockfds array are valid as I maintaing the bookkeeping it in
> another way. As I am quite new to this newsgroup can anyone please tell
> me how to attach any file so that I can send the two programs
You could trim it down to a usable (and compilable!) size, which still
illustrates your problems. In most cases this will help you find the
bugs yourself.
It could also help you to decouple your network-skeleton from your
application logic.
HTH,
AvK
| |
| shibdas 2006-05-28, 7:06 pm |
| thanks for all the advice, I have sorted out the problem. It was
happening due to some synchronization issue. Now it is running and
giving the desired output albeit with a small problem. I have only a
fprintf(stderr, result) statement at the end but it is outputing some
garbage characters on my terminal emulator prior to printing that and
it sets the terminal characteristics so that all the characters in the
terminal including the prompt also becomes garbage..
| |
| Barry Margolin 2006-05-28, 7:06 pm |
| In article <1148834502.152683.102510@i40g2000cwc.googlegroups.com>,
"shibdas" <shibdas@gmail.com> wrote:
> thanks for all the advice, I have sorted out the problem. It was
> happening due to some synchronization issue. Now it is running and
> giving the desired output albeit with a small problem. I have only a
> fprintf(stderr, result) statement at the end but it is outputing some
> garbage characters on my terminal emulator prior to printing that and
> it sets the terminal characteristics so that all the characters in the
> terminal including the prompt also becomes garbage..
Shouldn't that statement be something like fprintf(stderr, "%s\n",
result)?
And are you sure result is a null-terminated string?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| shibdas 2006-05-30, 7:07 pm |
| I mean the last statement is something like
fprint(stderr, "%d %d %d", number_tx, number_rx, number_affected);
so it is indeed null terminated... My presumption is that it is writing
something to /dev/tty file but i am not able to figure out the reason
why it will do so when I am writing to socket which is having some
other file descriptor..
| |
| Barry Margolin 2006-05-31, 4:05 am |
| In article <1149018085.432794.36760@r44g2000cwb.googlegroups.com>,
"shibdas" <shibdas@gmail.com> wrote:
> I mean the last statement is something like
> fprint(stderr, "%d %d %d", number_tx, number_rx, number_affected);
> so it is indeed null terminated... My presumption is that it is writing
> something to /dev/tty file but i am not able to figure out the reason
> why it will do so when I am writing to socket which is having some
> other file descriptor..
Set a breakpoint before the call to send() or write() and check the fd
it's about to write to.
I really don't know how you expect us to tell you what's wrong with your
program. We can't see it, you can.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| shibdas 2006-05-31, 7:09 pm |
| I'll do the neccessary debugging and will tell you what is the problem
if I am able to figure it out
Barry Margolin wrote:
> In article <1149018085.432794.36760@r44g2000cwb.googlegroups.com>,
> "shibdas" <shibdas@gmail.com> wrote:
>
>
> Set a breakpoint before the call to send() or write() and check the fd
> it's about to write to.
>
> I really don't know how you expect us to tell you what's wrong with your
> program. We can't see it, you can.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
|
|
|