资源简介
使用C++语言编写基于TCP协议的C/S传输文件。具体使用方法见里面的report。
代码片段和文件信息
#include
#include
#include
#include
#include
#define PORT 2680
#define BUFFER_SIZE 1024
#define MAXLINE 512
#pragma comment(lib “WS2_32“)
int main()
{
int recvTimeout = 1000;
char c = 0;
int nNetTimeout = 1000;
WSADATA wsdata;
WORD v = MAKEWORD(20);
char IP[32];
SOCKET ConnectSocket = INVALID_SOCKET;
int len = 0;
char filename[MAXLINE+1];
// Initialize Winsock
if( WSAStartup(v &wsdata) != 0)
{
printf(“WSAStartup failed with error: %d\n“ WSAStartup(v &wsdata));
return 1;
}
sockaddr_in server_addr client_addr;
int size = sizeof(client_addr);
server_addr.sin_family = AF_INET;
printf(“Please input the IP address:“);
scanf(“%s“ &IP);
server_addr.sin_addr.S_un.S_addr = inet_addr(IP);
server_addr.sin_port = htons(PORT);//htons transfer to u_short
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET SOCK_STREAM 0);
if (ConnectSocket == INVALID_SOCKET)
{
printf(“socket failed with error: %ld\n“ WSAGetLastError());
WSACleanup();
return 1;
}
//connect
if (SOCKET_ERROR == connect(ConnectSocket (sockaddr *)&server_addr sizeof(server_addr)))
{
printf(“Unable to connect to server!\n“);
WSACleanup();
return 1;
}
char str[20] = ““;
recv(ConnectSocket str sizeof(str) 0);
printf(“server:%s“ str);
char buffer[BUFFER_SIZE];
while(c != 27)
{
memset(filename 0 MAXLINE+1);
printf(“\nplease input the file name in the server:“);
scanf(“%s“&filename);
memset(buffer 0 BUFFER_SIZE);
strncpy(buffer filenamestrlen(filename)>BUFFER_SIZE ? BUFFER_SIZE:strlen(filename));
// Send an initial buffer
if(send(ConnectSocket buffer BUFFER_SIZE 0) == SOCKET_ERROR)
{
printf(“send failed with error: %d\n“ WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
//open the file to write
FILE *fp = fopen(filename “wb“);
memset(buffer 0 BUFFER_SIZE);
setsockopt(ConnectSocket SOL_SOCKET SO_RCVTIMEO (char *)&recvTimeout sizeof(int));
len = recv(ConnectSocket buffer BUFFER_SIZE 0);
// int length = fwrite(buffer sizeof(char) len fp);
// fflush(fp);
if (len == -1)
{
printf(“can not find the file\n“);
}
else if(fp == NULL)
{
printf(“can not open to write the file %s\n“filename);
return 0;
}
else
{
int length = 0;
length = recv(ConnectSocket buffer BUFFER_SIZE 0);
if (fwrite(buffer sizeof(char) length fp) < length)
{
// fflush(fp
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-11-10 16:20 assign4-516111910213\
目录 0 2018-11-05 23:28 assign4-516111910213\client2\
目录 0 2018-11-01 22:13 assign4-516111910213\client2\bin\
目录 0 2018-11-01 22:13 assign4-516111910213\client2\bin\Debug\
文件 35471 2018-11-01 21:51 assign4-516111910213\client2\bin\Debug\client2.exe
文件 1145 2018-11-01 22:13 assign4-516111910213\client2\client2.cbp
文件 317 2018-11-05 21:13 assign4-516111910213\client2\client2.depend
文件 241 2018-11-05 23:28 assign4-516111910213\client2\client2.layout
文件 4717 2018-11-01 21:51 assign4-516111910213\client2\main.cpp
目录 0 2018-11-01 22:13 assign4-516111910213\client2\obj\
目录 0 2018-11-01 22:13 assign4-516111910213\client2\obj\Debug\
文件 7131 2018-11-01 21:51 assign4-516111910213\client2\obj\Debug\main.o
文件 439926 2018-11-10 16:20 assign4-516111910213\report.pdf
目录 0 2018-11-01 22:16 assign4-516111910213\server2\
文件 9728 2018-11-01 19:59 assign4-516111910213\server2\123.doc
文件 85072 2016-08-08 14:12 assign4-516111910213\server2\CYVW3880.jpg
文件 126208 2018-10-19 21:14 assign4-516111910213\server2\assign4.pdf
目录 0 2018-11-01 22:13 assign4-516111910213\server2\bin\
目录 0 2018-11-01 22:13 assign4-516111910213\server2\bin\Debug\
文件 34488 2018-11-01 21:45 assign4-516111910213\server2\bin\Debug\server2.exe
文件 3680 2018-11-01 21:45 assign4-516111910213\server2\main.cpp
文件 1308294 2018-06-24 15:38 assign4-516111910213\server2\nba.gif
目录 0 2018-11-01 22:13 assign4-516111910213\server2\obj\
目录 0 2018-11-01 22:13 assign4-516111910213\server2\obj\Debug\
文件 6397 2018-11-01 21:45 assign4-516111910213\server2\obj\Debug\main.o
文件 1145 2018-11-01 22:13 assign4-516111910213\server2\server2.cbp
文件 139 2018-11-01 21:46 assign4-516111910213\server2\server2.depend
文件 239 2018-11-01 22:16 assign4-516111910213\server2\server2.layout
文件 28 2018-10-30 16:12 assign4-516111910213\server2\test.txt
文件 5 2018-10-30 19:10 assign4-516111910213\server2\test2.txt
文件 5 2018-11-01 13:51 assign4-516111910213\server2\test3.txt
............此处省略0个文件信息
相关资源
- Visual C++.NET图像处理编程源代码 陆宗
- Boost.Asio C++ Network Programming(2nd) 无水
- Internet选项设置-C++
- C语言实现telnet客户端
- RakNet 基于UDP网络传输协议的C++网络库
- Ethernet 帧结构解析程序
- [原创]实现用VB.Net(C#)开发K3 BOS 插件
- LeNet-5神经网络——C源代码
- sap nco30 dotnet40 x64 nco3.0 net4.0 x64
- WPViewPDF v3.11 VCL/.NET/ActiveX (x32/x64)
- Boost.Asio C++ Network Programming(2nd) az
- C语言操作netcdf的安装文件
- 《Computer Systems--A Programmer‘s Perspect
- netcdf 4.4.1 c/c++类库windows64位绿色版
- VC++文件仿搜狗
- Virtual Machine Design and Implementation in C
- Programming_With_Microsoft_Visual_C++_.Net
- 基于OPNET的TCP协议研究与仿真论文
- telnet客户端
- Boost.Asio C++ Network Programming 2nd edition
- Expert C++ / CLI .NET for Visual C++ Programme
- C++.Net编写的记事本程序
- Visual C++.NET 精彩编程集锦
- telnet客户端源码
- FastReport_.NET_NetFx2-NetFx4_2019.3.5全功能破
- Learning OpenCV 3 Computer vision in C++ 无水印
- 精通net互操作pdf带目录
- Computer Graphics Programming in OpenGL with C
- Ms Press - Programming with Microsoft Visual C
- vc++.net入门教程
评论
共有 条评论