Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Connect call fails.
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.

Here is the program.

#include <stdio.h>
#include <errno.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;
struct sockaddr_un name;

const char* const client_name = argv[1];
// String to send
const char* const sendstr = argv[2];
int length = strlen(sendstr) + 1;

// Initialization of socket
socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
name.sun_family = AF_LOCAL;
strcpy(name.sun_path, client_name);

// Connect
if (connect(socket_fd, &name, SUN_LEN (&name)) <0 );
printf("%d\n",errno);

// write the length of the msg first
printf("%d\n", length);

write(socket_fd, &(length),  sizeof(length));
write(socket_fd, sendstr, length);
close(socket_fd);
return 0;
}


Report this thread to moderator Post Follow-up to this message
Old Post
amitraj.trehan@gmail.com
04-27-05 08:58 AM


Re: Connect call fails.
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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
04-27-05 08:58 AM


Re: Connect call fails.
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:
> 
wrong 
connect 
>
> 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 ***


Report this thread to moderator Post Follow-up to this message
Old Post
amitraj.trehan@gmail.com
04-27-05 08:59 PM


Re: Connect call fails.
On Wed, 27 Apr 2005 07:56:31 -0700, amitraj.trehan wrote:

> 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");

In both applications you are passing SUN_LEN() not SUN_LEN() + 1 ... this
might be a problem (SUN_LEN() works like strlen(), so you need to +1 for
the NIL).

Apart from that, you aren't checking error returns in all the right
places  (hint ... #include <err.h>, then you can just do...

err(EXIT_FAILURE, "bind");

...which is almost as simple as leaving it out.

>         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);

client_len needs to be == sizeof(client) as you call this function.

>                 bytes_read = read(client_sock_fd, &length,
> sizeof(length));
>                 if (bytes_read >0)
>                 printf("%d", bytes_read);

You don't check for what happens if bytes_read != sizeof(length).

>                 msg = (char*) malloc (length);
>
>                 read(client_sock_fd,msg, length);
>                 printf("%s", msg);

You are leaking memory here.

>         }
>         close(socket_fd);
>         unlink(socket_name);
>         return 0;
> }

Note that you may wish to unlink() before the bind call, so that you
don't need to manually cleanup on failure shutdowns.

You can also look at:

http://www.and.org/vstr/examples/evnt.c

...which has working code (evnt_make_bind_local() and evnt_make_con_local()
).

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/


Report this thread to moderator Post Follow-up to this message
Old Post
James Antill
04-28-05 01:58 AM


Re: Connect call fails.
Thanks I was able to solve the problem and I am atleast seeing what the
client sends. Another problem thats happening is the the server, keeps
on reading the same thing again and again and the while(1) never stops.
Any ideas why this might be happening?


Report this thread to moderator Post Follow-up to this message
Old Post
amitraj.trehan@gmail.com
04-28-05 01:58 AM


Re: Connect call fails.
<amitraj.trehan@gmail.com> wrote in message
news:1114630405.097129.184110@g14g2000cwa.googlegroups.com...

> Thanks I was able to solve the problem and I am atleast seeing what the
> client sends. Another problem thats happening is the the server, keeps
> on reading the same thing again and again and the while(1) never stops.
> Any ideas why this might be happening?

Post your current code. The bugs that would typically cause this are
exactly the ones that have already been pointed out. The most common one is
not properly using the return value from 'read'. That tells you how many
bytes you read, and you *must* use that to determine how many bytes you now
have in your buffer.

DS



Report this thread to moderator Post Follow-up to this message
Old Post
David Schwartz
04-28-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.