PDA

View Full Version : سوال: سوال درباره برنامه كلاينت سرور به زبان cتو لينوكس



zahra.mf
سه شنبه 10 آبان 1390, 22:22 عصر
سلام
من يه برنامه كلاينت سرور به زبان cتو لينوكس نوشتم برنامه كلاينت و سرور هر دوcompileميشن و error هم ندارن ولي نميتونن با هم ارتباط برقرار كنن هم تابع connectو توكلاينت نوشتم هم تابع acceptو تو سرور

aram_2
سه شنبه 17 آبان 1390, 10:17 صبح
کدش رو بذارید تا راهنماییتون کنم

zahra.mf
سه شنبه 24 آبان 1390, 19:30 عصر
سوالى كه راجع به برنامه كلاينت سرور داشتم و توconnectمشكل داشتم مشكل connectحل شد و رابطه بين كلاينت و سرور برقرار شده كلاينت هم از كاربر usernameوpasswordميخاد ولي وقتيusernameوpasswordميگيره بايد هر جي تو homeلينوكس داريم جاب كنه ولي برنامه كاري نميكنه نميدونم مشكل از سرور يا كلاينت.من سورس برنامه اي كه درستش كردم و اجرا ميشه ميذارم هر كي فهميد مشكلش از كجاست يا سرور مشكل داره يا كلاينت كمكم كنه
username=zahra
password=mina
كار برنامهreaddirectoriاينه كه وقتي كلاينت از سرور ميخاد كه ليشت برنامه هاي تويhomeرو براش جاب كنه سرور با استفاده از اين برنامه اين كار و انجام ميده حلقه whileكه قرار دادم برا اينه كه سرور با يه بار دريافت درخاست از كلاينت كارش تموم نشه و بازم منتظر درخاست بمونه يه جيز ديكه هم اون فايلي كه قراره سرور بره ازشuserوpasswordبخونه وبا userوpassكلاينت مقايسه كنه تو خط اولش شمارهportنوشته شده خط دومlocalhostخط سوم هم userوpassواينجوري نوشتمzahra:mina
نحوه اجراش هم اينطوريه كه اول كلاينت كامبايل ميشه بعد سرور بعد سرور runميكنيم منتظر ميمونه تا يه تلرمينال جديد باز كنيم براي اجراي كلاينت با دستور اجرا بايد به صورت زير بنويسم همه توي يه خط
localhost
شمارهport
آدرس homeكامبيوتر
سرور

1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<FONT face=Tahoma>
#define DIRSIZE 8192
main()
{

char dir[DIRSIZE] ; /* used for incomming dir name, and
outgoing data */
char hostname,PORT[10],HOST[10],username[30],password[30],user[20],pass[20],port1[4];
char Z[30],M[30];
int sd, sd_current, cc, fromlen, tolen;
int addrlen;
int port ;

struct sockaddr_in sin;
struct sockaddr_in pin;
FILE *p;
p=fopen("server.conf","r");
fseek(p,0,SEEK_SET);
fscanf(p,"%4s",port1);

port=atoi(port1);



/* get an internet domain socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* complete the socket structure */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
/* bind the socket to the port number */
if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
perror("bind");
exit(1);
}
/* show that we are willing to listen */

if (listen(sd,5) == -1) {
perror("listen");
exit(1);
}
/* wait for a client to talk to us */
/* socklen_t addrlen = sizeof(pin); */
while(1){
if ((sd_current = accept(sd, (struct sockaddr *) &pin, &addrlen)) == -1) {
perror("accept");
exit(1);
}

if(send(sd_current, "200" ,strlen("200"), 0) == -1){
perror("send");
exit(1);
}
/* get a message from the client */
if(recv(sd_current,username,sizeof(username), 0) == -1){
perror("recv");
exit(1);
}

if(recv(sd_current,password,sizeof(password), 0) == -1){
perror("recv");
exit(1);
}

if (recv(sd_current, dir, sizeof(dir), 0) == -1) {
perror("recv");
exit(1);
}
fseek(p,22,SEEK_SET);
fscanf(p,"%5s",user);
fseek(p,1,SEEK_CUR);
fscanf(p,"%4s",pass);

if(strcmp (user, username) == 0){

if( strcmp ( pass, password) == 0){

read_dir(dir);

if (send(sd_current, dir, strlen(dir), 0) == -1) {
perror("send");
exit(1);
}

}


else{

if (send(sd_current, "202", strlen("202"), 0) == -1){
perror("send");
exit(1);}
}

}





else{
if (send(sd_current, "201", strlen("201"), 0) == -1){
perror("send");
exit(1);}
}









}
close(sd_current);

/* give client a chance to properly shutdown */
sleep(1);
}

</FONT>




كلاينت

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

/* REPLACE with your server machine name*/
#define DIRSIZE 8192
main(argc, argv)
int argc; char **argv;
{
char hostname[100],portnumber[100],password[30],username[30],INVALIDUSER[30],INVALIDPASS[30],OK[5];
char dir[DIRSIZE];
int sd;
int port1;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent *hp;

if (argc>3)
{
strcpy(hostname,argv[1]);
strcpy(portnumber,argv[2]);
strcpy(dir,argv[3]);
}

port1=atoi(portnumber);
/* go find out about the desired host machine */
if ((hp = gethostbyname(hostname)) == 0) {
perror("gethostbyname");
exit(1);
}
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(port1);
/* grab an Internet domain socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* connect to PORT on HOST */
if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
perror("connect");
exit(1);
}
if(recv(sd,OK,sizeof(OK),0) == -1){
perror("recv");
exit(1);
}
printf("%s","username:");
scanf("%s",username);
printf("%s","password:");
scanf("%s",password);

if(send(sd,username,strlen(username),0) == -1){
perror("send");
}
if(send(sd,password,strlen(password),0) == -1){
perror("send");
exit(1);
}

/* send a message to the server PORT on machine HOST */
if (send(sd, argv[3], strlen(argv[3]), 0) == -1) {
perror("send");
exit(1);
}


/* wait for a message to come back from the server */
if (recv(sd, dir, DIRSIZE, 0) == -1) {
perror("recv");
exit(1);
printf("%s\n", dir);
}
else{
if(recv(sd,INVALIDUSER,sizeof(INVALIDUSER),0) == -1){
perror("recv");
exit(1);
}
if(recv(sd,INVALIDPASS,sizeof(INVALIDPASS),0) == -1){
perror("recv");
exit(1);}
printf("ERROR");


}
/* spew-out the results and bail out of here! */
close(sd);
}




readdirectory

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#define DIR_SIZE 8192
read_dir(dir)
char *dir; /* anticipate char dir[DIR_SIZE] */
{
DIR * dirp;
struct dirent *d;
/* open directory */
dirp = opendir(dir);
if (dirp == NULL)
return(0);
/* stuff filenames into dir buffer */
dir[0] = '\0';
while (d = readdir(dirp))
sprintf(dir, "%s%s\n", dir, d->d_name);
/* return the result */
closedir(dirp);
}