Reputation: 105
I'm programming an interactive SMTP Client, which can talk to an SMTP Server though the terminal. So far I've established a connection (server response 220 after first recv()
), and then try to send a HELO
to the Server. The send function gives me a return value of 15, so that should be working. But then I try to read the response (which should be 250 OK
), but the program blocks and returns nothing. I'm using fake SMTP as a testing Server.
Here is my code so far:
char IPADDRESS []= "127.0.0.1";
int initConnect(){
int sock = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,NULL, 0);
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_port = htons(25);
serv_addr.sin_family = AF_INET;
if(inet_pton(AF_INET, IPADDRESS, &serv_addr.sin_addr) > 0){
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
}else{
return -1;
}
return sock;
}
int main(void){
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
int FLAGS = 0;
int fd;
fd = initConnect();
if(fd == -1){
printf("Error occured while connecting to the Server");
exit(1);
} else {
printf(" -> Connected\n");
}
// get response from Server after connection
int recBytes = recv(fd, buffer, sizeof(buffer), FLAGS);
printf("Server: %s", buffer);
// send HELO cmd to Sever (if < 0 error)
int ret = send(fd, "HELO 127.0.0.1\n", strlen("HELO 127.0.0.1\n"), FLAGS);
printf("\nSend bytes: %i\n", ret);
// get response from Server after HELO (program blocks here)
memset(buffer, 0, sizeof(buffer));
recBytes = recv(fd, buffer, sizeof(buffer), FLAGS);
printf("\nServer: %s", buffer);
}
Upvotes: 0
Views: 422