Gettting ENOTCONN when sending data, but the socket was opened.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.