服务器 - 发送到这个地方
客户 - 当前本地
我运行带有微小更改代码的镜像主机和客户端。
您好,我有这个代码问题是我在主机端添加
server.sin_family = AF_INET;
server.sin_port = htons(27016);
server.sin_addr.s_addr = inet_addr("169.254.57.24");
client.sin_family = AF_INET;
client.sin_port = htons(27015);
client.sin_addr.s_addr =inet_addr("127.0.0.1");
和客户端的代码
server.sin_family = AF_INET;
server.sin_port = htons(27015);
server.sin_addr.s_addr = inet_addr("169.254.200.200");
client.sin_family = AF_INET;
client.sin_port = htons(27016);
client.sin_addr.s_addr =INADDR_ANY;
由于WSAGetLastError()== WSAECONNRESET在客户端为TRUE,所以数据包不会在双方都收到,但是对于逻辑,它必须工作,因为我在使用continue时(WSAGetLastError()== WSAECONNRESET)当我们无法发送时它只是忽略在客户端到服务器上。因此,当我在客户端使用sendto时,数据包无法传递WSAECONNRESET成长并且我忽略了这个错误,但问题是我无法接收主机数据包。 RECFROM始终捕获错误。但是,如果我改变client.sin_addr.s_addr = INADDR_ANY;在主机方面,它将在两个方向上工作。当我向不存在的套接字发送数据包时,为什么我无法向一个方向发送packe。
#include "stdafx.h"
#include <iostream>
#pragma comment(lib, "WS2_32.lib")
using namespace std;
#define DEFAULT_BUFLEN 256
sig_atomic_t volatile stop = 0;
void sig_handler(int signum);
DWORD WINAPI handle_request(LPVOID param);
// holds data from the client
char send_buffer[DEFAULT_BUFLEN] = "TEST MESSAGE CLIENT";
char receive_buffer[DEFAULT_BUFLEN] = "";
int server_length;
// sockets which listens for a client
// and socket which represents the client
SOCKET server_socket = INVALID_SOCKET;
SOCKET client_socket = INVALID_SOCKET;
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsa_data;
HRESULT result;
HANDLE hThread;
// register signal SIGINT and signal handler
signal(SIGINT, sig_handler);
// initializing sockets
// it has two main purposes
// 1. specify which version you want (2.2)
// 2. load the winsock dll into your process
result = WSAStartup(MAKEWORD(2,2), &wsa_data);
if(result != 0) {
printf("WSAStartup error: %d.\n", result);
return 1;
}
///////////////////// send socket START
/* Clear out server struct */
memset((void *)&server, 0 , sizeof(struct sockaddr_in));
/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(27016);//27016
server.sin_addr.s_addr = inet_addr("169.254.57.24"); //INADDR_ANY; // inet_addr("127.0.0.1");
///////////////////// send socket END
//////////////////// recv socket START
/* Clear out server struct */
memset((void *)&client, 0 , sizeof(struct sockaddr_in));
/* Open a datagram socket */
client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (client_socket == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
/* Set family and port */
client.sin_family = AF_INET;
client.sin_port = htons(27015);
client.sin_addr.s_addr =inet_addr("127.0.0.1");//inet_addr("127.0.0.1");///INADDR_ANY;
/* Bind address to socket */
if (bind(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(client_socket);
WSACleanup();
exit(0);
}
///////////////////// recv socket END
hThread = (HANDLE) _beginthreadex ( NULL, 0, reinterpret_cast<unsigned int (__stdcall *)(void *)>(handle_request), (void*)send_buffer, 0, NULL);
if (!hThread)
{
std::cout << "Error at CreateThread(): " << GetLastError() << std::endl;
closesocket(client_socket);
WSACleanup();
return 0;
}
server_length = sizeof(struct sockaddr_in);
while(!stop) {
if (sendto(client_socket, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Error transmitting data.\n");
Sleep(1000);
continue;
closesocket(client_socket);
WSACleanup();
exit(0);
}
static int c = 0;
send_buffer[0] = char(++c);
send_buffer[1] = 'A';
send_buffer[2] = '\0';
std::cout << "SEND NUMBER: "<<(int)send_buffer[0] << send_buffer[1] << std::endl;
Sleep(1000);
}
WaitForSingleObject(hThread, INFINITE);
closesocket(client_socket);
CloseHandle(hThread);
WSACleanup();
return 0;
}
void sig_handler(int signum){
if (signum == SIGINT){
stop = 1;
printf("Shutting down the server!\n");
}
}
DWORD WINAPI handle_request(LPVOID param)
{
/* Receive time */
int iResult;
do
{
iResult =
// 1c recvfrom ( client_socket, (char *)&receive_audio, (int)sizeof(receive_audio), 0, 0, 0); //(struct sockaddr *)&server, &server_length
recvfrom ( client_socket, (char *)&receive_buffer, (int)sizeof(receive_buffer), 0, 0, 0);
if (iResult <= 0)
{
if (WSAGetLastError()==WSAECONNRESET)
continue;
printf("Error when connecting! %d\n",WSAGetLastError());
// fprintf(stderr, "Error receiving data.\n");
Sleep(1000);
continue;
closesocket(client_socket);
WSACleanup();
exit(0);
}
/* receive_buffer[iResult] = '\0';
std::cout << "Receive: " << receive_audio << std::endl;
Sleep(1000); */
std::cout << "Receive: " << (int)receive_buffer[0] << receive_buffer[1] << std::endl;
Sleep(1000);
Sleep(100);
}
while (!stop);
return 0;
}