gpt4 book ai didi

c - 在winsock中阻止相同的地址

转载 作者:行者123 更新时间:2023-11-30 15:39:44 25 4
gpt4 key购买 nike

我想使用Winsock构建一个没有服务器的聊天应用程序(我不知道叫什么,也许是点对点聊天)。

我刚刚从这两个源代码中了解到:

//==================================================== file = udpServer.c =====
//= A message "server" program to demonstrate sockets programming =
//=============================================================================
//= Notes: =
//= 1) This program conditionally compiles for Winsock and BSD sockets. =
//= Set the initial #define to WIN or BSD as appropriate. =
//= 2) This program serves a message to program udpClient running on =
//= another host. =
//= 3) The steps #'s correspond to lecture topics. =
//=---------------------------------------------------------------------------=
//= Example execution: (udpServer and udpClient running on host 127.0.0.1) =
//= Waiting for recvfrom() to complete... =
//= IP address of client = 127.0.0.1 port = 55476) =
//= Received from client: Test message from CLIENT to SERVER =
//=---------------------------------------------------------------------------=
//= Build: bcc32 udpServer.c or cl udpServer.c wsock32.lib for Winsock =
//= gcc udpServer.c -lsocket -lnsl for BSD =
//=---------------------------------------------------------------------------=
//= Execute: udpServer =
//=---------------------------------------------------------------------------=
//= Author: Ken Christensen =
//= University of South Florida =
//= WWW: http://www.csee.usf.edu/~christen =
//= Email: christen@csee.usf.edu =
//=---------------------------------------------------------------------------=
//= History: KJC (08/02/08) - Genesis (from server1.c) =
//= KJC (09/07/09) - Minor clean-up =
//= KJC (09/22/13) - Minor clean-up to fix warnings =
//=============================================================================
#define WIN // WIN for Winsock and BSD for BSD sockets

//----- Include files --------------------------------------------------------
#include <stdio.h> // Needed for printf()
#include <string.h> // Needed for memcpy() and strcpy()
#include <stdlib.h> // Needed for exit()
#ifdef WIN
#include <windows.h> // Needed for all Winsock stuff
#endif
#ifdef BSD
#include <sys/types.h> // Needed for sockets stuff
#include <netinet/in.h> // Needed for sockets stuff
#include <sys/socket.h> // Needed for sockets stuff
#include <arpa/inet.h> // Needed for sockets stuff
#include <fcntl.h> // Needed for sockets stuff
#include <netdb.h> // Needed for sockets stuff
#endif

//----- Defines --------------------------------------------------------------
#define PORT_NUM 1050 // Arbitrary port number for the server

//===== Main program =========================================================
int main()
{
#ifdef WIN
WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions
WSADATA wsaData; // Stuff for WSA functions
#endif
int server_s; // Server socket descriptor
struct sockaddr_in server_addr; // Server Internet address
struct sockaddr_in client_addr; // Client Internet address
struct in_addr client_ip_addr; // Client IP address
int addr_len; // Internet address length
char out_buf[4096]; // Output buffer for data
char in_buf[4096]; // Input buffer for data
int retcode; // Return code

#ifdef WIN
// This stuff initializes winsock
WSAStartup(wVersionRequested, &wsaData);
#endif

// >>> Step #1 <<<
// Create a socket
// - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
server_s = socket(AF_INET, SOCK_DGRAM, 0);
if (server_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}

// >>> Step #2 <<<
// Fill-in my socket's address information
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port number to use
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address
retcode = bind(server_s, (struct sockaddr *)&server_addr,
sizeof(server_addr));
if (retcode < 0)
{
printf("*** ERROR - bind() failed \n");
exit(-1);
}

// >>> Step #3 <<<
// Wait to receive a message from client
printf("Waiting for recvfrom() to complete... \n");
addr_len = sizeof(client_addr);
retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0,
(struct sockaddr *)&client_addr, &addr_len);
if (retcode < 0)
{
printf("*** ERROR - recvfrom() failed \n");
exit(-1);
}

// Copy the four-byte client IP address into an IP address structure
memcpy(&client_ip_addr, &client_addr.sin_addr.s_addr, 4);

