PDA

View Full Version : کامپایل سورس سوکت به زبان سی



policweb
یک شنبه 26 آبان 1392, 02:00 صبح
سلام دوستان یه سورس سوکت به زبان سی دارم هرکاری میکنم با visual studio 2010کامپایل نمیشه 2006 رو هم ندارم امتحان کنم.
خواهشن یکی کامپایلش کنه خیلی ضروریه!!!!!!!!
اصلن نمیدونم چرا این ارورا رو میده؟
==============================
سورس مربوط به سرویس دهنده smtp هست
و سورس دومی برای pop3
=============================

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

#ifndef _MAILBOX_H
#define _MAILBOX_H

/*the port mail clients will be connecting to */
#define MSERVERPORT 3476
/* server hold 20 mails at most*/
#define MAILMAX 20
/*all message are of fixed length: MSGLEN*/
#define MSGLEN 80

#define START 1
#define QUIT 2
#define RETRIEVE_MESSAGE 3
#define INSERT_MESSAGE 4
#define LIST_ALL_MESSAGES 5
#define DELETE_MESSAGE 6

typedef char mess[MSGLEN];

struct argument {
int cmdType;
mess user;
int messageID;
mess message;
};
typedef struct argument argument;

struct srvResponse {
int respondID; /* 0: successful, otherwise error*/
mess message;
};
typedef struct srvResponse srvResponse;

struct mails {
int mailNum;
mess mails[MAILMAX];
};
typedef struct mails mails;

#endif

#define MAXUSER 100
static char* INTERNALERR = "System internal error";

struct mailbox {
char user[80];
struct timeval lastAccessTime;
mails mails;
};
typedef struct mailbox mailbox;
static mailbox * mailSpool[MAXUSER];
static char* errMessage;
void debug(char * msg) {errMessage = msg;}
int getEmptyMailSpool() {
int i;
for (i=0; i<MAXUSER;i++) {
if (mailSpool[i] == NULL) return i;
}
debug("MailSpool is full");
return -1;
}
mailbox * getMailBox(char* user) {
int i;
i = getMailBoxIndex(user);
if (i< 0) return NULL;
else return mailSpool[i];
}
int getMailBoxIndex(char * user) {
int i;
struct timeval curTime;
char buffer[100];
for (i=0; i<MAXUSER;i++) {
if ((mailSpool[i] != NULL) &&
(strcmp(user,mailSpool[i]->user) == 0)) {
gettimeofday(&curTime,NULL);
if ((curTime.tv_sec - mailSpool[i]->
lastAccessTime.tv_sec) >300) {
debug("session expired");
return -1;
}
gettimeofday(&mailSpool[i]->lastAccessTime,NULL);
return i;
}
}
sprintf(buffer,"mailbox for %s not found",user);
debug(buffer);
return -1;
}

/*
1. read "user.mbx" from local file system
2. if file not exist, then create one
3. initialize the necessary fields
4. add it to the mailSpool
status: test
*/
mailbox* loadMailBox(char* user) {
FILE * fp;
char filename[MSGMAX+5];
char msg[MSGMAX+1];
mailbox* mbox;
int i;
strcpy(filename,user);
strcat(filename,".mbx");


fp = fopen(filename,"a+");
if (fp == NULL) {
debug("open file failed");
return NULL;
}
mbox = (mailbox *) malloc(sizeof(mailbox));
if (mbox == NULL) {
debug("allocate memory for mbox failed");
fclose(fp);
return NULL;
}
//initialize the mailbox
mbox->mails.mailNum = 0;
gettimeofday(&mbox->lastAccessTime,NULL);
strcpy(mbox->user,user);
while ((mbox->mails.mailNum < MAILMAX) &&
(fgets(msg,MSGMAX+1,fp) !==
strcpy(mbox->mails.mails[mbox->mails.mailNum],msg); NULL)){
mbox->mails.mailNum ++;
}
i = getEmptyMailSpool();
if (i == -1) {
free(mbox);
fclose(fp);
return NULL;
}
mailSpool[i] = mbox;
fclose(fp);
return mbox;
}

