作者:隐姓埋名er | 来源:互联网 | 2023-08-20 13:44
WSASend(....); 参数太多不懂调用
比如我要给192.168.1.1的10000端口发送一个封包 数据为1234 请问大家该如何写 给我源代码好琢磨
12 个解决方案
不懂怎么建立套接字 就是第一个参数不懂 网上的例子都没见到完整的
MSDN的例子
#include
#include
#include
#include
#define DATA_BUFSIZE 4096
#define SEND_COUNT 10
void __cdecl main()
{
WSADATA wsd;
struct addrinfo *result = NULL,
hints = {0};
WSAOVERLAPPED SendOverlapped = {0};
SOCKET ListenSocket = INVALID_SOCKET,
AcceptSocket = INVALID_SOCKET;
WSABUF DataBuf;
DWORD SendBytes, Flags;
char buffer[DATA_BUFSIZE];
int err, rc, i;
// Load Winsock
rc = WSAStartup(MAKEWORD(2,2), &wsd);
if (rc != 0) {
fprintf(stderr, "Unable to load Winsock: %d\n", rc);
return;
}
// Initialize the hints to obtain the
// wildcard bind address for IPv4
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
rc = getaddrinfo(NULL, "27015", &hints, &result);
if (rc != 0) {
fprintf(stderr, "getaddrinfo failed: %d\n", rc );
return;
}
ListenSocket = socket(result->ai_family,
result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
fprintf(stderr, "socket failed: %d\n",
WSAGetLastError());
freeaddrinfo(result);
return;
}
rc = bind(ListenSocket, result->ai_addr,
(int)result->ai_addrlen);
if (rc == SOCKET_ERROR) {
fprintf(stderr, "bind failed: %d\n",
WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
return;
}
rc = listen(ListenSocket, 1);
if (rc == SOCKET_ERROR) {
fprintf(stderr, "listen failed: %d\n",
WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
return;
}
// Accept an incoming connection request
AcceptSocket = accept(ListenSocket, NULL, NULL);
if (AcceptSocket == INVALID_SOCKET) {
fprintf(stderr, "accept failed: %d\n",
WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
return;
}
printf("Client Accepted...\n");
// Create an event handle and setup an overlapped structure.
SendOverlapped.hEvent = WSACreateEvent();
if (SendOverlapped.hEvent == NULL) {
fprintf(stderr, "WSACreateEvent failed: %d\n",
WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
closesocket(AcceptSocket);
return;
}
DataBuf.len = DATA_BUFSIZE;
DataBuf.buf = buffer;
for(i=0; i < SEND_COUNT ;i++) {
rc = WSASend(AcceptSocket, &DataBuf, 1,
&SendBytes, 0, &SendOverlapped, NULL);
if ( (rc == SOCKET_ERROR) &&
(WSA_IO_PENDING != (err = WSAGetLastError()))) {
fprintf(stderr, "WSASend failed: %d\n", err);
break;
}
rc = WSAWaitForMultipleEvents(1, &SendOverlapped.hEvent, TRUE, INFINITE, TRUE);
if (rc == WSA_WAIT_FAILED) {
fprintf(stderr, "WSAWaitForMultipleEvents failed: %d\n", WSAGetLastError());
break;
}
rc = WSAGetOverlappedResult(AcceptSocket, &SendOverlapped, &SendBytes, FALSE, &Flags);
if (rc == FALSE) {
fprintf(stderr, "WSASend operation failed: %d\n", WSAGetLastError());
break;
}
printf("Wrote %d bytes\n", SendBytes);
WSAResetEvent(SendOverlapped.hEvent);
}
WSACloseEvent(SendOverlapped.hEvent);
closesocket(AcceptSocket);
closesocket(ListenSocket);
freeaddrinfo(result);
WSACleanup();
return;
}
先找本书来看,把sockets的知识系统的学一遍,比如"windows网络编程"
看這裡:
http://topic.csdn.net/t/20021128/19/1214199.html