我正在尝试学习一些WinSock并坚持到位。我想将RAW数据包(256字节)发送到网络中的另一台设备。这个设备只是一个听众。我正在使用VC ++ 2010和WinSock。
所以有一个我正在处理的代码:
// winsock.cpp : main project file.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
//#include <WinSock2.h>
#define WIN32_LEAN_AND_MEAN
using namespace System;
int main(array<System::String ^> ^args)
{
WORD RequiredVersion;
WSADATA WData;
SOCKET s;
struct sockaddr_in addr;
long val;
struct hostent *he;
char host[128];
int port;
printf("HOST: ");
scanf("%s",host);
printf("PORT: ");
scanf("%d",&port);
printf("Data to send: ");
scanf("%d",&val);
RequiredVersion = MAKEWORD(2, 0);
if (WSAStartup(RequiredVersion, &WData) != 0) {
printf("ERROR! WinSock2\n");
return 1;
}
//he = gethostbyaddr((char *) host, 4, AF_INET);
he = gethostbyname(host);
if (he == NULL) {
printf("host not found.\n");
system("PAUSE");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *((unsigned long*) he->h_addr);
s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
//SOCK_RAW, IPPROTO_TCP);
connect(s, (struct sockaddr*) &addr, sizeof(addr));
printf("SENDING %d TO %s\n",val,inet_ntoa(addr.sin_addr));
val = htonl(val);
send(s, (char*) &val, sizeof(long), 0);
printf("WAITING FOR RESPONSE...\n");
recv(s, (char*) &val, sizeof(long), 0);
val = ntohl(val);
printf("RE: %d\n", val);
closesocket(s);
WSACleanup();
system("PAUSE");
return 0;
}
我遇到的问题:
任何提示?非常感谢您提供的任何帮助!