C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-30-2009, 10:25 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 2
Question posix sockets in c

I'm trying to setup a connection between a client and a server using sockets.
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);
}
server.c:
----------
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;
}
client:
-------
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;
}
- server runs and waits
- 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   Reply With Quote
Old 10-30-2009, 10:46 AM   #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   Reply With Quote
Old 10-30-2009, 10:50 AM   #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   Reply With Quote
Old 10-30-2009, 11:13 AM   #4
Registered User
 
Join Date: Oct 2009
Posts: 2
thanks bithub, everything runs fine now!
tsiknas is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22