Thread: socket programming in linux

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    socket programming in linux

    The code is pretty sloppy, but I'm trying to figure out socket programming in unix and have a problem. The client passes a filename to a "fileserver", fileserver returns a code if the file is there or not, if there is, it's supposed to copy the contents of the file on the server to a new file on the client. Right now I'm just trying to print the contents of the file on the client screen, but it doesn't appear to be working unfortunately.

    Server
    Code:
    /*
    usage: ./fileserver port-number
    
    */
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <arpa/inet.h>
    
    int main(int argc, char *argv[])
    {
        if(--argc != 1)
        {
            fprintf(stderr,"usage: ./fileserver port-number\n");
            return 1;
        }
        
    	int server_sock_fd = socket(PF_INET, SOCK_STREAM, 0);
    	if (server_sock_fd == -1)
    	{
    		perror("Socket");
    		exit(1);
    	}
    	
    	struct sockaddr_in server_addr;
    	memset(&server_addr, 0, sizeof(struct sockaddr_in));
    	server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(atoi(argv[1]));
        server_addr.sin_addr.s_addr = inet_addr("192.168.1.103");
    	
        int b_err = bind(server_sock_fd, 
                        (struct sockaddr *) &server_addr, 
                         sizeof(struct sockaddr_in));
    	if(b_err == -1)
    	{
    		perror("bind failed");
    		exit(1);
    	}
    	
    	int list_err = listen(server_sock_fd, 5);
    	if (list_err == -1)
    	{
    		perror("listen");
    		exit(1);
    	}
    
    	while(1)
    	{
    		struct sockaddr_in client_addr;
    		socklen_t client_addr_len = sizeof(struct sockaddr_in);
            int client_sock_fd = accept(server_sock_fd, 
                                       (struct sockaddr *) &client_addr, 
                                        &client_addr_len);
    		if(client_sock_fd== -1)
    		{
    			perror("accept");
    			exit(1);
    		}
      
            char filename[100];
            int n;
            if(n = read(client_sock_fd, filename, 100) == -1)
            {
                perror("client socket read");
                return 2;
            }
            
            /* shutdown socket input 
            shutdown(client_sock_fd, 0); */
            
            int fd;
            char *buffer;
            
            /* check if the filename client passed exists */
            if((fd = open(filename, O_RDONLY)) == -1)
            {
                if(write(client_sock_fd, "0", 1) == -1)
                {
                    perror("client write");
                    exit(1);
                }
                fprintf(stderr,"The file count not be found\n");
            }
            else
            {
                if(write(client_sock_fd, "1", 1) == -1)
                {
                    perror("client write");
                    exit(1);
                }
                int n;
                char buf[10];
                while(n = read(fd, buf, 10))
                {
                    if(n == 0) break;
                    if(n == -1)
                    {
                        perror("read");
                        exit(1);
                    }
                    if(write(client_sock_fd, buf, n) == -1)
                    {
                        perror("write");
                        exit(1);
                    }
                }
                close(fd);
            }
            close(client_sock_fd);
            break;
    	}
    	
    	return 0;
    }
    Client
    Code:
    /*
    usage: ./client file server-address port-number
    */
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(int argc, char **argv){
    
        if(--argc != 3)
        {
            fprintf(stderr, "usage: ./client file server-address port-number\n");
            return 1;
        }
    
        int sock_fd = socket(PF_INET, SOCK_STREAM, 0);
        if(sock_fd == - 1)
        {
    	   perror("socket");
    	   exit(1);
        }
        struct sockaddr_in server_addr;
        memset(&server_addr, 0, sizeof(struct sockaddr_in));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(atoi(argv[3]));
        server_addr.sin_addr.s_addr = inet_addr(argv[2]);
    
        int conn_err = connect(sock_fd,(struct sockaddr *) &server_addr, sizeof(server_addr));
        if(conn_err == -1)
        {
    	   perror("Connect");
    	   exit(1);
        }
        
        /* send file name to server */
        if(write(sock_fd,argv[1],strlen(argv[1])) == -1)
        {
            perror("Write to socket");
            exit(1);
        }
    
        /* shutdown socket output 
        shutdown(sock_fd, 1);*/
        
        /* read server response */
        int n;
        char buffer[10];
        while(n = read(sock_fd, buffer, 10))
        {
            if(n == 0) break;
            if(n == -1)
            {
                perror("read from socket");
                exit(1);
            }
        }
    
        /* file not found */
        if(buffer[0] == '0')
        {
            fprintf(stderr, "Server can't find the file\n");
        }
        /* file found */
        else
        {
            int fd;
            if((fd = open("test", O_WRONLY | O_CREAT, 0777)) == -1)
            {
                perror("open");
                exit(1);
            }    
            
            /* put contents of server file into local copy */
            char buf[10];
            while(n = read(sock_fd, buf, 10))
            {
                if(n == 0) break;
                if(n == -1)
                {
                    perror("read from socket");
                    exit(1);
                }
                if(write(2,buf,n) == -1)
                {
                    perror("write");
                    exit(1);
                }
            }
            close(fd);
        }
        close(sock_fd);
       
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    So what does and/or doesn't happen? How are you trying to run them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 08:56 AM
  2. socket programming in c or c++ in linux...
    By pk20 in forum Linux Programming
    Replies: 5
    Last Post: 02-12-2009, 01:54 AM
  3. linux, doubles, and unions
    By paraglidersd in forum Linux Programming
    Replies: 14
    Last Post: 11-19-2008, 11:41 AM
  4. pthread and socket porting from Linux to Windows
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 07-18-2008, 06:57 AM