头文件无法打开,请指教!
--------------------Configuration: udp1 - Win32 Debug--------------------Compiling...
udp1.cpp
e:\软件编程系列\c++源代码\网络编程课程实例\udp1.cpp(1) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
Error executing cl.exe.
udp1.obj - 1 error(s), 0 warning(s)
小弟在编译时返回这个错误,很是不明白问题出在哪.
特意查找了这个头文件,发现这个头文件分别存在于:
G:\Visual C++ 6.0\VC98\ATL\SRC
G:\Visual C++ 6.0\VC98\MFC\SRC
G:\Visual C++ 6.0\VFP98\API\SAMPLES\FOXTLIB
不知道是不是新建文件时出现错误,如果是,那具体操作的步骤应该是什么?
下面是源代码,是一个基于Winsock API的UDP小程序.
#include "stdafx.h"
#include <Winsock.h>
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
///////////////////////////////////////////////////////////////////////
int APIENTRY WinMain(HINSTANCE hI,HINSTANCE hP,LPSTR lp,int nC)
{
MSG msg;
HWND hWnd;
//实现句柄存入全局量hInst
hInst=hI;
WNDCLASS wc;
memset(&wc,0,sizeof(WNDCLASS));
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.hInstance =hI;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hbrBackground =(HBRUSH)COLOR_WINDOW;
wc.lpszClassName ="W1";
RegisterClass(&wc);
hWnd=CreateWindowEx(WS_EX_PALETTEWINDOW,"W1","UDP程序1[端口:8000]",
WS_OVERLAPPEDWINDOW,
100,100,200,100,
NULL,NULL,hI,NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nC);
//主消息循环
while(GetMassage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
////////////////////////////////////////////////////////////////////////
//定义Winsock API需要的量
WSADATA ws;
SOCKET s1,s2;
struct sockaddr_in Cs1A,Cs2A;
//发送数据缓冲
char sendBuf[200];
//接收数据缓冲
char recvBuf[200];
int len; //存放接收到的字节数
#define Cs1Port 8000 //本地端口
#define Cs2Port 6000 //远程端口
#define Cs1IP "127.0.0.1" //本地IP
#define Cs2IP "127.0.0.1" //远程IP
///////////////////////////////////////////////////////////////////////
//消息处理
LRESULT CALLBACK WndProc(HWND hW,UNIT msg,WPARAM wP,LPARAM lP)
{
switch(msg)
{
case WM_DESTROY:
WSAAsyncSelect(s1,hW,0,0);
closesocket(s1);
WSACleanup();
PostQuitMessage(0);
break;
case WM_CREATE:
WSAStartup(0x0202,&ws);
s1=socket(AF_INET,SOCK_DGRAM,0);
Cs1A.sin_family=AF_INET;
Cs1A.sin_port=htons(Cs1Port);
Cs1A.sin_addr.s_addr=inet_addr(Cs1IP);
len=bind(s1,(struct sockaddr*)&Cs1A,sizeof(Cs1A));
Cs2A.sin_family=AF_INET;
Cs2A.sin_port=htons(Cs2Port);
Cs2A.sin_addr.s_addr=inet_addr(Cs2IP);
WSAAsyncSelect(s1,hW,WM_USER+1,FD_READ);
Beep(2000,200);
break;
case WM_USER+1:
switch(LOWORD(lP))
{
case FD_READ:
len=recv(s1,recvBuf,sizeof(recvBuf),0);
recvBuf[len]=0;
MessageBox(0,recvBuf,"UDP程序1从UDP程序2收到的数据",MB_OK);
break;
}
break;
case WM_LBUTTONDOWN:
wsprintf(sendBuf,"Hello,UDP程序2!");
len=sizeof(Cs2A);
sendto(s1,sendBuf,strlen(sendBuf),0,(struct sockaddr*)&Cs2A,len);
break;
}
return DefWindowProc(hW,msg,wP,lP);
}