LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2005, 11:03 AM   #1
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
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.
 
Old 05-12-2005, 11:20 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You're attempting to send on sockfd, which is your LISTENING socket. new_fd is the incoming connection: try to send to it.
 
Old 05-12-2005, 11:22 AM   #3
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Original Poster
Rep: Reputation: 32
D'oh! I was right, I was doing something stupid.

It's working perfectly now - thanks!
 
Old 05-12-2005, 11:43 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
No problem. Keeping sockets straight are always fun, right?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM
is a socket already opened? fcntl(); Thinking Programming 1 09-19-2005 10:47 AM
using a socket decriptor opened by anoter process sudheernair Programming 6 04-19-2004 11:20 AM
using a socket descriptor opened by another process sudheernair Programming 0 04-16-2004 07:29 AM
Receiving all data from socket Ohmu Programming 6 03-08-2004 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration