trevelluk |
05-12-2005 10:03 AM |
Gettting ENOTCONN when sending data, but the socket was opened.
Hi everyone, I hope someone can help with this. I'm writing a simple client-server application at the moment, with the server end written in C. For some reason though, I can't seem to send () any data from this program to the client - send () is returning -1 and setting errno to ENOTCONN. I've probably just done something stupid while setting up the connection, but I can't see where. Any advice will be much appreciated. The code is as follows (yes, I know it doesn't do anything useful, but I want to get a basic system working before I add in more complexity).
Code:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#define MYPORT 5897
#define BACKLOG 10
#define DATALEN 13
int main (void) {
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
char data[DATALEN];
int bytes_sent;
extern int errno;
/* Set up the IEC program structures */
iec_scan_init (RL_COLD_START);
/* Set up the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, BACKLOG);
sin_size = sizeof(struct sockaddr_in);
/* And now we wait for a connection */
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
printf ("Couldn't accept connection\n");
}
printf ("Client connected\n");
/* Send the results to the client */
sprintf (data, "%d,%d,%d,%d,%d,%d", 1, 2, 3, 4, 5, 6);
printf ("Sending results\n");
bytes_sent = send (sockfd, data, strlen (data), 0);
if (bytes_sent == -1) {
switch (errno) {
case EBADF: printf ("Invalid socket file descriptor\n"); break;
case EINTR: printf ("Operation interrupted\n"); break;
case ENOTSOCK: printf ("Descriptor is not a socket\n"); break;
case EMSGSIZE: printf ("Message is too large\n"); break;
case EWOULDBLOCK: printf ("Operation would block\n"); break;
case ENOBUFS: printf ("Not enough internal buffer space\n"); break;
case ENOTCONN: printf ("Socket is not connected\n"); break;
case EPIPE: printf ("Connection broken\n"); break;
default: printf ("Unknown error\n");
}
}
while (1) {
/* Send the results to the client */
sprintf (data, "%d,%d,%d,%d,%d,%d", 1, 2, 3, 4, 5, 6);
printf ("Sending results\n");
bytes_sent = send (sockfd, data, strlen (data), 0);
if (bytes_sent == -1) {
switch (errno) {
case EBADF: printf ("Invalid socket file descriptor\n"); break;
case EINTR: printf ("Operation interrupted\n"); break;
case ENOTSOCK: printf ("Descriptor is not a socket\n"); break;
case EMSGSIZE: printf ("Message is too large\n"); break;
case EWOULDBLOCK: printf ("Operation would block\n"); break;
case ENOBUFS: printf ("Not enough internal buffer space\n"); break;
case ENOTCONN: printf ("Socket is not connected\n"); break;
case EPIPE: printf ("Connection broken\n"); break;
default: printf ("Unknown error\n");
}
}
sleep (1);
}
}
When I run this, the output I get is:
Client connected
Sending results
Socket is not connected
Sending results
Socket is not connected
Sending results
Socket is not connected
I don't know if it's relevant at all, but I'm using Cygwin to run this code, and the client program is written in (shudder) VB.
Any advice will be much appreciated.
|