/*
save line by line
*/
void saveMailBox(mailbox * mbox) {
FILE * fp;
char filename[MSGMAX+5];
char msg[MSGMAX+1];
int i;
strcpy(filename,mbox->user);
strcat(filename,".mbx");
fp = fopen(filename,"w");
if (fp == NULL) {
debug("open file failed");
return NULL;
}
for (i=0; i<mbox->mails.mailNum; i++) {
fputs(mbox->mails.mails[i],fp);
}

fclose(fp);
}
/*
getMailBox, if success, do nothing
else loadMailBox, and insert it to the mailspool
*/
char ** start_2(char ** user,struct svc_req * req){
mailbox* myMbox = getMailBox(*user);
if (myMbox == NULL) { //not logged in
myMbox = loadMailBox(*user);
}
if (myMbox == NULL) {
return & errMessage; //what's the matter?
}
strcat(*user," log in successful");
return(user);
}
/*
remove the user from the spool
save the mailbox
*/
char ** quit_2(char ** user,struct svc_req * req){
int i = getMailBoxIndex(*user);
mailbox* myMbox;
if (i < 0) {
return & errMessage; //what's the matter?
}
myMbox = mailSpool[i];
saveMailBox(myMbox);
return(user);
}
/*
locate the mail box
*/
char ** insert_message_2(insert_argument * arg,struct svc_req * req){
mailbox* myMailBox;
myMailBox = getMailBox(arg->user);
if (myMailBox == NULL) {
return &errMessage;
}
if (myMailBox->mails.mailNum >= MAILMAX) {
debug("mailbox reached it's max size\n");
return &errMessage;
}
strcpy(myMailBox->mails.mails[myMailBox->mails.mailNum],arg->mesg);
myMailBox->mails.mailNum ++;
debug("mail insert successful");
return(&errMessage);
}

/*
design:
1. get mailbox, if error, return mails with mailNum = -1, put the error meg in mails[0]
2. return all the mails
status: design
*/
mails* list_all_messages_2(char** user,struct svc_req * req){
mailbox* myMailBox;
static mails allMails;
myMailBox = getMailBox(*user);
if (myMailBox == NULL) {
allMails.mailNum = -1;
strcpy(allMails.mails[0],errMessage);
return &allMails;
}
return &(myMailBox->mails);
}
char** retrieve_message_2(argument * arg,struct svc_req * req){
mailbox* myMailBox;
int i;
myMailBox = getMailBox(arg->user);
if (myMailBox == NULL) {
return &errMessage;
}
if (arg->number >= myMailBox->mails.mailNum) {
debug("No such mail\n");
return &errMessage;
}
debug(myMailBox->mails.mails[arg->number]);
return(&errMessage);
}
char ** delete_message_2(argument * arg,struct svc_req * req){
mailbox* myMailBox;
int i;
myMailBox = getMailBox(arg->user);
if (myMailBox == NULL) {
return &errMessage;
}
if (arg->number >= myMailBox->mails.mailNum) {
debug("No such mail\n");
return &errMessage;
}
for (i=arg->number; i<myMailBox->mails.mailNum-1;i++) {
strcpy(myMailBox->mails.mails[i],myMailBox->mails.mails[i+1]);
}
myMailBox->mails.mailNum --;
debug("mail delete successful");
return(&errMessage);
}

============================================

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

/*the port mail clients will be connecting to */
#define MSERVERPORT 3476
/* server hold 20 mails at most*/
#define MAILMAX 20
/*all message are of fixed length: MSGLEN*/
#define MSGLEN 80

#define START 1
#define QUIT 2
#define RETRIEVE_MESSAGE 3
#define INSERT_MESSAGE 4
#define LIST_ALL_MESSAGES 5
#define DELETE_MESSAGE 6

typedef char mess[MSGLEN];

struct argument {
int cmdType;
mess user;
int messageID;
mess message;
};
typedef struct argument argument;

/*
server to client message
*/
struct srvResponse {
int respondID; /* 0: successful, otherwise error*/
mess message;
};
typedef struct srvResponse srvResponse;

struct mails {
int mailNum;
mess mails[MAILMAX];
};
typedef struct mails mails;

#endif /* !_MAILBOX_H */
#define CMDQUIT 0
#define CMDLIST 1
#define CMDREAD 2
#define CMDINSERT 3
#define CMDDELETE 4

