C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2009, 08:51 AM   #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);
}
shawon is offline   Reply With Quote
Old 10-27-2009, 11:43 AM   #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?
pantherse is offline   Reply With Quote
Old 10-27-2009, 11:52 AM   #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...
shawon is offline   Reply With Quote
Old 10-27-2009, 12:07 PM   #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 ....... "
shawon is offline   Reply With Quote
Old 10-27-2009, 01:11 PM   #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.
pantherse is offline   Reply With Quote
Old 10-28-2009, 04:01 AM   #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.
shawon is offline   Reply With Quote
Reply

Tags
socket, ssh client

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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