Untitled
Anonymous
plain_text
01/22/2026 5:07 AM
4.0 KB
11
Indexable
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
void check(char strr[], int value)
{
if (value < 0)
printf("%s failed\n",strr);
}
void main()
{
int cid, con, csend, crec,i;
char sendbuff[500], recbuff[500], fromaddress[500], toaddress[500], mailbody[1000];
cid = socket(AF_INET, SOCK_STREAM, 0);
check("Socket creation", cid);
struct sockaddr_in caddr;
caddr.sin_family = AF_INET;
caddr.sin_port = htons(8082);
caddr.sin_addr.s_addr = INADDR_ANY;
size=sizeof(caddr);
--con = connect(cid, (struct sockaddr*)&caddr,size);
check("connection", con);
printf("\n <-->\n");--
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("receive", 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))
printf("ok not received\n");
else
printf("Message from server: %s\n", recbuff);
printf("Enter the 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 server\n");
crec = recv(cid, recbuff, 500, 0);
check("receive", crec);
if (strncmp(recbuff, "250", 3))
printf("ok not received\n");
else
printf("Message from server: %s\n", recbuff);
printf("Enter to address: ");
scanf("%s", toaddress);
strcpy(sendbuff, "mail to:");
strcat(sendbuff, toaddress);
csend = send(cid, sendbuff, 500, 0);
check("sending", csend);
printf("Waiting ok from server\n");
crec = recv(cid, recbuff, 500, 0);
check("receive", crec);
if (strncmp(recbuff, "250", 3))
printf("ok not received\n");
else
printf("Message from server: %s\n", recbuff);
printf("Sending data to the server...\n");
strcpy(sendbuff, "data");
csend = send(cid, sendbuff, 500, 0);
check("sending", csend);
printf("Waiting ok from server\n");
crec = recv(cid, recbuff, 500, 0);
check("receive", crec);
if (strncmp(recbuff, "354", 3))
printf("ok not received\n");
else
printf("Message from server: %s\n", recbuff);
printf("Enter mail body \n");
for(i=0;;i++)
{
fgets(mailbody, sizeof(mailbody), stdin);
csend = send(cid, mailbody, 1000, 0);
if (strncmp(mailbody, "$", 1) == 0)
break;
check("sending", csend);
}
printf("sending mailbpdy to server\n");
printf("Waiting ok from server\n");
crec = recv(cid, recbuff, 500, 0);
check("receive", crec);
if (strncmp(recbuff, "221", 3))
printf("ok not received\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);
}Editor is loading...
Leave a Comment