void main(int argc, char* argv[]){
CLIENT *cl;
char hname[70];
char* user;
char cmd[80];
int cmdType;
char* tokens[10];
char* token;
int tokenNum;
char** p;
int quit = 0;
mails * myMails;
int i;
argument arg;
insert_argument insert_arg;

int messageID;
char message[80+1];

if (argc != 3) {
printf("Usage: rpcclient hostname userID\n");
exit(1);
}
cl = clnt_create(argv[1],MAILBOX_PROG, MAILBOX_VERS, "udp");
if (cl == NULL) {
clnt_pcreateerror(argv[1]);
exit(1);
}

printf("Getting ready to call start\n");
// strcpy(host,"ClientA");


i = gethostname(hname,69);
printf("hostname = %s i= %d\n",hname,i);
user = strcat(hname,argv[2]);
printf("user = %s i= %d\n",user,i);
p = start_2(&user,cl);
if (p == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
printf("%s\n",*p);
printf("press any key to continue"); getchar();
/*
command interpreter
*/
while (quit == 0) {
system("clear");
printf("******* Available Commands *******\n");
printf(" quit --> quit the system\n");
printf(" list --> list all mails\n");
printf(" read [msgNO] -->read a certain mail\n");
printf(" delete [msgNO] --> delete the mail\n");
printf(" insert --> insert a message\n");
printf("***********************************\n");
printf("cmd>");
gets(cmd);
for((token=strtok(cmd,""));token&&(tokenNum < 9);token=
strtok(NULL,”")) {
tokens[tokenNum] = token;
tokenNum ++;
}
if (tokenNum < 1) {
printf("command error, press any key to continue");
getchar();
continue;
}
cmdType = -1;
if (strcmp(tokens[0],"quit") == 0) cmdType = CMDQUIT;
if (strcmp(tokens[0],"list") == 0) cmdType = CMDLIST;
if (strcmp(tokens[0],"read") == 0) {
printf(" messageID:");
gets(message);
messageID = atoi(message);
if (messageID != 0) {
messageID --;
cmdType = CMDREAD;
}
}
if (strcmp(tokens[0],"delete") == 0) {
printf(" messageID:");
gets(message);
messageID = atoi(message);
if (messageID != 0) {
messageID --;
cmdType = CMDDELETE;
}
}
if (strcmp(tokens[0],"insert") == 0) {
printf(" message:");
gets(message);
cmdType = CMDINSERT;
}
switch (cmdType) {
case -1:
printf("command error, press any key to continue");
getchar();
break;
case CMDQUIT:
p= quit_2(&user, cl);
if (p == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
quit= 1;
printf("Thanks for using Zheng Run's Mail System\n");
printf("Bye! Bye %s!\n",*p);
break;
case CMDLIST:
myMails = list_all_messages_2(&user, cl);
if (myMails == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
if (myMails->mailNum < 0) { //error
printf("%s\n",myMails->mails[0]);
} else {
printf("you have %d mails totally\n",myMails->mailNum);
for (i=0; i< myMails->mailNum;i++) {
printf("%d: %s",i+1,myMails->mails[i]);
}
}
printf("retrived mail list finished, press any key to continue");
getchar();
break;
case CMDREAD:
strcpy(arg.user,user);
arg.number = messageID;
p= retrieve_message_2(&arg, cl);
if (p == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
printf("%s press any key to continue",*p); getchar();
break;
break;
case CMDDELETE:
strcpy(arg.user,user);
arg.number = messageID;
p= delete_message_2(&arg, cl);
if (p == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
printf("%s press any key to continue",*p); getchar();
break;
case CMDINSERT:
strcpy(insert_arg.user,user);
strcpy(insert_arg.mesg,message);
strcat(insert_arg.mesg,"\n");
p= insert_message_2(&insert_arg, cl);
if (p == NULL) {
clnt_perror(cl,argv[1]);
exit(1);
}
printf("%s press any key to continue",*p); getchar();
break;
}
}

clnt_destroy(cl);

}

rahnema1
یک شنبه 26 آبان 1392, 11:05 صبح
این برنامه با کامپایلر gcc و احتمالا تحت لینوکس یا cygwin باید کامپایل بشه و لایبری sunrpc که یک لایبری قدیمیه هم باید استفاده بشه حالا من یه اصلاحاتی انجام دادم اگه تمام چیزها توی لینوکس یا cygwin نصب بود ان شا الله اجرا بشه فایلش ضمیمه شده

policweb
یک شنبه 26 آبان 1392, 13:21 عصر
سلام
شرمنده داداش جفتشون ارور میده !
بازم مرسی
میشه فایلهای exe که داخل سیستم شما اجرا میشه رو پیوست کنید؟

rahnema1
یک شنبه 26 آبان 1392, 21:32 عصر
سلام
بگید چه اروری میده ارور رو بذارید
با چی کامپایل کردید؟
اولی که اصلا تابع main نداره که exe درست کنه
واسه دومی هم من لایبری sunrpc رو نصب نکرده بودم که بشه لینکش کرد
فقط خطاهای کامپایلش رو اصلاح کردم

policweb
دوشنبه 27 آبان 1392, 00:23 صبح
سلام راستش من بلد نیستم با کامپایلرهای تحت لینوکس با هیچ کدومشون کار کنم و فقط با ویژال استدیو کار میکنم اما بازم زمان اجرا ارور های unable و define میده نمیدونم مشکل از سیستم منه حتی چند با vs رو حذف و نصب کردم ولی تاثیری نداشت! به این برنامه ها خیلی نیاز دارم هم کدهای کامپایلش و هم فایل اجرایی خواهشن کمک کنید.این سورس پایینی مربوط به یه سرویس گیرنده smtp تحت ویندوزه نیاز به فایلهاش دارم

#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <windows.h>

typedef char stringa[500];

SOCKET conn;

void RispostaServer();

int main(int argc,char **argv){
FILE *fp;
int lun, err=0;
char buf[1500],end[7];
char filecar;
SOCKADDR_IN conn_addr;
WSADATA data;
WORD ver;
LPHOSTENT host;

if(argc!=4){
printf("Uso: \"%s\" <Indirizzo server SMTP> <mittente>
<destinatario>\n",argv[0]);

exit(0);
}
fp=fopen("mail.txt","r");
if(fp==NULL){
printf("Errore file \"mail.txt\"");
return (1);
}
ver=MAKEWORD(2,0);
WSAStartup(ver,&data);
conn=socket(PF_INET,SOCK_STREAM,0);
conn_addr.sin_family=AF_INET;
conn_addr.sin_port=htons(25);
host=gethostbyname(argv[1]);
if(host==NULL){
err=WSAGetLastError();
printf("Errore host\t%d",err);
exit(1);
}
conn_addr.sin_addr=*((LPIN_ADDR)*host->h_addr_list);
lun=sizeof(struct sockaddr);
err=connect(conn,(struct sockaddr*)&conn_addr,lun);
if(err!=0){
err=WSAGetLastError();
printf("Errore socket\t%d",err);
exit(1);
}
RispostaServer();
sprintf(buf,"helo sendmail\nmail from:<%s>\nrcpt
to:<%s>\ndata\n",argv[2],argv[3]);
send(conn, buf, strlen(buf), 0);
printf(buf);
RispostaServer();
printf("Corpo mail\n");
while(filecar!='§'){
fscanf (fp,"%c",&filecar);
if(filecar!='§')
send(conn,&filecar,1,0);
}
fclose(fp);
RispostaServer();
sprintf(end,"quit\n");
send(conn,end,strlen(end),0);
printf(end);
RispostaServer();
closesocket(conn);
WSACleanup();
return 0;
}

void RispostaServer(){
char mess[1000];
int n=0;
n=recv(conn,mess,200,0);
mess[n]=0;
printf(mess,"%s");
}

کد اولی در پست اولی مربرط به سرویس دهنده بود ولی تحت لینوکس

rahnema1
دوشنبه 27 آبان 1392, 14:03 عصر
این هم فایلهای کمپایل شده با cygwin:
http://mihanbit.com/download/5289f7039c768/cyg.7z

policweb
دوشنبه 27 آبان 1392, 15:09 عصر
سلام
بخدا شرمنده شما رو به زحمت میندازیم دیگه روم نمیشه پست بدم .این فایل زیپ تون دانلود نمیشه اورو 403 forbidden میده اگه میشه داخل خود انجمن آپ کنید
انشالا خیر ببینی!

rahnema1
دوشنبه 27 آبان 1392, 15:20 عصر
انجمن بیشتر از 400K اجازه نمیده
http://www12.online-convert.com/download-file/464a2636f63ec08f6f7fd0e4a2e3b030/converted-5989bac8.7z