windows c语言传输文件,socket应用(二) windows下用c语言进行socket连接,进行文件收发...

#include

#include

#include

#include

#include

#pragma comment(lib, "ws2_32.lib")

#define MY_PORT 8000

#define CONNECT_COUNT 10

int main()

{

int sock_listen,new_sock;

sockaddr_in server_addr;

sockaddr_in client_addr;

int sin_size;

int numbytes;

char recvbuf[1024];

FILE *fp;

char filepath[10] =

{'c',':','\\','1','.','t','x','t','\0'};

bool FileIsClose = false;

//创建socket

WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2,2), &wsaData );

//加载winsock2.2库

if ( iResult != NO_ERROR

)

printf("Error at WSAStartup()\n");

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(MY_PORT);

server_addr.sin_addr.s_addr = INADDR_ANY;

memset(&(server_addr.sin_zero), 0,

8);

if((fp=fopen(filepath, "wb")) == NULL)

{

printf("cannot open the file....:%s\n",

filepath);

return -1;

}

//创建一个socket,将socket描述符保存到sock_listen中

if((sock_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))

== -1)

{

printf("socket create error....sock_listen:%d\n",

sock_listen);

return -1;

}

//将socket描述符与server_addr绑定

if (bind(sock_listen,(struct sockaddr *)&server_addr,

sizeof(struct sockaddr)) == -1)

{

printf("bind error\n");

return -1;

}

//监听socket描述符,即监听server_addr,第二个参数是监听的队列长度

if(listen(sock_listen, CONNECT_COUNT) == -1)

{

printf("listen error\n");

return -1;

}

while(1)

{

sin_size=sizeof(struct sockaddr_in);

//用client_addr与客户端的socket进行连接通信,假如没有客户端连接请求,则不退出此函数

if((new_sock=accept(sock_listen,(struct sockaddr

*)&client_addr,&sin_size))==-1)

{

printf("accept error\n");

fclose(fp);

FileIsClose = true;

closesocket(new_sock);

return -1;

}

if(FileIsClose)

{

if((fp=fopen(filepath, "wb")) == NULL)

{

printf("cannot open the file....:%s\n",

filepath);

return -1;

}

}

while(1)

{

numbytes = recv(new_sock, recvbuf, 100, 0);

if (numbytes == -1 ||numbytes == 0)

{

printf("recv error\n");

closesocket(new_sock);

fclose(fp);

FileIsClose = true;

break;

}

recvbuf[strlen(recvbuf)] = '\0';

fwrite(recvbuf,strlen(recvbuf),1,fp);

// printf("Received: %s\n",recvbuf);

}

printf("...............file path:%s\n", filepath);

// while(waitpid(-1,NULL,WNOHANG) > 0);

}

return 0;

}

客户端:

#include

#include

#include

#include

#include

#pragma comment(lib, "ws2_32.lib")

#define MY_PORT 8000

int main()

{

int client_fd;

struct sockaddr_in client_socket;

char connect_ip[20] = {0};

char send_str[1024];

char exit_str[2] = {'x','\0'};

int length = 0;

WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2,2), &wsaData );

//加载winsock2.2库

if ( iResult != NO_ERROR

)

printf("Error at WSAStartup()\n");

printf("Server IP..............:");

scanf("%s", connect_ip);

length = strlen(connect_ip);

connect_ip[length] = 0;

client_socket.sin_family = AF_INET;

client_socket.sin_port = htons(MY_PORT);

client_socket.sin_addr.s_addr = inet_addr(connect_ip);

memset(&(client_socket.sin_zero), 0,

8);

while(1)

{

FILE *fp;

char filepath[100];

printf("Please enter a file path....:");

scanf("%s",filepath);

if((fp=fopen(filepath, "rb")) == NULL)

{

printf("cannot open the file....:%s\n",

filepath);

continue;

}

//创建一个socket,将socket描述符保存到client_fd中

if((client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) ==

-1)

{

printf("socket create error....client_socket:%d\n",

client_fd);

return -1;

}

//连接服务器

if(connect(client_fd, (struct sockaddr *)&client_socket,

sizeof(struct sockaddr)) == -1)

{

printf("connect error!\n");

closesocket(client_fd);

return 1;

}

while(!feof(fp))

{

fgets(send_str, 1024,fp);

send_str[strlen(send_str)] = 0;

if(strlen(send_str) != 0)

{

if (send(client_fd,send_str,100,0)==-1)

{

printf("send error!\n");

break;

}

}

//发送数据

send_str[0] = 0;

}

closesocket(client_fd);

fclose(fp);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值