如何连接到邮件服务器用C [英] How to connect to mail server in C

查看:196
本文介绍了如何连接到邮件服务器用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的邮件服务器的连接是在局域网。邮件服务器的IP为192.168.1.1。于是,我尝试了
下面的程序来测试。

I tried to make a connection to my mail server which is in local area network. The ip of mail server is 192.168.1.1. So, I tried the following program to test that.

程序:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
int main()
{
    struct sockaddr_in sa;
    struct in_addr ip;

    int fd=socket(AF_INET,SOCK_STREAM,0);

    if(inet_pton(AF_INET,"192.168.1.1",&ip)==-1){
        printf("Unable to convert ip to binary\n");
        perror("");
        exit(1);
    }

    sa.sin_family=AF_INET;
    sa.sin_port=25;
    sa.sin_addr=ip;

    if(connect(fd,(struct sockaddr*)&sa,sizeof(sa))==-1){
        printf("Unable to connect to server\n");
        perror("");
        exit(1);
    }
    else{
        printf("Successfully connected to server...\n");
    }
}

输出:

$ ./a.out 
Unable to connect to server
Connection refused
$

但是通过telnet,它被成功连接,如下所示。

But via telnet, it is successfully connected as shown below.

$ telnet 192.168.1.1 25
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
220 mail.msys.co.in ESMTP Postfix (Debian/GNU)
^]
telnet> Connection closed.
$

那么,什么是我在这里完成的错误。这有什么错在我的计划。我请求你帮我解决这个问题,为什么它会发生。

So, what is the mistake I done here. Is there anything wrong in my program. I request you to help me solve this problem and why it occurs.

推荐答案

随着Wireshark的跟踪,你可以看到你的code试图连接到端口6400尝试:

With a wireshark trace you can see that your code tries to connect to port 6400. Try:

sa.sin_port = htons(25);

sa.sin_port=htons(25);

这篇关于如何连接到邮件服务器用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