源代码he-client.cpp
//ws2_32.lib!!!!!!!!!这个要注意,要在连接库中加个这个库,否则会编译不通过
#include <winsock2.h>
#include <iostream.h>
#include <stdio.h>
#include <io.h>
#include<fstream.h>
#include<stdlib.h>
#define SERVER_PORT 12345
#define BUF_SIZE 100
void fatal(char *string)
{
printf("%s\n", string);
exit(1);
}
char *IP;
void main()
{
int a,IPCH,YN=1,YN1,YN2,i;
char buf[BUF_SIZE],sender[100],geter[100],res[200],*ssname[10];
char *ss=new char;
size_t bytes = 1;
SOCKET s;
FILE *fd;
struct sockaddr_in channel;
WSADATA wsaData;
if (WSAStartup( MAKEWORD( 2, 2 ), &wsaData ))
fatal("Can not find a usable winsock dll");
channel.sin_family = AF_INET;
IP="127.0.0.1";
cout<<"你目前连接的主机IP为^_^:"<<IP<<endl; //主机IP变换
cout<<"需要改变连接的主机的IP,请输入1;不需要请输入2:"<<endl;
cin>>IPCH;
if(IPCH==1)
{cout<<"^_^请输入新IP:";
cin>>IP;
}
else ;
channel.sin_addr.s_addr = inet_addr(IP); //为了简单,设定从本机读存 127.0.0.1
channel.sin_port = htons(SERVER_PORT);
cout<<"要读取资源列表请输入:1;若不要则输入2"<<endl; //在这里我们默认服务器有个资源列表,在c:\\资源.txt(方便查看,服务器的资源)
cin>>YN1;
if(YN1==1)
{
ifstream instuf("c:\\资源.txt",ios::in);
while(!instuf.eof())
{instuf.getline(res,200);
cout<<res<<endl;
}
instuf.close();
}
else ;
/*printf("有多个文件(最多10个)要传输请输入1;不是请输入2:\n"); //多文件传送还没成功通过
cin>>YN2;
if(YN2==1)
{for(i=1;i<=10;i++)
{cout<<"请输入第"<<i<<"个文件路径"<<endl;
cin>>ss;cout<<ss;
if(ss=="aaaa")
cout<<"?????"<<endl;
send(s, sname, strlen(sname)+1, 0);
printf("请输入文件的保存路径A:\n");
cin>>cname;
fd = fopen(cname,"wb"); //读入
while (1) {
bytes = recv(s, buf, BUF_SIZE, 0);
if (bytes <= 0)
{
printf("传输完成^_^\n\n");break;
}
fwrite(buf, sizeof(char), bytes, fd);
}
}
}
*/
while(YN){ //进入读存
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s <0) fatal("socket");
a = connect(s, (struct sockaddr *) &channel, sizeof(channel));
if (a < 0) fatal("connect failed");
printf("^_^请输入需要传输的文件路径:\n");
cin>>sender;
send(s, sender, strlen(sender)+1, 0); //发送
cout<<"要使用默认保存路径?^_^要请输入1;不需要请输入2"<<endl; //为了方便,设了默认保存路径(注意默认保存路径为读取的路径,若本机分区比服务器少时,请看清楚)
cin>>YN2;
if(YN2==1)
{fd = fopen(sender,"wb"); //读入
while (1) {
bytes = recv(s, buf, BUF_SIZE, 0);
if (bytes <= 0) {
printf("传输完成^_^\n\n");goto WWWW ;
}
fwrite(buf, sizeof(char), bytes, fd);
}
}
else ;
printf("^_^那么请输入你想要的保存路径:\n"); //用户自己选择保存路径
cin>>geter;
fd = fopen(geter,"wb"); //读入
while (1) {
bytes = recv(s, buf, BUF_SIZE, 0);
if (bytes <= 0) {
printf("传输完成^_^\n\n");break;
}
fwrite(buf, sizeof(char), bytes, fd);
}
WWWW:;
printf("是否继续传输?\n1:是\n0:否\n");
cin>>YN;
}
WSACleanup( );
}
源代码he-server.cpp
#include <winsock2.h>
#include <stdio.h>
#define SERVER_PORT 12345
#define BUF_SIZE 100
#define QUEUE_SIZE 10
void fatal(char *string)
{
printf("%s\n", string);
exit(1);
}
int main(int argc, char* argv[])
{
int b,l,on = 1;
size_t bytes = 1;
char buf[BUF_SIZE];
struct sockaddr_in channel;
FILE *fd;
SOCKET sa,s;
//以下内容用于初始化DLL库,Winsock的使用需要这一步
WSADATA wsaData;
if (WSAStartup( MAKEWORD( 2, 2 ), &wsaData ))
fatal("Can not find a usable winsock dll");
memset(&channel, 0, sizeof(channel));
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl(INADDR_ANY);
channel.sin_port = htons(SERVER_PORT);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) fatal("socket failed");
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
//setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE , (char *) &on, sizeof(on));
b = bind(s, (struct sockaddr *) &channel, sizeof(channel));
if (b < 0) fatal("bind failed");
l = listen(s, QUEUE_SIZE);
if (l < 0) fatal("listen failed");
while (1) {
sa = accept(s, 0, 0);
if (sa == INVALID_SOCKET)
fatal("accept failed");
bytes = recv(sa, buf, BUF_SIZE,0);
if(bytes <= 0)
fatal("can not get any argument");
fd = fopen(buf, "r");
if (fd == NULL) fatal("open failed");
while (1) {
bytes = fread(buf, sizeof(char), BUF_SIZE, fd);
if (bytes <= 0) break;
send(sa, buf, bytes, 0);
}
fclose(fd);
closesocket(sa);
}
//需要下面这句来关闭之前的DLL初始化
WSACleanup( );
return 0;
}