send() won't send

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

send() won't send

Postby roadkillbunny » Sun Dec 14, 2003 1:53 pm

Hi

i am writting a console client to control AMSN remotely, but the send funcion won't send any data.
This is the function I use to send data.
Code: Select all
put(string msg) {
        int datasend;
        int len=msg.length();
        const char *data=msg.c_str();
        if(datasend=send(sock,data,len,0)==-1) {
                perror("send");
                exit(1);
        }
        cout << "Sent " << datasend << "/" << len << " bytes of data..." << endl;
}


The socket is right, I see that I connected in AMSN. But the output is:
The Sent 0/21 bytes of data...
I am sending: "roadkillbunny@msn.com"
When I quit my program with crtl-C (it just hands there after it says it sent it), AMSN recieves the string.

Cheers
--RoadkillBunny
PS: Not sure if this is a c++ or tcl/tk related problem.
roadkillbunny
 
Posts: 3
Joined: Sun Nov 16, 2003 11:20 pm

Postby Alvaro » Sun Dec 14, 2003 8:10 pm

Let me start by saying that I don't know what AMSN is and I am not really a network programming expert, so I may not be able to help much.

You have an operator precedence problem in this line:
if(datasend=send(sock,data,len,0)==-1) {

datasend will get only a copy of the boolean indicating an error. You probably want to store the result of send(). You can either use parenthesis around the assignment, or split that line into two, which avoids all the confusion.

Code: Select all
    ...
    datasend=send(sock,data,len,0);
    if(datasend==-1){
        ...


Anyway, can you post the code where the socket is created and its attributes are set? In particular, I would like to know if your socket is in blocking or non-blocking mode.

I use select() with non-blocking sockets and that has always worked fine for me. It is hard to get these things working at first, though.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby devil_slayer » Mon Dec 15, 2003 5:23 am

If you think you have an error, you should check what that error is.
See http://www.rt.com/man/send.2.html for a list of errors:
ERRORS
These are some standard errors generated by the socket
layer. Additional errors may be generated and returned
from the underlying protocol modules; see their respective
manual pages.

EBADF An invalid descriptor was specified.

ENOTSOCK
The argument s is not a socket.

EFAULT An invalid user space address was specified for a
parameter.

EMSGSIZE
The socket requires that message be sent atomi-
cally, and the size of the message to be sent made
this impossible.

EAGAIN The socket is marked non-blocking and the
requested operation would block.

ENOBUFS The system was unable to allocate an internal mem-
ory block. The operation may succeed when buffers
become available.

EINTR A signal occurred.

ENOMEM No memory available.

EINVAL Invalid argument passed.

EPIPE The local end has been shut down on a connection
oriented socket. In this case the process will
also receive a SIGPIPE unless MSG_NOSIGNAL is set.

Include the <errno.h> header and check the errno variable to see what's wrong. For help on checking errors in your programs see Advanced Linux Programming chapter 2 page 17 here!

Cheers!
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby Filter » Mon Dec 15, 2003 6:51 am

The "error" you got is due to operator precedence. Add a pair of parantheses in your if and you'll have the result you want.
--
Filter www.ampoff.org
Filter
 


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 0 guests