Re: Connect call fails.

amitraj.trehan_at_gmail.com
Date: 04/27/05


Date: 27 Apr 2005 07:56:31 -0700

Thanks for your reply. I have the same error even if I have my server
running. Here is the server code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

int main(int argc, char* const argv[])
{
        int socket_fd, client_sock_fd;
        int bytes_read, length;
        struct sockaddr_un name;

        const char* const socket_name = argv[1];
        socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
        name.sun_family = AF_LOCAL;
        strcpy(name.sun_path, socket_name);
        if (bind(socket_fd, (struct sockaddr*)&name, SUN_LEN(&name)) <
0)
                printf("Error\n");
        listen(socket_fd, 5);

        while(1)
        {
        char *msg;
        struct sockaddr_un client;
        socklen_t client_len;
                client_sock_fd = accept (socket_fd, (struct
sockaddr*)&client, &client_len);
                bytes_read = read(client_sock_fd, &length,
sizeof(length));
                if (bytes_read >0)
                printf("%d", bytes_read);
                msg = (char*) malloc (length);

                read(client_sock_fd,msg, length);
                printf("%s", msg);
        }
        close(socket_fd);
        unlink(socket_name);
        return 0;
}
Barry Margolin wrote:
> In article <1114572274.076660.142900@f14g2000cwb.googlegroups.com>,
> amitraj.trehan@gmail.com wrote:
>
> > Hi,
> > I am writing my first socket program and I am getting the error 111
> > (Connection refused). Can someone please tell me what am I doing
wrong
> > in the program.
> >
> > When i run the client program without running the server, the
connect
> > call still fails. I would really appreciate your help.
>
> That's to be expected. You can't connect to a socket if there's no
> server listening for connections.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***



Relevant Pages

  • Problem with writing to sockets - not robust
    ... I want to make it more robust so that it can handle network or server ... It works well when the WAN connections are good ... The socket is opened in a subroutine... ... signla to close the socket on the server side), the client program does ...
    (comp.lang.perl.misc)
  • secure multiplayer using python?
    ... -many client program instances connect to a central file+db server ... -over time intervals the client program checks to see if claimed files ... Currently I am handing out ftp user/pass to each client program (hidden ...
    (comp.lang.python)
  • Re: VB6 compatible Dll created with VC++6 problems using afxsock functions
    ... VB6 compatible DLL that I've made. ... Sockets declarations and recv& sendfunction are defined in ... More over I had already written the Client program in VC++6 using ... send to the server aren't received altough the code is hardly the same ...
    (microsoft.public.win32.programmer.networks)
  • logoff/shutdown philosophy with out-of-process servers
    ... I have an out-of-process server that handles datalogging automatically ... of client program that "cares" about the COM object by holding onto an ... or the user has to keep open some kind of interactive control panel ...
    (microsoft.public.vc.atl)
  • Re: Sending to Socket
    ... "FilexBB" wrote in message ... > I compose a client program that capture System.in and send it to ... > work on server that can't recieve anything. ... Sun Certified Developer for the Java 2 Platform ...
    (comp.lang.java.programmer)