Untitled
Anonymous
plain_text
01/22/2026 4:26 AM
4.3 KB
16
Indexable
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
void check(char str[],int value)
{
if(value<0)
{
perror(str);
exit(1);
}
}
int main()
{
int cid,con,csend,crec;
char sendbuff[500],recbuff[500],fromaddress[500],toaddress[500],mailbody[1000];
struct sockaddr_in caddr;
cid=socket(AF_INET,SOCK_STREAM,0);
check("socket creation",cid);
caddr.sin_family=AF_INET;
caddr.sin_port=htons(8083);
caddr.sin_addr.s_addr=inet_addr("127.0.0.1");
con=connect(cid,(struct sockaddr*)&caddr,sizeof(caddr));
check("CONNECTION",con);
printf("sending HI to server\n");
strcpy(sendbuff,"HI");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("waiting for server response...\n");
crec=recv(cid,recbuff,500,0);
check("RECIEVE",crec);
printf("message from server:%s\n",recbuff);
printf("sending HELLO to server\n");
strcpy(sendbuff,"HELLO");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("waiting for OK message...\n");
crec=recv(cid,recbuff,500,0);
check("RECIEVE",crec);
if(strncmp(recbuff,"250",3)!=0)
printf("OK not recieved\n");
else
printf("message from server:%s\n",recbuff);
printf("Enter FROM address:");
scanf("%s",fromaddress);
strcpy(sendbuff,"MAIL FROM:");
strcat(sendbuff,fromaddress);
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("waiting OK from sever\n");
crec=recv(cid,recbuff,500,0);
check("RECIEVE",crec);
if(strncmp(recbuff,"250",3)!=0)
printf("OK not recieved\n");
else
printf("message from server:%s\n",recbuff);
printf("Enter TO address:");
scanf("%s",toaddress);
getchar();
strcpy(sendbuff,"MAIL TO: ");
strcat(sendbuff,toaddress);
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("waiting for OK message...\n");
crec=recv(cid,recbuff,500,0);
check("RECIEVE",crec);
if(strncmp(recbuff,"250",3)!=0)
printf("OK not recieved\n");
else
printf("message from server:%s\n",recbuff);
printf("sending DATA to server...\n");
strcpy(sendbuff,"DATA");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("waiting for OK message...\n");
crec=recv(cid,recbuff,500,0);
check("RECIEVE",crec);
if(strncmp(recbuff,"354",3)!=0)
printf("OK not recieved\n");
else
printf("message from server:%s\n",recbuff);
printf("enter mail body(Type '$' on a new line to finish): \n");
while(1)
{
fgets(mailbody,sizeof(mailbody),stdin);
csend=send(cid,mailbody,1000,0);
check("SENDING",csend);
if(strncmp(mailbody,"$",1)==0)
break;
}
printf("waiting ok from server\n");
check("RECIEVE",crec);
if(strncmp(recbuff,"250",3)!=0)
printf("OK not recieved\n");
else
printf("message from server:%s\n",recbuff);
strcpy(sendbuff,"QUIT");
csend=send(cid,sendbuff,1000,0);
printf("sending %s...\n",sendbuff);
strcpy(recbuff," ");
crec=recv(cid,recbuff,500,0);
if(strncmp(recbuff,"221 OK",6)==0)
{
printf("Exiting...\n");
}
printf("connection closed\n");
close(cid);
return 0;
}
Editor is loading...
Leave a Comment