所以我一直在這個指南
http://www.binarytides.com/winsock-socket-programming-tutorial/
要建立在C一個簡單的TCP服務器下面,這是我想出了代碼:
https://pastebin.com/CDxiLv3b
的Winsock只接受來自同一臺計算機的連接,但不能從同一網絡
int c;
char client_message[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Server Socket created.\n");
memset(&server_addr, 0, sizeof(server_addr));
//Prepare the sockaddr_in structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
//Bind
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
}
printf("Bind to port %d done.\n", port);
//Listen to incoming connections
listen(server_socket, 3);
//Accept and incoming connection
puts("Waiting for incoming reverse shell...");
c = sizeof(struct sockaddr_in);
while ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &c)) != INVALID_SOCKET)
{
puts("Connection accepted");
if ((recv_size = recv(client_socket, client_message, 2000, 0)) == SOCKET_ERROR)
{
puts("recv failed");
break;
}
client_message[recv_size] = '\0';
strcpy_s(path, MAX_PATH_LEN, client_message);
break;
}
if (client_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d", WSAGetLastError());
return 1;
}
return 0;
(是的,我basicly複製和改變的東西)
現在的問題是,我只能連接到這臺服務器使用相同的連接,我不能使用網絡中的不同計算機連接到它,即使我打開端口(實際上應該不重要),它不會接受來自我網絡上不同計算機的連接(僅來自同一臺計算機)
定義'不起作用'。 – EJP
什麼接口(IP)是程序監聽(你可以用netstat檢查)以及你使用哪個IP從另一臺機器連接? – Juan
將無法正常工作意味着它不會接受來自網絡中其他計算機的連接我現在將測試它(ip列表的東西),並在我的本地計算機(它工作的地方)和我網絡中的其他計算機上使用我的本地ip (10.0.0.14) – NacOJerk