PDA

View Full Version : More Issues


kodex
2005.06.09, 03:12 PM
Sorry to keep bothering you guys but im pretty lost without my books and code, and searching google hasnt yeilded and great results yet. Im getting a "exited due to signal 13 (SIGPIPE)" on send();, this is a new one too me since its the send after making a network connection =p.

Anyways code for connection to host

static void SendConnectionRequestToHost(char *ipAddrString)
{
struct sockaddr_in hostAddr;

//creates a connection socket (TCP/IP Streaming)
gConnectionToHost = socket(AF_INET, SOCK_STREAM, nil);

//setup addy info for our host
bzero(&hostAddr, sizeof(hostAddr)); //clear

hostAddr.sin_family = AF_INET; //network type internet
hostAddr.sin_port = htons(5500); //our port number

inet_pton(AF_INET, ipAddrString, &hostAddr.sin_addr);

//issue connection request
if (connect(gConnectionToHost, (struct sockaddr *)&hostAddr, sizeof(hostAddr))< 0)
{
printf("CANNOT CONNECT!\n");

}//failed host is not there

if(WriteNetData(gConnectionToHost, "Hello", 32) <=0)
{
printf("FAILED WRITE");
}
}


Send Data Code

int WriteNetData(int socket, void *buffer, int numBytes)
{
Ptr bytes = buffer; //ptr to buffer
int count, n;

n = count =0;


while(count< numBytes) //loop until we have sent all bytes
{
n = send(socket, bytes, numBytes - count, 0); //send..... CRASHES HERE

if(n>0)
{
count += n;
bytes += n;
}

else
if(n<0) //error
return(-1);
}

return(count);

}


Thanks in advance guys

wadesworld
2005.06.09, 10:55 PM
1) You're not checking to be sure your socket is valid
2) Call select on the socket before calling send to make sure the socket is available for writing.

Wade

kodex
2005.06.10, 03:22 AM
calling this code before sending data, same error same crash. Yes it returns true.


static Boolean IsSocketReadyForWrite(int socket)
{
struct fd_set connectionSet;
struct timeval timeout;
int result;

//set timeout values
timeout.tv_sec =0;
timeout.tv_usec =0;

FD_ZERO(&connectionSet);
FD_SET(socket, &connectionSet);

//call select to see if data is waiting on the socket
result = select(socket+1, nil, &connectionSet, nil, &timeout);

//check results
if (result < 0 )
return (false);
if (result>0)
{
if(FD_ISSET(socket, &connectionSet))
return(true);
else
return(false);
}

return(false);

}

Steven
2005.06.10, 10:53 AM
Are you sure that socket() did not fail? Are you checking the result and calling perror() if necessary?

kodex
2005.06.10, 04:01 PM
Socket returns a 6, no other errors besides failure to connect on connect. which i think is related. Anyone want to take a look at my whole source i could email it to you pretty small. Would be very grateful :D

wadesworld
2005.06.11, 11:35 AM
Well yeah - if it did not connect, you can't send.

Sure, send me your code and I'll take a look. wade at dogwatchsw.com

Wade