***********************server.cpp************************
/*
这个程序的功能是在本地监听一个端口,然后把从这对端口来的文件数据数据写到本地
*/
#include
#include
#pragma comment(lib,"wsock32.lib")
#pragma comment(lib,"ws2_32")
#define data_max 100 //控制每次发送数据包的大小
SOCKET _socket;
SOCKET server;
struct sockaddr_in server_addr;
int error;
UINT server_uPort=8473;//监听8473端口
WSADATA WSAData;
char Output_Text[1024]="/0";//接受数据缓冲区
char Input_Text[1024]="gxter!";//发送数据
char data[100];
int Listen_addrlen=sizeof(server_addr);
int end;
char IP_String[20]="218.61.206.199";//本地IP地址
char filepath[1024];
char *cmd="yes!";
//--------------------------------程序开始-----------------------------
int main()
{
printf("input filepath:");
gets(filepath); //输入文件在本地的路径
//------------------------------------------------
error=WSAStartup(0x0002,&WSAData);
if(error=0)
{
printf("SERVER_SOCKET建立错误");
}
_socket=socket(AF_INET,SOCK_STREAM,0);
if(_socket==SOCKET_ERROR)
{
printf("SERVER_SOCKET建立错误");
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons((u_short)server_uPort);
//server_addr.sin_addr.S_un.S_addr=inet_addr(IP_String);//监听
server_addr.sin_addr.S_un.S_addr=INADDR_ANY;
error=bind(_socket,(LPSOCKADDR)&server_addr,sizeof(server_addr));
if(error==SOCKET_ERROR)
{
printf("绑定错误/n");
}
error=listen(_socket,5);
if(error==SOCKET_ERROR)
{
printf("监听失败/n");
}
server=accept(_socket,(LPSOCKADDR)&server_addr,&Listen_addrlen);//listen
if(server==SOCKET_ERROR)
{
printf("Listen_hSocket错误/n");
}
//--------------------------------------------------------------------------------------------
FILE *writefp;
char data[data_max];
char ch;
int error = 1;
int a = 0; //接受数据的数量
int errorr;
if((writefp = fopen(filepath,"wb"))==NULL)//写文件
{
printf("open2 file error!");
}
//--------------------------------------------------------------------------------------------
while(error == 1)
//for(int j=0;j<1640;j++)
{
errorr = recv(server,data,sizeof(data),0);
printf("%d",errorr);
if(errorr == SOCKET_ERROR)//接收
{
printf("recv error!");
}
if(errorr != data_max)
{
printf("----------");
error=0;
break;
}
for (int i=0;i {
ch=data[i];
fputc(ch,writefp);
}
a++;
printf("%d/n",a);
memset(data,0,sizeof(data));
}
//--------------------------------------------------------------------------------------------
fclose(writefp);
closesocket(_socket);
closesocket(server);
WSACleanup();
scanf("%d",&end);
return 0;
}
void return_socket()
{
}
//-----------------------------
/*
建立一个套接字_SOCKET,再建立监听套接字SERVER。
监听成功后关闭_SOCKET和SERVER。
然后建立
*/
***********************client.cpp*************************
//发送文件
//
#include
#include
#include "tiny.h"
#pragma comment(lib,"ws2_32")
#define data_max 100 //控制每次发送数据包的大小
//---------------------------------------------------------
int WSA_return;
WSADATA WSAData;
SOCKET client_hSocket;
struct sockaddr_in client_addr;
BOOL bErr;
UINT client_uPort=8473;//端口
char IP_String[20]="192.168.0.119";//目标端口
char Input_Text[1024]="zhis is client messenger!";
void listen();
//-------------------------------程序开始-----------------
int main()
{
int end=0;
char filepath[1024];
printf("input filepath:");
gets(filepath);
printf("input IP:");
gets(IP_String);
WSA_return=WSAStartup(0x0002,&WSAData);
if(WSA_return==0)//建立成功
{
client_hSocket=socket(AF_INET,SOCK_STREAM,0);
if(client_hSocket==SOCKET_ERROR)
printf("socket出错");
}
else//建立失败
{
printf("初始化错误");
}
client_addr.sin_family=AF_INET;
client_addr.sin_addr.S_un.S_addr=inet_addr(IP_String);
client_addr.sin_port=htons((u_short)client_uPort);
connect(client_hSocket,(LPSOCKADDR)&client_addr,sizeof(client_addr)); //连接
//----------------------------------------------------------------------------------------------
FILE *readfp;
char data[data_max];
char ch;
int error=1;
int a=0; //发送数据包的数量
if((readfp = fopen(filepath,"rb"))==NULL)//读文件
{
printf("open1 file error!");
return 1;
}
//-----------------------------------------------------------------------------------------------
while (error==1)
{
for (int i=0;i {
ch=fgetc(readfp);
data[i]=ch;
if(feof(readfp))
{
error=0;
}
}
end=send(client_hSocket,data,sizeof(data),0);//发送数据
if(end==-1)
printf("发送错误");
else
{
printf("/nOK!");
}
a=a+1;
printf("%d",a);
memset(data,0,sizeof(data));
}
//------------------------------------------------------------------------------------------------
fclose(readfp);
closesocket(client_hSocket);
WSACleanup();
scanf("%d",&end);
return 0;
}