// Print an informational message of IP address and port of the client
printf("IP address of client = %s port = %d) \n", inet_ntoa(client_ip_addr),
ntohs(client_addr.sin_port));

// Output the received message
printf("Received from client: %s \n", in_buf);

// >>> Step #4 <<<
// Send to the client using the server socket
strcpy(out_buf, "This is a reply message from SERVER to CLIENT");
retcode = sendto(server_s, out_buf, (strlen(out_buf) + 1), 0,
(struct sockaddr *)&client_addr, sizeof(client_addr));
if (retcode < 0)
{
printf("*** ERROR - sendto() failed \n");
exit(-1);
}

// >>> Step #5 <<<
// Close all open sockets
#ifdef WIN
retcode = closesocket(server_s);
if (retcode < 0)
{
printf("*** ERROR - closesocket() failed \n");
exit(-1);
}
#endif
#ifdef BSD
retcode = close(server_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);
}
#endif

#ifdef WIN
// This stuff cleans-up winsock
WSACleanup();
#endif

// Return zero and terminate
return(0);
}

//=========================================== file = udpClientBroadcast.c =====
//= A message "client" program to demonstrate sockets programming =
//= - This is udpClient.c modified to use broadcast =
//=============================================================================
//= Notes: =
//= 1) This program conditionally compiles for Winsock and BSD sockets. =
//= Set the initial #define to WIN or BSD as appropriate. =
//= 2) This program needs udpServer to be running on another host. =
//= Program udpServer must be started first. =
//=---------------------------------------------------------------------------=
//= Example execution: (udpServer and udpClientBroadcast on host 127.0.0.1) =
//= Received from server: This is a reply message from SERVER to CLIENT =
//=---------------------------------------------------------------------------=
//= Build: Windows: Borland: bcc32 udpClientBroadcast.c =
//= Visual C cmd line: cl udpClientBroadcast.c wsock32.lib =
//= MinGW: gcc udpClientBroadcast.c -lws2_32 =
//= Unix: gcc udpClientBroadcast.c -lnsl -o udpClientBroadcast =
//=---------------------------------------------------------------------------=
//= Execute: udpClientBroadcast =
//=---------------------------------------------------------------------------=
//= Author: Ken Christensen =
//= University of South Florida =
//= WWW: http://www.csee.usf.edu/~christen =
//= Email: christen@csee.usf.edu =
//=---------------------------------------------------------------------------=
//= History: KJC (04/10/10) - Genesis (from udpClient.c) =
//=============================================================================
#define WIN // WIN for Winsock and BSD for BSD sockets

//----- Include files ---------------------------------------------------------
#include <stdio.h> // Needed for printf()
#include <string.h> // Needed for memcpy() and strcpy()
#ifdef WIN
#include <windows.h> // Needed for all Winsock stuff
#endif
#ifdef BSD
#include <sys/types.h> // Needed for sockets stuff
#include <netinet/in.h> // Needed for sockets stuff
#include <sys/socket.h> // Needed for sockets stuff
#include <arpa/inet.h> // Needed for sockets stuff
#include <fcntl.h> // Needed for sockets stuff
#include <netdb.h> // Needed for sockets stuff
#endif

//----- Defines ---------------------------------------------------------------
#define PORT_NUM 1050 // Port number used

