#include <WINSOCK2.H>
#include <stdio.h>
#include <conio.h>
#pragma comment(lib,"WSOCK32.lib")
char * host_name = "192.168.58.128"; // local host
int port = 8000;
int main(int argc, char *argv[])
{
int err;
WORD versionRequired;
WSADATA wsaData;
versionRequired=MAKEWORD(1,1);
err=WSAStartup(versionRequired,&wsaData);//协议库的版本信息
if (!err)
{
printf("客户端嵌套字已经打开!/n");
}
else
{
printf("客户端的嵌套字打开失败!/n");
getch();
return 0;//结束
}
char buf[1024];
char message[256];
int len;
int lenTotal;
int sd;
int fd;
FILE * fp;
struct sockaddr_in pin;
struct hostent *nlp_host;
fp = fopen("a.txt","rb");
if(NULL == fp )
{
printf("File:/t%s Can Not Open To Read/n", argv[1]);
getch();
exit(1);
}
if ((nlp_host = gethostbyname(host_name)) == 0) {
printf("Error resolving local host/n");
getch();
exit(1);
}
memset(&pin,0,sizeof(pin));
//bzero(&pin, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr *)(nlp_host->h_addr))->s_addr;
pin.sin_port = htons(port);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Error opening socket/n");
getch();
exit(1);
}
//connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//开始连接
if (connect(sd,(SOCKADDR*)&pin, sizeof(pin)) == -1) { //跟主机建立Tcp连接
printf("Error connecting to socket/n");
getch();
exit(1);
}
lenTotal = 0;
while((len = fread(buf,sizeof(char),1024,fp))>0){//发送文件
lenTotal += len;
send(sd,buf,len,0);
}
printf("send file success!send len = %d/n",lenTotal);
fclose(fp);
WSACleanup();
getch();
system("pause");
return(0);
}