即使我知道正在监听,也拒绝连接到localhost:30000

时间:2019-08-26 04:27:40

标签: c sockets winsock2

我正在使用C和Winsock2设置一个简单的服务器和客户端应用程序。我设法使它一天能够工作,但是第二天,它一直给我报错10061(连接被拒绝)。我没有更改我的安全设置或我认为相关的任何内容。

我可以使用Web浏览器通过在地址栏中键入localhost:30000连接到服务器。我仔细检查了代码,将其与Winsock官方教程进行了比较,但似乎无济于事。

由于服务器似乎可以正常工作,因此以下是客户端代码,直到失败为止:

#if defined __WIN32__ || defined _WIN32 || defined WIN32
// define WINVER for MinGW
#ifndef WINVER
#define WINVER 0x0501
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
//(this will be for Linux code that I haven't implemented yet)
#endif
#include <stdio.h>
#include <sys/types.h>
#include <process.h>
#define PORTNUM "30000"
#define BUFLEN 512

int main(int argc, char *argv[]) {
    WSADATA wsaData;
    // Create new SOCKET object
    SOCKET ConnectSocket = INVALID_SOCKET;
    // Create socket for client
    struct addrinfo *result = NULL,
                    *ptr = NULL,
                    hints;
    // Initialize Winsock
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        fprintf(stderr, "WSAStartup failed: %d\n", iResult);
        exit(1);
    }

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], PORTNUM, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    // Attempt to connect to the first address returned by
    // the call to getaddrinfo
    ptr=result;
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
        ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Error at socket(): %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Connect to server.
    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("Error at connect(): %ld\n", WSAGetLastError());
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        scanf("%s", NULL);
    }
// ...
}

1 个答案:

答案 0 :(得分:1)

我不确定这是否可以解决您的问题,但请在以下部分进行一些更改:

Dim mysel As Range

Dim LastEl As Long
With ThisWorkbook
    Set wsRaw = .Worksheets("RAW")
End With

wsRaw.Activate
LastEl = wsRaw.Cells.SpecialCells(xlLastCell).row
Do Until LastEl = 0
    If WorksheetFunction.CountA(Rows(LastEl)) = 0 Then

        If mysel Is Nothing Then
            Set mysel = Rows(LastEl)
        Else
            Set mysel = Union(mysel, Rows(LastEl))
        End If

    End If
    LastEl = LastEl - 1
Loop


mysel.Delete xlUp

我认为这可能是原因,因为我在MSDN中找到了这句话:

  

当前支持的值为import time p_s = 100 e_s = 100 px = 0 py = 0 ex = 0 ey = 0 while True: pupc = px + p_s / 2 pdownc = px - p_s / 2 pupcy = py + p_s / 2 pdowncy = py - p_s / 2 if ex == px or ex > px and ex < pupc or ex == px or ex < px and ex > pdownc: if ey == py or ey > py and ey < pupcy or ey == py or ey < py and ey > pdowncy: pec = 1 else: pec = 0 else: pec = 0 while True: if pec == 1: print('collision') else: print('nope') ZeroMemory( &hints, sizeof(hints) ); hints.ai_family = AF_INET; // <-- Here hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; ,它们是   AF_INETAF_INET6的Internet地址族格式。其他选择   地址族(例如,用于NetBIOS的AF_NETBIOS)是   如果地址族的Windows套接字服务提供者支持   已安装[link]

也许这部分会引起您的问题。