Thread: SSH Client Socket

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

    SSH Client Socket

    Here we use two programs Server & Client using socket. The server program will execute on Linux machine & the client program will run on windows machine throw SSH Client connecting with linux machine. When the both programs are running if we write something on server it will be seen in client as well as if in client we write something it will be seen in server.

    The problem is it didn't work this way... the code of both programs are given below... please help me it's very urgent, i'm running out of my submission deadline... please mark the changes that are required.

    the file server1.c contains the following:
    Code:
    /*  Make the necessary includes and set up the variables.  */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <sys/un.h>
    #include <unistd.h>
    
    int main()
    {
        int server_sockfd, client_sockfd;
        int server_len, client_len;
        struct sockaddr_un server_address;
        struct sockaddr_un client_address;
    
    /*  Remove any old socket and create an unnamed socket for the server.  */
    
        unlink("server_socket");
        server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
    /*  Name the socket.  */
    
        server_address.sun_family = AF_UNIX;
        strcpy(server_address.sun_path, "server_socket");
        server_len = sizeof(server_address);
        bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
    
    /*  Create a connection queue and wait for clients.  */
    
        listen(server_sockfd, 5);
        while(1) {
            char ch;
    
            printf("server waiting\n");
    
    /*  Accept a connection.  */
    
            client_len = sizeof(client_address);
            client_sockfd = accept(server_sockfd, 
                (struct sockaddr *)&client_address, &client_len);
    
    /*  We can now read/write to client on client_sockfd.  */
    
            read(client_sockfd, &ch, 1);
            ch++;
            write(client_sockfd, &ch, 1);
            close(client_sockfd);
        }
    }
    the file client1.c contains the following:
    Code:
    /*  Make the necessary includes and set up the variables.  */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <sys/un.h>
    #include <unistd.h>
    
    int main()
    {
        int sockfd;
        int len;
        struct sockaddr_un address;
        int result;
        char ch = 'A';
    
    /*  Create a socket for the client.  */
    
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    
    /*  Name the socket, as agreed with the server.  */
    
        address.sun_family = AF_UNIX;
        strcpy(address.sun_path, "server_socket");
        len = sizeof(address);
    
    /*  Now connect our socket to the server's socket.  */
    
        result = connect(sockfd, (struct sockaddr *)&address, len);
    
        if(result == -1) {
            perror("oops: client1");
            exit(1);
        }
    
    /*  We can now read/write via sockfd.  */
    
        write(sockfd, &ch, 1);
        read(sockfd, &ch, 1);
        printf("char from server = %c\n", ch);
        close(sockfd);
        exit(0);
    }

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    So what exactly does not work? Could you be more specific as to what you are expecting to happen and what is actually happening?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    i'm expecting that when i write something on the client it will be sent to the server & it'll be seen

    but when i run client file then it shows "oops: client1" and says "invalid.... " some thing like that.. sorry i forgot the last message...

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Actually i ran the client1.c program in windows machine using SSH Secure Shell Client software by connecting to Linux machine and server1.c program runs on that Linux machine.

    After running the server1 it shows "server is waiting " and then when i run the client1 in SSH Secure Shell Client after logged in into Linux machine using my user name and password it shows "oops: client1 Invalid ....... "

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Actually what you're doing will only work if BOTH client and server are running on the same machine. If you need to run this on 2 separate machines, you need to use UDP or TCP protocol. I have a feeling you may be confusing UNIX sockets with TCP or UDP socket programming.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    I also tried to run both on same Linux machine but never works as it should... so if u can suggest me what change it requires i'll be more thankful.. however my course instructor was instructed me that i will only have to change some header file & some statements on client file... so please help me out here, if any one can.. the deadline is tomorrow.. if i cann't submit it by tomorrow then i wont get my grade...
    Last edited by shawon; 10-28-2009 at 04:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program on ssh client
    By shawon in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-28-2009, 03:59 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  4. socket web client
    By Abila in forum C Programming
    Replies: 0
    Last Post: 06-28-2003, 04:42 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

Tags for this Thread