Hi, I am stuck on a homework assignment where we use SMTP to transmit an email a
ID: 3605346 • Letter: H
Question
Hi,
I am stuck on a homework assignment where we use SMTP to transmit an email across a client/ server using read and send functions. I have read the unistd.h implementation of the functions read and send. Could someone possibly explain via pseudo or example how i could use this in terms of sockets? I will be using a while loop to read byte by byte the message. Not looking for a fully written program or anything , but if it helps understanding the function of sockets in an email transaction then by all means share the knowledge! ( The main function is written so just in terms of the parameters found in unix documentation.)
Explanation / Answer
Instad of read byte by byte you can go for structure. it will work properly according to your expectation .
so create common structure for both side and use it . I am posting sample program here. go threw it. I hope it may helpful to you.
/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio_ext.h>
#define SIZE 30
#define NAME_SIZE 15
int sock_fd,ret;
typedef struct cli_data {
char name[NAME_SIZE];
char data[SIZE];
}cli_data_t;
int main(int argc,char **argv)
{
struct sockaddr_in connectAddr;
socklen_t len;
cli_data_t r_info;
if(argc != 3) {
printf("Usage:./client port_number name ");
goto done;
}
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if (sock_fd == -1) {
perror("socket");
goto done;
}
printf("Client_fd: %d ",sock_fd);
connectAddr.sin_family = AF_INET;
connectAddr.sin_port = htons(atoi(argv[1]));
connectAddr.sin_addr.s_addr = INADDR_ANY;
len = sizeof(connectAddr);
if (connect(sock_fd,(struct sockaddr *)&connectAddr,len) == -1) {
perror("connect_fail");
goto done;
}
perror("connect");
while(1) {
bzero(r_info.data,SIZE);
bzero(r_info.name,NAME_SIZE);
strcpy(r_info.name, argv[2]);
printf("Waiting for your input............... ");
scanf(" %[^ ]",r_info.data);
if (write(sock_fd,&r_info,sizeof(cli_data_t)) == -1) {
perror("Write_Faild");
goto done;
}
if (read(sock_fd,&r_info,sizeof(cli_data_t)) == -1) {
perror("read");
goto done;
}
printf(" %s: %s ",r_info.name,r_info.data);
}
done:
return 0;
}
/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio_ext.h>
#define SIZE 30
#define NAME_SIZE 15
int sock_fd,ret;
typedef struct cli_data {
char name[NAME_SIZE];
char data[SIZE];
}cli_data_t;
int main(int argc,char **argv)
{
struct sockaddr_in connectAddr;
socklen_t len;
cli_data_t r_info;
if(argc != 3) {
printf("Usage:./client port_number name ");
goto done;
}
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if (sock_fd == -1) {
perror("socket");
goto done;
}
printf("Client_fd: %d ",sock_fd);
connectAddr.sin_family = AF_INET;
connectAddr.sin_port = htons(atoi(argv[1]));
connectAddr.sin_addr.s_addr = INADDR_ANY;
len = sizeof(connectAddr);
if (connect(sock_fd,(struct sockaddr *)&connectAddr,len) == -1) {
perror("connect_fail");
goto done;
}
perror("connect");
while(1) {
bzero(r_info.data,SIZE);
bzero(r_info.name,NAME_SIZE);
strcpy(r_info.name, argv[2]);
printf("Waiting for your input............... ");
scanf(" %[^ ]",r_info.data);
if (write(sock_fd,&r_info,sizeof(cli_data_t)) == -1) {
perror("Write_Faild");
goto done;
}
if (read(sock_fd,&r_info,sizeof(cli_data_t)) == -1) {
perror("read");
goto done;
}
printf(" %s: %s ",r_info.name,r_info.data);
}
done:
return 0;
}
/////////////////////////////////////////////////////////////////////////
give ./server portNO form server side to run server code .
give ./client portNO YourName form client side to run client code.
It will convert your data into upper or lower case .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.