没有合适的资源?快使用搜索试试~ 我知道了~
网络编程程序练习题.txt
资源推荐
资源详情
资源评论
注:以下程序均以控制台应用程序(字符界面)方式完成。
1.编写程序,查询本机的主机名称及 IP 地址。
#include "WinSock2.h"
#include "iostream"
#pragma comment(lib, "ws2_32.lib")
#pragma warning (disable:4996)
using namespace std;
int main(int argc, char **argv)
{
struct hostent *hptr;
char **pptr;
char hostname[256];
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
cout << "加载WinSock DLL失败!\n";
return 0;
}
if (gethostname(hostname, sizeof(hostname)))
{
cout << "获取主机名字失败!\n" << endl;
WSACleanup();
return 0;
}
cout << "本机名字为:" << hostname << endl;
if ((hptr = gethostbyname(hostname)) == NULL)
{
cout << "通过主机名获取本机IP地址失败!\n" << endl;
1.编写程序,查询本机的主机名称及 IP 地址。
#include "WinSock2.h"
#include "iostream"
#pragma comment(lib, "ws2_32.lib")
#pragma warning (disable:4996)
using namespace std;
int main(int argc, char **argv)
{
struct hostent *hptr;
char **pptr;
char hostname[256];
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
cout << "加载WinSock DLL失败!\n";
return 0;
}
if (gethostname(hostname, sizeof(hostname)))
{
cout << "获取主机名字失败!\n" << endl;
WSACleanup();
return 0;
}
cout << "本机名字为:" << hostname << endl;
if ((hptr = gethostbyname(hostname)) == NULL)
{
cout << "通过主机名获取本机IP地址失败!\n" << endl;
WSACleanup();
return 0;
}
pptr = hptr->h_addr_list;
cout << "本机IP地址:" << endl;
while (*pptr != NULL)
{
cout << inet_ntoa(*(struct in_addr *) (*pptr)) << endl; pptr++;
}
WSACleanup();
return 0;
}
2. 编写一个 TCP 客户程序,该程序把本机 C:盘根目录下的一个文件 a.txt 的内容发送给服
务器(文件内容发完就关闭连接)。
#include <iostream>
#include<WinSock2.h>
#include <fstream>
#define PORT 65432
#pragma warning(disable:4996)
#pragma comment(lib, "ws2_32.lib")
using namespace std;
struct fileMessage {
char fileName[256];
long int fileSize;
};
void send_file(int sock_client)
return 0;
}
pptr = hptr->h_addr_list;
cout << "本机IP地址:" << endl;
while (*pptr != NULL)
{
cout << inet_ntoa(*(struct in_addr *) (*pptr)) << endl; pptr++;
}
WSACleanup();
return 0;
}
2. 编写一个 TCP 客户程序,该程序把本机 C:盘根目录下的一个文件 a.txt 的内容发送给服
务器(文件内容发完就关闭连接)。
#include <iostream>
#include<WinSock2.h>
#include <fstream>
#define PORT 65432
#pragma warning(disable:4996)
#pragma comment(lib, "ws2_32.lib")
using namespace std;
struct fileMessage {
char fileName[256];
long int fileSize;
};
void send_file(int sock_client)
剩余30页未读,继续阅读
资源评论
逍遙-冷風
- 粉丝: 9
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功