![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 2
| I'm doing: test.h: --------- Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define MYSOCKET "/tmp/mysocket"
#define LISENQ 20
void error(char *msg)
{
perror(msg);
exit(1);
}
---------- Code: #include "test.h"
int main()
{
struct sockaddr serveraddr, clientaddr;
int sd, sd_new;
socklen_t serverlen, clientlen;
char buf[256];
sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd < 0)
error("server socket failure");
bzero(&serveraddr, sizeof(serveraddr));
bzero(&clientaddr, sizeof(clientaddr));
serveraddr.sa_family = AF_UNIX;
strcpy(serveraddr.sa_data, MYSOCKET);
serverlen = sizeof(serveraddr.sa_family) + strlen(serveraddr.sa_data);
clientlen = sizeof(clientaddr.sa_family) + strlen(clientaddr.sa_data);
unlink(MYSOCKET);
if (bind(sd, (struct sockaddr *)&serveraddr, serverlen) < 0)
error("server bind failure");
if (listen(sd, LISENQ) < 0)
error("server listen failure");
for(;;)
{
sd_new = accept(sd, (struct sockaddr *)&clientaddr, &clientlen);
if (sd_new < 0)
error("server accept failure");
if (fork() == 0)
{
read(sd_new, buf, sizeof(buf));
printf("Got msg: %s", buf);
close(sd_new);
exit(0);
}
close (sd);
}
return 0;
}
------- Code: #include "test.h"
int main()
{
struct sockaddr serveraddr;
int sd;
socklen_t serverlen;
char buf[256];
sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd < 0)
error("client socket failure");
serveraddr.sa_family = AF_UNIX;
strcpy(serveraddr.sa_data, MYSOCKET);
serverlen = sizeof(serveraddr.sa_family) + strlen(serveraddr.sa_data);
if (connect(sd, (struct sockaddr *)&serveraddr, serverlen) < 0);
error("client connect failure");
puts("client connected!");
while(1)
{
printf(">> ");
scanf("%s", &buf);
write(sd, buf, sizeof(buf));
}
close(sd);
return 0;
}
- client runs and stops with message : client connect failure: Success and crashes the server with msg: Got msg: �Ҋ�server accept failure: Bad file descriptor need some advice please Last edited by tsiknas; 10-30-2009 at 10:33 AM. |
| tsiknas is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| The client is sending random data to the server. The server is trying to print that random data as if it were a string. That's why it's crashing. EDIT: Oops, for some reason I missed the scanf call. At any rate, you should send strlen(buf)+1 bytes -- not sizeof(buf). Also, get rid of the semicolon in your if (connect... statement.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. Last edited by bithub; 10-30-2009 at 10:49 AM. |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Also, the reason your server crashes the second time around is that you close the listening socket in the for loop. Move that close outside of the for loop.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #4 |
| Registered User Join Date: Oct 2009
Posts: 2
| thanks bithub, everything runs fine now! |
| tsiknas is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Best way to poll sockets? | 39ster | Networking/Device Communication | 3 | 07-22-2008 01:43 PM |
| Problem with POSIX sockets | MCRonald | C Programming | 2 | 07-23-2006 10:41 AM |
| multiple UDP sockets with select() | nkhambal | Networking/Device Communication | 2 | 01-17-2006 07:36 PM |
| Raw Sockets and SP2... | Devil Panther | Networking/Device Communication | 11 | 08-12-2005 04:52 AM |
| Starting window sockets | _Cl0wn_ | Windows Programming | 2 | 01-20-2003 11:49 AM |