主题:VC的应用
下面有两个程序
发送程序:
#pragma comment(lib,"ws2_32.lib")
#include <STDIO.H>
#include <WINSOCK2.H>
#define OFFLINE_HOST "192.168.1.100"
#define DATANUM 6
void HandleError(char*);
int main(int argc,char *argv[])
{
WSAData wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);
SOCKET sock=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in to;
memset(&to,0,sizeof(to));
to.sin_addr.s_addr=inet_addr(OFFLINE_HOST);
to.sin_family=AF_INET;
to.sin_port=htons(9999);
char *buf="Hello!";
int res=sendto(sock,buf,DATANUM,0,(struct sockaddr*)&to,sizeof(to));
if(res==SOCKET_ERROR){
HandleError("sendto");
}
else
printf("Send out %d bytes!\n",res);
closesocket(sock);
WSACleanup();
return 0;
}
void HandleError(char *func)
{
int errCode=WSAGetLastError();
char info[65]={0};
_snprintf(info,64,"%s: %d\n",func,errCode);
printf(info);
}
接收程序:
#pragma comment(lib,"ws2_32.lib")
#include<STDIO.H>
#include<WINSOCK2.H>
int main()
{
WSAData wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);
SOCKET sock=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
int len=sizeof(addr);
addr. sin_addr.s_addr=INADDR_ANY;
addr.sin_family=AF_INET;
addr.sin_port=htons(9999);
if(bind(sock,(struct sockaddr*)&addr,len)==SOCKET_ERROR){
printf("bind:%d",WSAGetLastError());
}
memset(&addr,0,sizeof(addr));
char buf[120]={0};
int ret=recvfrom(sock,buf,120,0,(struct sockaddr*)&addr,&len);
if(ret==SOCKET_ERROR){
printf("recv:%d\n",WSAGetLastError());
}
else
printf("recv %d byte from %s!\n",ret,inet_ntoa(addr.sin_addr));
closesocket(sock);
WSACleanup();
return 0;
}
怎么在VC上运行????
发送程序:
#pragma comment(lib,"ws2_32.lib")
#include <STDIO.H>
#include <WINSOCK2.H>
#define OFFLINE_HOST "192.168.1.100"
#define DATANUM 6
void HandleError(char*);
int main(int argc,char *argv[])
{
WSAData wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);
SOCKET sock=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in to;
memset(&to,0,sizeof(to));
to.sin_addr.s_addr=inet_addr(OFFLINE_HOST);
to.sin_family=AF_INET;
to.sin_port=htons(9999);
char *buf="Hello!";
int res=sendto(sock,buf,DATANUM,0,(struct sockaddr*)&to,sizeof(to));
if(res==SOCKET_ERROR){
HandleError("sendto");
}
else
printf("Send out %d bytes!\n",res);
closesocket(sock);
WSACleanup();
return 0;
}
void HandleError(char *func)
{
int errCode=WSAGetLastError();
char info[65]={0};
_snprintf(info,64,"%s: %d\n",func,errCode);
printf(info);
}
接收程序:
#pragma comment(lib,"ws2_32.lib")
#include<STDIO.H>
#include<WINSOCK2.H>
int main()
{
WSAData wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);
SOCKET sock=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in addr;
int len=sizeof(addr);
addr. sin_addr.s_addr=INADDR_ANY;
addr.sin_family=AF_INET;
addr.sin_port=htons(9999);
if(bind(sock,(struct sockaddr*)&addr,len)==SOCKET_ERROR){
printf("bind:%d",WSAGetLastError());
}
memset(&addr,0,sizeof(addr));
char buf[120]={0};
int ret=recvfrom(sock,buf,120,0,(struct sockaddr*)&addr,&len);
if(ret==SOCKET_ERROR){
printf("recv:%d\n",WSAGetLastError());
}
else
printf("recv %d byte from %s!\n",ret,inet_ntoa(addr.sin_addr));
closesocket(sock);
WSACleanup();
return 0;
}
怎么在VC上运行????