如何在C上连接到服务器,定义双方端口

时间:2011-02-20 02:15:56

标签: c linux sockets connection bind

Helo人!

我正在尝试制作一个简单的程序。 将一些信息发送到服务器到端口80, 但问题是我需要指明 我必须使用的发件人(本地)端口。

在ubuntu机器上使用语言。

Ejample:

My Pc :( Port 2000)----------->服务器:(端口80)

我做了一个程序,但是没有用,有人可以帮我吗?

4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netdb.h>
9 #include <arpa/inet.h>
10
11 int main(int argc, char *argv[]) {
12
13   struct addrinfo server_hints;
14   struct addrinfo *server_res;
15   int    server_sockfd;
16   int    server_addrinfo;
17   int    server_bind;
18
19   struct addrinfo client_hints;
20   struct addrinfo *client_res;
21   int    client_sockfd;
22   int    client_addrinfo;
23   int    client_bind;
24
25   int    conn_info;
26
27   // Completamos la estrucctura de dirección del servidor con getaddrinfo():
28
29   memset(&server_hints, 0, sizeof (server_hints));
30   server_hints.ai_family   = AF_UNSPEC;     // Usa IPv4 or IPv6
31   server_hints.ai_socktype = SOCK_STREAM;   // TCP
32   server_hints.ai_flags    = AI_PASSIVE;    // Rellena la ip del server
33
34   server_addrinfo = getaddrinfo(NULL, "2000", &server_hints, &server_res);
35
36   if (server_addrinfo != 0){
37     fprintf(stderr, "Getaddrinfo server: %s\n", gai_strerror(server_addrinfo));
38     return 1;
39   }
40
41
42   // Crea la conexión del socket:
43
44   server_sockfd = socket(server_res->ai_family, server_res->ai_socktype, server_res->ai_protocol);
45
46   if (server_sockfd == -1){
47     fprintf(stderr, "Socket: %s\n", gai_strerror(server_sockfd));
48     return 1;
49   }
50
51
52   // Para eliminar esas veces que te pone que "Address already in use."
53
54   int yes=1;
55
56   if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
57     perror("setsockopt");
58     return 1;
59   }
60
61
62   // Lo lincamos con el puerto que le pasamos en getaddrinfo():
63
64   server_bind = bind(server_sockfd, server_res->ai_addr, server_res->ai_addrlen);
65
66
67   if (server_bind == -1){
68     fprintf(stderr, "Socket: %s\n", gai_strerror(server_sockfd));
69     return 1;
70   }
71
72
73   // Completamos la estrucctura de dirección del cliente con getaddrinfo():
74   memset(&client_hints, 0, sizeof(client_hints));
75   client_hints.ai_family   = AF_UNSPEC;
76   client_hints.ai_socktype = SOCK_STREAM;
77
78   client_addrinfo = getaddrinfo("82.197.1.1", "80", &client_hints, &client_res);
79
80   if (client_addrinfo != 0){
81     fprintf(stderr, "Getaddrinfo server: %s\n", gai_strerror(client_addrinfo));
82     return 1;
83   }
84
85
86   conn_info = connect(server_sockfd, client_res->ai_addr, client_res->ai_addrlen);
87
88   if (conn_info == -1){
89     fprintf(stderr, "Connect: %s\n", gai_strerror(conn_info));
90     return 1;
91   }
92
93
94   char *men = "GET /index.php?uuu=hi&ppp=bye HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0\r\n\r\n"    ;
95
96   send(server_sockfd, men, strlen(men), 0);
97
98   // Limpiamos la conexiónes:
99
100   close(server_sockfd);
101   freeaddrinfo(server_res);
102   freeaddrinfo(client_res);

感谢您的帮助! : - )

2 个答案:

答案 0 :(得分:1)

在连接之前,您需要 bind到一个地址。如果不这样做,操作系统将选择合适的source_address:source_port。这是你如何做到的。

创建套接字

s = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == s) {
    perror("socket");
    exit(1);
}

初始化sockaddr_in服务器结构

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);

if (1 != inet_pton(AF_INET, "192.168.1.1", &server_addr.sin_addr)) {
    perror("inet_pton");
    exit(1);
}

初始化客户端sockaddr_in结构并绑定到它

memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(22222);

if (bind(s, (struct sockaddr *) &client_addr, sizeof(client_addr)) < 0) {
    perror("bind");
    exit(1);
}

照常连接

if (connect(s, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
    perror("connect");
    exit(1);
}

另一种可能性是使用getaddrinfo来获取要绑定的sockaddr

memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;        /* Both v4 and v6.*/
hints.ai_flags = AI_PASSIVE;

rc = getaddrinfo(addr, port, &hints, &result);
if (rc)
    return -1;

for (rp = result; rp != NULL; rp = rp->ai_next) {
    rc = bind(s, rp->ai_addr, rp->ai_addrlen);
    if (!rc)
        break;
    /* Otherwise move on. */
}

使用tcpdump(1)来观察它是否为您的细分设置源端口。

答案 1 :(得分:-1)

使用TCP连接时,您需要使用connectwrite来写入套接字(通常只使用发送进行UDP连接)。您可以在套接字上使用read来读取响应。在服务器内write返回的套接字上使用accept将自动发回消息(假设连接仍处于打开状态),因此您无需告知服务器发送方主机和端口。