| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1428 人关注过本帖
标题:memcpy(),ZeroMemory();怎么用:
只看楼主 加入收藏
xhselina
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-10-23
收藏
 问题点数:0 回复次数:0 
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;
}
搜索更多相关主题的帖子: memcpy ZeroMemory 
2008-12-11 17:48
快速回复:memcpy(),ZeroMemory();怎么用:
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024764 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved