Unix Sockets Programming Help????
Nov. 22nd, 2008 10:59 pmAnyone out there reading who knows Unix Socket Programming? I have a project with a hard deadline tomorrow at noon for a class that I'm already in serious danger of failing right now, and I'm stuck on one thing and the TA hasn't answered the e-mail I sent almost 8 hours ago. So I really hope the Livejournal Group Mind can come to my rescue!
Basically, I'm using UDP/datagram protocol and I can send a message and receive it fine, but when I try to send a response back reusing the same sockaddr info I got from the recvfrom in the sendto I get an invalid argument error, and it's driving me nuts. I don't know how well this will show up, but here's the relevant source code:
int numbytes;
struct sockaddr_storage their_addr;
char buf[MAXBUFLEN];
size_t addr_len;
char s[INET6_ADDRSTRLEN];
char msg[] = "";
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(m_listenerSocket, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("listener: got packet from %s\n",
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s));
printf("listener: packet is %d bytes long\n", numbytes);
buf[numbytes] = '\0';
printf("listener: packet contains \"%s\"\n", buf);
(stuff in the middle deleted)
cout << m_Name << " attempting to send message " << msg << endl;
if ((numbytes = sendto(m_listenerSocket, msg, strlen(msg), 0,
(struct sockaddr *)&their_addr, (socklen_t)addr_len)) == -1) {
cout << m_Name << ": sendto error.\n";
exit(1);
}
Update: The TCP version is choking on that too but with a different error message, bad file number. Here's that code:
if ((numbytes = recv(newSocket, buf, MAXBUFLEN-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Target: received '%s'\n",buf);
//close(m_listenerSocket); // child doesn't need the listener
sprintf(msg, "target ACK o");
sleep(1);
if (send(newSocket, msg, strlen(msg), 0) == -1)
perror("send");
UPDATE: I managed to find a /h/a/c/k/ fix for the first problem, but I still don't know what's going on with the TCP one. I'll probably have to just let it go since I'll probably be up all night as it is. *sigh*
Basically, I'm using UDP/datagram protocol and I can send a message and receive it fine, but when I try to send a response back reusing the same sockaddr info I got from the recvfrom in the sendto I get an invalid argument error, and it's driving me nuts. I don't know how well this will show up, but here's the relevant source code:
int numbytes;
struct sockaddr_storage their_addr;
char buf[MAXBUFLEN];
size_t addr_len;
char s[INET6_ADDRSTRLEN];
char msg[] = "";
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(m_listenerSocket, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("listener: got packet from %s\n",
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s));
printf("listener: packet is %d bytes long\n", numbytes);
buf[numbytes] = '\0';
printf("listener: packet contains \"%s\"\n", buf);
(stuff in the middle deleted)
cout << m_Name << " attempting to send message " << msg << endl;
if ((numbytes = sendto(m_listenerSocket, msg, strlen(msg), 0,
(struct sockaddr *)&their_addr, (socklen_t)addr_len)) == -1) {
cout << m_Name << ": sendto error.\n";
exit(1);
}
Update: The TCP version is choking on that too but with a different error message, bad file number. Here's that code:
if ((numbytes = recv(newSocket, buf, MAXBUFLEN-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Target: received '%s'\n",buf);
//close(m_listenerSocket); // child doesn't need the listener
sprintf(msg, "target ACK o");
sleep(1);
if (send(newSocket, msg, strlen(msg), 0) == -1)
perror("send");
UPDATE: I managed to find a /h/a/c/k/ fix for the first problem, but I still don't know what's going on with the TCP one. I'll probably have to just let it go since I'll probably be up all night as it is. *sigh*