Thread: posix sockets in c

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.
    Last edited by bithub; 10-30-2009 at 10:49 AM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    thanks bithub, everything runs fine now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Problem with POSIX sockets
    By MCRonald in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 10:41 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM