Vous êtes sur la page 1sur 4

ANS-1

CLIENT.C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

// the port client will be connecting to


#define PORT 3490
// max number of bytes we can get at once
#define MAXDATASIZE 300

int main(int argc, char *argv[])


{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
// connector’s address information
struct sockaddr_in their_addr;

// if no command line argument supplied


if(argc != 2)
{
fprintf(stderr, "Client-Usage: %s the_client_hostname\n", argv[0]);
// just exit
exit(1);
}

// get the host info


if((he=gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname()");
exit(1);
}
else
printf("Client-The remote host is: %s\n", argv[1]);

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)


{
perror("socket()");
exit(1);
}
else
printf("Client-The socket() sockfd is OK...\n");

// host byte order


their_addr.sin_family = AF_INET;
// short, network byte order
printf("Server-Using %s and port %d...\n", argv[1], PORT);
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
// zero the rest of the struct
memset(&(their_addr.sin_zero), '\0', 8);

if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct


sockaddr)) == -1)
{
perror("connect()");
exit(1);
}
else
printf("Client-The connect() is OK...\n");
printf("\nType a string: (STOP to ending connection)\n\n");
do{
while ((c = getchar ()) != '\n' && i < MAXLENGHT){
buf[i++] = c;
}
buf[i] = '\0';
len = strlen(buf);
}
while( strcmp(buf,"STOP")!=0);
close(sockfd);
exit(0);
}
}

SERVER.C
int main (int argc, char **argv) {
int sock, fd;
socklen_t client_len;
struct sockaddr_in server, client;

if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1) {


perror ("Socket failed");
exit (1);
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons (SERVER_PORT);
if (bind ( sock, (struct sockaddr *) &server, sizeof server) == -1) {
perror ("bind failed");
exit (2);
}

listen (sock,1);

while (1) {
client_len = sizeof (client);
if ((fd = accept ( sock, (struct sockaddr *) &client, &client_len)) < 0) {
perror ("accepting connection");
exit(3);
}
fprintf (stderr, "\nOpen.\n");
send (fd, "\nWelcome!\n", 50, 0);
check_stats (fd, fd);}

close(fd);
fprintf (stderr, "\nClose\n");
exit (0);

ANS-2

Connecting to a remote Linux server


To make a connection to your remote server:
1. Switch to the Remote System Explorer perspective. From the workbench menu, click Window > Open
Perspective > Remote System Explorer.
2. In the Remote Systems view, New Connection is automatically expanded to show the various remote systems
you can connect to through the Remote System Explorer. Expand Linux or Unix to invoke the new connection
dialog box and configure a connection.
3. Enter a name for your first profile and click Next. (This step only occurs if you have never defined a connection
before.)
4. Enter a connection name. This name displays in your tree view and must be unique to the profile.
5. Enter the name or TCP/IP address of your Linux server in the Host name field, for example, LINUX_A.
6. (Optional) Enter a Description. The description appears in the Properties view after the connection is created.
7. Click Finish to define your system.

To transfer a Directory to Remote Server.


scp -r C:/site user@server_ip:path
path is the place, where site will be copied into the remote server
(B)
Follow the given link & write all steps from there
http://www.howtoforge.com/setting-up-a-linux-file-server-using-samba

Vous aimerez peut-être aussi