memcpy(),ZeroMemory();怎么用:
编译什么的都没有问题 就是怎么也连接不上,不知道有什么错误 请高手帮忙服务器程序:#include <stdio.h>
#include <winsock.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "wsock32.lib")
DWORD IniSOCKDLL()
{
WSADATA wsaData;
int err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
return err;
}
int main(int argc, char* argv[])
{
IniSOCKDLL();
SOCKET sc=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
const int ClientPort=5100;
const char clientAddr[]="219.225.249.157";
sockaddr_in Client;
Client.sin_family=AF_INET;
Client.sin_port = htons(ClientPort);
Client.sin_addr.s_addr=inet_addr(clientAddr);
char buf[1024];
ZeroMemory(buf,1024);
int x=sizeof(Client);
recvfrom(sc,buf,1024,0,(sockaddr*)&Client,&x);
sendto(sc,buf,1024,0,(sockaddr*)&Client,sizeof(Client));
memcpy(buf,"123",3);
cout<<buf<<endl;
closesocket(sc);
return 0;
}
客户端程序:#include "stdio.h"
#include <winsock.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "wsock32.lib")
DWORD IniSOCKDLL()
{
WORD wVersionRequested;
WSADATA wsaData;
int err=0;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
return err;
}
int main(int argc, char* argv[])
{
cout<<"程序开始:"<<endl;
IniSOCKDLL();
SOCKET ss=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
const int ServerPort=69;
const char serverAddr[]="219.225.249.157";
sockaddr_in Server;
Server.sin_family=AF_INET;
Server.sin_port = htons(ServerPort);
Server.sin_addr.s_addr=inet_addr(serverAddr);
const int ClientPort=5100;
const char clientAddr[]="219.225.249.157";
sockaddr_in Client;
Client.sin_family=AF_INET;
Client.sin_port = htons(ClientPort);
Client.sin_addr.s_addr=inet_addr(clientAddr);
bind(ss,(PSOCKADDR)&Client,sizeof(Client));
char buf[1024];
memcpy(buf,"1234",4);
int x=sizeof(Server);
sendto(ss,buf,1024,0,(sockaddr*)&Server,sizeof(Server));
recvfrom(ss,buf,1024,0,(sockaddr*)&Server,&x);
ZeroMemory(buf,1024);
cout<<buf<<endl;
closesocket(ss);
return 0;
}