2014-01-23 46 views
0

我想使用Winsock構建一個沒有服務器的聊天應用程序(我不知道是什麼,也許它是點對點聊天)。在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: [email protected]          = 
//=---------------------------------------------------------------------------= 
//= 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: [email protected]          = 
//=---------------------------------------------------------------------------= 
//= 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.

編輯27/01/2014:搜索後,我想我是無法找到解決我的問題幾天


但是,有一種替代方法可以實現。

第一步是獲取本地計算機的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相比,他們是同一什麼也不做,如果沒有的話你想做的事情。

希望這會幫助你所有

+0

問題:你如何知道你從環回接收?只是澄清:是否有可能你想要的服務器不接收自己的消息? – rodolk

+0

對不起,我錯了,它不是回送,但它收到從本身播放的消息。是的,那就是,如何讓服務器拒絕來自自己的消息 – rizkynggakool

+0

如果你想得到答案,請修改你的問題和你的頭銜。目前沒有任何意義。 – EJP

回答

1

根據您迴應的意見,問題是,你的服務器在同一節點上運行的客戶端接收消息。您希望服務器過濾掉自己發送的消息。

爲了實現這些服務器需要:

1開始之前,服務器檢測到自身的接口和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-你可以通過一種方法來補充多播或廣播機制,這種方法可以確保每個對等方都可以讀取所有消息。廣播和多播消息可能會被同伴丟失。有多種方式來實現它。但是,一旦你解決這個問題,這就是下一步。

+0

非常感謝你,你真的幫助。這就是我真正想要的 – rizkynggakool