//===== Main program ==========================================================
void main(void)
{
#ifdef WIN
WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions
WSADATA wsaData; // Stuff for WSA functions
#endif
int client_s; // Client socket descriptor
struct sockaddr_in server_addr; // Server Internet address
int addr_len; // Internet address length
char out_buf[4096]; // Output buffer for data
char in_buf[4096]; // Input buffer for data
int retcode; // Return code
int iOptVal; // Socket option value
int iOptLen; // Socket option length

#ifdef WIN
// This stuff initializes winsock
WSAStartup(wVersionRequested, &wsaData);
#endif

// Create a socket
client_s = socket(AF_INET, SOCK_DGRAM, 0);
if (client_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}

// Fill-in server socket's address information
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port num to use
server_addr.sin_addr.s_addr = inet_addr(INADDR_ANY); // Need this for Broadcast

// Set socket to use MAC-level broadcast
iOptVal = 1;
iOptLen = sizeof(int);
setsockopt(client_s, SOL_SOCKET, SO_BROADCAST, (char*)&iOptVal, iOptLen);

// Assign a message to buffer out_buf
strcpy(out_buf, "Test message from CLIENT to SERVER");

// Now send the message to server.
retcode = sendto(client_s, out_buf, (strlen(out_buf) + 1), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (retcode < 0)
{
printf("*** ERROR - sendto() failed \n");
exit(-1);
}

// Wait to receive a message
addr_len = sizeof(server_addr);
retcode = recvfrom(client_s, in_buf, sizeof(in_buf), 0,
(struct sockaddr *)&server_addr, &addr_len);
if (retcode < 0)
{
printf("*** ERROR - recvfrom() failed \n");
exit(-1);
}

// Output the received message
printf("Received from server: %s \n", in_buf);

// Close all open sockets
#ifdef WIN
retcode = closesocket(client_s);
if (retcode < 0)
{
printf("*** ERROR - closesocket() failed \n");
exit(-1);
}
#endif
#ifdef BSD
retcode = close(client_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);
}
#endif

#ifdef WIN
// This stuff cleans-up winsock
WSACleanup();
#endif
}

这两个代码对我来说效果很好,但是这是 2 个应用程序,我只想要 1 个应用程序既可以充当服务器又可以充当客户端。

因此,我使用线程将这两个源代码合并为一个。

程序再次运行,但监听器套接字接受来自广播消息的连接。

我想要的是监听器阻止来自环回地址的传入连接。

如何做到这一点?

编辑:

示例我有2台计算机,一台是A(192.168.1.100),另一台是B(192.168.1.101),当我运行程序时,计算机A接收来自自身的消息,我想要的是A只接收除答:

编辑2014年1月27日:

经过几天的搜索,我认为我无法找到问题的解决方案。
但是,还有另一种方法可以使其成为可能。

第一步是获取您的本地计算机 IP 地址。

第二步是在发送消息之前构建消息,例如格式为“ipAddress_messageToSend”。

第三步创建函数来处理来自recvfrom()函数的传入消息,该函数将按“_”分割传入消息
这样您将拥有二维数组,第一行是该消息来自的 IP 地址,第二行是消息本身。

第四步,您必须比较传入消息的 IP 地址和您计算机的 IP 地址。
您可以使用下面的代码来获取 IP 地址。

char ac[80];
gethostname(ac, sizeof(ac));
struct hostent *phe = gethostbyname(ac);
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
printf("IP Address is : %s\n", inet_ntoa(addr));

第五步当比较IP并且它们相同时不执行任何操作,如果不是则执行您想做的操作。

希望对大家有帮助

最佳答案

根据您对评论的回复,问题在于您的服务器正在从同一节点中运行的客户端接收消息。你希望服务器过滤掉那些自己发出的消息。

为了实现服务器需要:

1-开始之前,服务器会检测自己的接口(interface)和 IP 地址。 Winsock应该有一个像getifaddrs这样的函数。这里有一个与Windows相关的类似问题的链接:Get the IP Address of local computer您可以在列表或任何其他数据结构中添加本地地址列表。

对于 UNIX,这是一个类似的问题: Lazarus: How to list all the available network connection on a system?

2-每当收到消息时,在recvfrom之后,服务器将client_addr.sin_addr.s_addr与自己的地址列表进行比较,以检测自己的消息。然后过滤掉这些消息。

我认为不可能以不同的方式、以简单的方式过滤掉来自自身的消息。在Linux中你可以使用iptables过滤掉来自自身的消息,我不知道Windows是否有类似的机制。

其他建议:1-您应该在聊天应用程序中使用多播而不是广播。那么不运行聊天应用程序的系统就不会受到聊天消息的影响。

2-您可以使用确保每个对等方都可以读取所有消息的方法来补充多播或广播机制。广播和多播消息可能会被对等方丢失。有多种方法可以实现它。但这是解决这个问题后的下一步。

关于c - 在winsock中阻止相同的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com