 |
|
 |
|
04-27-05 07:48 AM
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;
}
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
04-27-05 07:48 AM
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 ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
04-27-05 10:58 PM
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[vbcol=seagreen]
connect[vbcol=seagreen]
>
> 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 ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
04-27-05 10:58 PM
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/
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
04-27-05 10:58 PM
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 a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
04-27-05 10:58 PM
<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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 02:12 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|