先决条件:使用C / C++进行套接字编程。
在套接字编程中,当服务器和客户端连接时,操作系统会为客户端提供任何随机端口号来运行,通常我们并不关心它,但是在某些情况下,客户端可能只有一台防火墙,允许某些端口号上的传出连接。因此,很有可能操作系统提供给客户端的端口号可能已被客户端防火墙阻止。在这种情况下,我们需要显式或强制将任何端口号分配给可以在其上运行的客户端。
某些协议(例如NFS协议)要求客户端程序仅在特定端口号上运行,因此在这种情况下,客户端仅在端口号在111或2049上运行时才需要强制分配该端口号。这可以使用bind( )在客户端套接字中指定特定端口号的系统调用。
下面是实施服务器和客户端程序,其中将强制为客户端分配端口号。
服务器端程序
// C program to demonstrate
// socket programming in finding ip address
// and port number of connected client
// on Server Side
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
// Two buffers for message communication
char buffer1[256], buffer2[256];
int server = socket(AF_INET, SOCK_STREAM, 0);
if (server < 0)
printf("Error in server creating\n");
else
printf("Server Created\n");
struct sockaddr_in my_addr, peer_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
// This ip address will change according to the machine
my_addr.sin_addr.s_addr = inet_addr("10.32.40.213");
my_addr.sin_port = htons(12000);
if (bind(server, (struct sockaddr*) &my_addr, sizeof(my_addr)) == 0)
printf("Binded Correctly\n");
else
printf("Unable to bind\n");
if (listen(server, 3) == 0)
printf("Listening ...\n");
else
printf("Unable to listen\n");
socklen_t addr_size;
addr_size = sizeof(struct sockaddr_in);
// Ip character array will store the ip address of client
char *ip;
// while loop is iterated infinitely to
// accept infinite connection one by one
while (1)
{
int acc = accept(server, (struct sockaddr*) &peer_addr, &addr_size);
printf("Connection Established\n");
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(peer_addr.sin_addr), ip, INET_ADDRSTRLEN);
// "ntohs(peer_addr.sin_port)" function is
// for finding port number of client
printf("connection established with IP : %s and PORT : %d\n",
ip, ntohs(peer_addr.sin_port));
recv(acc, buffer2, 256, 0);
printf("Client : %s\n", buffer2);
strcpy(buffer1, "Hello");
send(acc, buffer1, 256, 0);
}
return 0;
}
输出:
Server Created
Binded Correctly
Listening ...
Connection Established
connection established with IP : 10.32.40.213 and PORT : 12010
Client : Hello
客户端程序
// C program to demonstrate socket programming
// as well as explicitly assigning a port number
// on Client Side
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
// Two buffer are for message communication
char buffer1[256], buffer2[256];
struct sockaddr_in my_addr, my_addr1;
int client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
printf("Error in client creating\n");
else
printf("Client Created\n");
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY;
my_addr.sin_port = htons(12000);
// This ip address will change according to the machine
my_addr.sin_addr.s_addr = inet_addr("10.32.40.213");
// Explicitly assigning port number 12010 by
// binding client with that port
my_addr1.sin_family = AF_INET;
my_addr1.sin_addr.s_addr = INADDR_ANY;
my_addr1.sin_port = htons(12010);
// This ip address will change according to the machine
my_addr1.sin_addr.s_addr = inet_addr("10.32.40.213");
if (bind(client, (struct sockaddr*) &my_addr1, sizeof(struct sockaddr_in)) == 0)
printf("Binded Correctly\n");
else
printf("Unable to bind\n");
socklen_t addr_size = sizeof my_addr;
int con = connect(client, (struct sockaddr*) &my_addr, sizeof my_addr);
if (con == 0)
printf("Client Connected\n");
else
printf("Error in Connection\n");
strcpy(buffer2, "Hello");
send(client, buffer2, 256, 0);
recv(client, buffer1, 256, 0);
printf("Server : %s\n", buffer1);
return 0;
}
输出:
Client Created
Binded Correctly
Client Connected
Server : Hello
参考:https://stackoverflow.com/questions/4118241/what-client-side-situations-need-bind
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。