-
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);
}
-
So what exactly does not work? Could you be more specific as to what you are expecting to happen and what is actually happening?
-
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...
-
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 ....... "
-
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.
-
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...